BILANGAN PRIMA
program
#include <iostream>
#include <conio.h>
#include <ctype.h>
#include <math.h>
#include <cstdlib>
using namespace std;
//return 1 if n is prime, 0 otherwise
int isPrime(int p);
int main ()
{
for (int n=1; n<50;n++)
if (isPrime(n)) cout<<n<<" ";
getch();
cout<<endl;
}
int isPrime (int p)
{
float sqrtp=sqrt(p);
if(p<2) return 0; //2 is
if (p==2) return 1;
if (p%2==0) return 0; //2 is
for (int d=3; d<=sqrtp; d+=2)
if (p%d==0) return 0;
return 1;
}
0 komentar:
Posting Komentar