Prime Number
PRIME NUMBER
A prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole numbers that can be divided evenly into another number. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. Numbers that have more than two factors are called composite numbers. The number 1 is neither prime nor composite.
For every prime number p, there exists a prime number p' such that p' is greater than p. This mathematical proof, which was demonstrated in ancient times by the Greek mathematician Euclid, validates the concept that there is no "largest" prime number. As the set of natural numbers N = {1, 2, 3, ...} proceeds, however, prime numbers generally become less frequent and are more difficult to find in a reasonable amount of time. As of this writing, the largest known prime number has more than 23 million digits. It is referred to as M77232917 and has one million more digits than the previous record holder.
C Program to Check Prime Number
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i = 2; i <= n/2; ++i)
{
// condition for nonprime number
if(n%i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Output:
Enter a positive integer: 3 3 is a prime number.
Thin is not the most efficient code.
ReplyDelete