Prime Number Formula

前端 未结 13 1595
情话喂你
情话喂你 2020-12-06 08:47

I am trying to write a prime number function in C# and I am wondering if the follow code will work. It \"appears\" to work with the first 50 numbers or so. I just want to ma

13条回答
  •  时光取名叫无心
    2020-12-06 09:34

    This is a simple code for find prime number depend on your input.

      static void Main(string[] args)
        {
            String input = Console.ReadLine();
            long num = Convert.ToInt32(input);
            long a, b, c;
            c = 2;
            for(long i=3; i<=num; i++){
                b = 0;
                for (long j = 2; j < i ; j++) {
                    a = i % j;
                    if (a != 0) {
                        b = b+1;
                    }
                    else {
                        break;
                    }
                }
    
                if(b == i-2){
                    Console.WriteLine("{0}",i);
                }
            }
            Console.ReadLine();
        }
    

提交回复
热议问题