0

Solution for prime number

/*here i wrote a code to find prime number if anyone need. Ask me if you have any question. /* class Program { static void Main(string[] args) { primeval pm=new primeval(); int result=pm.primeMethod(17); if(result==0) { Console.WriteLine("is not prime"); } else Console.WriteLine("is prime"); } } class primeval{ public int primeMethod(int num) { int i; for(i=2;i<=num-1;i++) { if(num%i==0) { return 0; } } if(i==num) { return 1; } return 0; } }

15th Feb 2017, 12:31 AM
Shawon
Shawon - avatar
2 Respostas
+ 3
Put it in the code playground instead of section dedicated to ask and answer questions.
15th Feb 2017, 2:24 AM
Jakub Stasiak
Jakub Stasiak - avatar
0
You can speed it up quite a bit by only testing until i <= sqrt(num). Another easy way to speed it up a bit is to only test odd numbers after you've tested for num%2 == 0.
15th Feb 2017, 2:45 AM
Robobrine
Robobrine - avatar