c# - Prime Number Formula -


I am trying to write a master number function in C # and I'm thinking that the compliance code will work. It "appears" to work with the first 50 numbers, or I just want to make sure that it does not matter how big the number is:

  Fixed Bull ISPRM ( Int number) {if ((number == 2) || (number == 3) || (number == 5) || (number == 7) || (number == 9)) true return; If ((number% 2! = 0) & amp; amp; amp; amp; (number% 3! = 0) & amp; amp; amp; amp; amp; Amp; amp; amp; amp; amp; (number% 9! = 0) & amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp amp; amp; amp; amp amp; amp; amp; amp; amp; amp; amp; amp से; IN.% (& nbsp; 4! = 0) & amp; amp; amp; amp; amp; amp; Amp; nbsp; (number% 6! = 0)) True; return false; }  

No it will not work! For example, try 121 = 11 * 11 which is not explicitly prominent.

For any number given to your function, this is a product of prime numbers X1, X2, ..., Xn (where n> = 2 ) With all of them greater than or equal to 11, your function will come true. (And also, as already mentioned, 9 is not prominent).

With Wikipedia, you can see that:

In mathematics, a prime number (or a principal) is a natural number in which two different natural number divisors : 1 And Own

Then there is a very simple and naïve algorithm whether a number is prime or not:

  Public Bull CalcIsPrime (int number) {if (number == 1) Returns the wrong; If (number == 2) is true; If (number% 2 == 0) wrong return; // (Number I = 2; i & lt; num; i ++) also for the correct calculation for the number {// '4' to include the correct calculation (if the number% i == 0) returned incorrectly (number% i == 0) ; } Back true; }  

Check here for better algorithms:

If you want to check your code, then enter a test In test case written in [Theory] [Membership (Namesf (Prim nominal data)] Public Zero CalcIsPrimeTest (int number, expected bol) {Assert.Equal (Expected, CalcIsPrime (number)); } Public stable IEnumerable & lt; Object [] & gt; Prime Timberstasta () {returns new item returns [] {0, false}; Yield returns new object [] {1, false}; Returns new object [] {2, true}; Returns new object [] {3, true}; Yield returns new object [] {4, false}; Yield returns new object [] {5, true}; Yield returns new object [] {6, false}; Returns new object [] {7, true}; Yield returns new object [] {8, false}; Yield returns new object [] {9, false}; Yield returns new object [] (10, false); Yield returns new object [] {11, true}; Returns new object [] {23, true}; Yield returns new object [] {31, true}; Yield returns new object [] (571, true); Returns new object [] (853, true); Yield returns new object [] (854, false); Yield returns new object [] (997, true); Yield returns new object [] (999, false); }


Comments