0

im new to C# and ive been trying to solve project euler question number 3 and got the following error: i understand i need to decler a new BigInt before i use it, but i just cant figure out the syntax. would love to get some help!

using System;
using System.Numerics;
namespace primes
{
    class Question3
    {
       static void Main()
       {
        BigInteger Memash = 600851475143 ;
        for(BigInteger i = 100 ; i <= Memash ; i++)
            {
                if(primechec(i))
                    {
                        Console.Write("this number is prime: " + i);
                        if(Memash % i == 0)
                        {
                            Console.Write(i);
                        }
                    }
            }
       } 

       public bool primechec(BigInteger Naor)
       {
        for(int j = 2 ; j <= Naor ; j++)
        {
            if(Naor % j == 0)
            {
                return false;
            }
        }
        return true;
       }
    }
}
Se1234
  • 23
  • 2
  • 2
    Please follow sensible naming conventions. There's no good reason not to. `primes` should be `Primes`, `Memash` should be `memash`, `primechec` should be `PrimeCheck` and `Naor` should be `naor`. You should [read this](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines). – user18387401 Aug 06 '22 at 16:48
  • Does this answer your question? [C# error: "An object reference is required for the non-static field, method, or property"](https://stackoverflow.com/questions/10264308/c-sharp-error-an-object-reference-is-required-for-the-non-static-field-method) – Lance U. Matthews Aug 06 '22 at 16:56

1 Answers1

3

The problem is that your Main method is static, so it can't call instance methods in the same class. You need to declare primechec static as well, so you're calling a static method from a static method.

user18387401
  • 2,514
  • 1
  • 3
  • 8
  • so how can i fix it. adding static to both would help? – Se1234 Aug 06 '22 at 16:52
  • @Se1234 "You need to declare primechec static as well" – gunr2171 Aug 06 '22 at 16:58
  • @Se1234, there's only two sentences in my answer and it seems that you didn't bother to read the second one. – user18387401 Aug 07 '22 at 02:07
  • @user18387401 why do you think i didnt bother to read it? i did and it really helped. i just forgot to reply back because i continued the project euler im sorry. it really helped thank you – Se1234 Aug 07 '22 at 15:22
  • @Se1234, the reason I thought that you didn't bother to read that second sentence is because you proceeded to ask whether it would help to do what that sentence explicitly instructs you to do. It seems an odd question to ask if you've already read the answer. Anyway, if this answer resolved your issue, please accept it by clicking the check/tick mark next to it. That will let people see that your question has been answered and you need no more help without having to open the question and read a comment on the answer. – user18387401 Aug 08 '22 at 08:09