51 is not a Prime number as it is divisible by 3. A prime number has only 2 factors which are 1 and itself. Composite numbers are every other positive integer except 1 and 0. 1 and 0 are neither prime, nor composite.
i=2 rem=1 echo "Enter a number" read num if [ $num -lt 2 ] then echo "$num is not prime" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ] do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ] then echo "$num is prime" else echo "$num is not prime" fi
You need two utility functions. The first determines if a given number is prime or not. The second finds the next prime after a given number. The following function can be used to determine if a given integer is prime: bool is_prime (const unsigned num) { if (num<2) return false; if (0==(num%2)) return num==2; unsigned max_factor = (unsigned) sqrt ((double) num) + 1; unsigned factor; for (factor=3; factor<max_factor; ++factor) if (0==(num%factor)) return false; return true; } The following function can be used to determine the next prime after the given integer: unsigned next_prime (unsigned num) { while (!is_prime (++num)); return num; } Now you can print a series of primes using the following: int main (void) { unsigned num=1; while (num<10000) { num = next_prime (num); printf ("%d is prime\n", num); } return 0; }
YES 5 is a prime number!
31 is prime, but 51 is not as 51=3*17.
51 is not a prime number: 3x17=51
You do not need OOP for this because the first 100 prime numbers are all integral types. All you need is a function to determine if a given integral is prime or not, and another to find the next prime after a given integral: bool is_prime (const unsigned int num) { unsigned factor, max_factor; /* anything less than 2 is non-prime */ if (num<2) return false; /* 2 is the only even prime */ if (!(num%2) return num==2; /* determine largest potential factor of num (square root of num)*/ max_factor = sqrt (num); /* test each odd factor from 3 onwards */ for (factor=3; factor<=max_factor; factor+=2) { if (!(num%factor)) return false; /* num is non-prime */ } return true; /* the number is prime */ } unsigned next_prime (unsigned int num) { /* increment num until num is prime */ while (!is_prime (++num)); return num; } /* test the functions */ int main (void) { unsigned num, count; count=0; num=0; printf ("Printing the first 100 prime numbers:\n"); while (count<100) { num=next_prime (num); printf ("%d is prime\n", num); ++count; } return 0; }
No.
i=2 rem=1 echo "Enter a number" read num if [ $num -lt 2 ] then echo "$num is not prime" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ] do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ] then echo "$num is prime" else echo "$num is not prime" fi
int number; int i=2; while (i<number) { if(number%i==0) { printf("Not a prime no."); break; } else printf("number entered is prime"); getch(); }
/*Determine prime numbers between 1 to 50*/ #include<stdio.h> #include<conio.h> main() { int num, i ,n; clrscr(); printf ( "\nEnter a number limit" ) ; scanf ( "%d", &n ) ; for ( num=2; num <= 50; num++ ) { for ( i=2; i <= num - 1; i++ ) { if ( num % i == 0 ) { Break; continue; } } if (num==i) printf(\n The prime number=%d", num); continue; } getch() }
Yes, 73 is a prime number.
i=2 rem=1 echo -e "Enter a number: \c" read num if [ $num -lt 2 ]; then echo -e "$num is not prime\n" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ]; do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ]; then echo -e "$num is prime\n" else echo -e "$num is not prime\n" fi
#include#includebool is_prime(unsigned num) {unsigned max, factor;if (num
You need two utility functions. The first determines if a given number is prime or not. The second finds the next prime after a given number. The following function can be used to determine if a given integer is prime: bool is_prime (const unsigned num) { if (num<2) return false; if (0==(num%2)) return num==2; unsigned max_factor = (unsigned) sqrt ((double) num) + 1; unsigned factor; for (factor=3; factor<max_factor; ++factor) if (0==(num%factor)) return false; return true; } The following function can be used to determine the next prime after the given integer: unsigned next_prime (unsigned num) { while (!is_prime (++num)); return num; } Now you can print a series of primes using the following: int main (void) { unsigned num=1; while (num<10000) { num = next_prime (num); printf ("%d is prime\n", num); } return 0; }
No, 51 is not prime. 51's factors are 1, 3, 17, and 51. 17*3=51.
(defun prime (num) (if (< 2 num) (do ((dividend 2 (1 + dividend)) (chk-to (sqrt num))) ((equal (rem num dividend) 0)) (when (<= chk-to dividend) (return t))) t))
#include<iosys.h> #include<math.h> // for sqrt() bool is_prime (unsigned num) { if (num<2) return false; // 2 is the first prime if (!(num%2)) return num==2; // 2 is the only even prime // largest potential factor is square root of num unsigned max = (unsigned) sqrt ((double) num) + 1; // test all odd factors for (unsigned factor=3; factor<max; factor+=2) if (!(num%factor)) return false; return true; // if we get this far, num has no factors and is therefore prime } int main (void) { // test all nums from 0 to 100 inclusive for (unsigned num=0; num<=100; ++num) { if (is_prime (num)) printf ("%d is prime\n", num); else if (num>0) printf ("%d is composite\n", num); else printf ("%d is neither prime nor composite\n", %d); } return 0; }