can u suggest an algorithm to check prime or not?
The fundamental task in looking for prime numbers is to confirm that your candidate is not a multiple of any prime number less than itself. To accomplish this, you can either keep a list of the prime numbers you find, and check each candidate for candidate % prime for each prime you have found. If you can't find a prime for which candidate % prime == 0, then candidate is a prime. A somewhat simpler (perhaps) solution would be to check each candidate against the numbers smaller than itself and see if any of them divide your candidate. If so then it must have a prime factor and is therefore non-prime.
You would check the number from 2 to the square root of the number you are trying to decide if it is prime or not. If any of them divide evenly (most programs have a mod type function which returns the remainder of a division problem) then it is not prime. No reason to check past the square root of the number.
see that a prime number yields a remainder 0 only when it is divide by itself and 1.so make a variable to count how many numbers yields a remanider 0 when the given number is divided by a series of numbers.if the variable count is 2 then it is a prime.
I'm pretty sure you didn't mean an actual algorithm, but just in case: http://en.wikipedia.org/wiki/Primality_test#Fast_deterministic_tests
Join our real-time social learning platform and learn together with your friends!