algorithm in c to find largest prime number in an interval f
algorithm in c to find largest prime number in an interval
for example, to find the largest prime number between 2 and 1000
the output should be 997
Solution
Algorithm isPrime(n):
for i = 2 : sqrt(n)://For values between 2 and sqrt(n) inclusive.
if n % i == 0: //If there is a factor in between.
return false; //Which means its not a prime number.
return true; //If no factors, return true.
Algorithm LargestPrimeInBetween(first, last):
for i = last : -1 : first //For value from last down to first inclusive.
if isPrime(i) //If the number is prime using the other algorithm.
return i; //Thats the largest prime in the range.
return -1; //If no primes encounter, there is no prime in between.
