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; } }
2 Réponses
+ 3
Put it in the code playground instead of section dedicated to ask and answer questions.
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.