S
someone else
hi all
I'm a newbie to this group. my apologies if I break any rules.
I've wrote a simple program to find the first 1,000,000 primes, and to find
all primes within any range (up to 200 * 10^12)
it's pretty efficient, it took 15 minutes to compute the first 1,000,000
primes.
I would like to here your opinion about it
p.s. I'm not a mathematician, in fact, my math level is primary school
(hardly).
please Cc all replies to (e-mail address removed)
replace the aesqq with (hrneo) before the @,
thanks
michael
//compiler: ms visual c++ 7.0
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
long nth,pause;
//************collect info from user**************
printf("\t***WELCOME***\nthis program displays all prime numbers up to about
2 billion\nsince the output proccess taks a lot of resources,if you wish to
display large numbers, it is recommended that you set the Nth prime to be
larger then 100");
printf("\n\nplease enter the Nth prine to be displayed, if you select 10,
the 10th, 20th, 30th... primes will be displayed.\n\nplease select a number
between 1 and 10,000, or 0 to exit\n");
fflush(stdin);
scanf("%d",&nth);
if (!nth) return 0;
printf("please enter after how meny numbers to pause. plese select a number
between 2 and 10,000\n");
fflush(stdin);
scanf("%d",&pause);
//************end collect info from user**************
//create and arry to store the first 5000 primes:
long *prime = new long[1000000];long counter=3;bool flag =1;
prime[0]=1;prime[1]=2;prime[2]=3; //set the first three primes
printf("\n\n\t\t---finished\n\n"); getch();
// i is the number that will be checked if it's a prime
//loop with i = 5 initially, add 2 to i evey cycle,terminate if the counter
is greater then 10000000
for (long i=5;counter<1000000;i+=2,flag=1)
{
//check all primes that satisfy sqrt(i) >= prime
for (long j=2;prime[j]<long(sqrt(double(i))+1);j++)
{
if (i%prime[j]==0)//i is not a prime, go back to the first loop
{
flag=0;
break;
}
}
if (flag)
// now we know that i is a prime (since it's not divisible by any prime that
( root(i) >= prime )
{
prime[counter]=i;
counter++;
if(counter%nth==0) printf("\t%ld",i);
if (counter%(nth*pause)==0) getch();
flag=0;
}
else
continue;
}
printf("\n\n***************************finished******************\n\npress
any key to exit");
getch();
return 0;
}
/**************************
//code to store the first 1000000 primes to a binary file:
//place the code at the END of the file
FILE *fp=fopen("15000pri.bin","r+b");
if (!fp) printf("unable to open file");
fwrite(prime,sizeof(prime)*1000000,1,fp);
if (fclose(fp))
printf("\n\nfile not closed\n\n");
else
printf("\n\nfile successfully closed\n\n");
getch();
//code to get the first 1000000 primes from the binary file:
//place the code right after the declaration of *prime
FILE *fp=fopen("15000pri.bin","r+b");
if (!fp) printf("unable to open file");
fread(prime,sizeof(prime)*1000000,1,fp);
if (fclose(fp))
printf("\n\nfile not closed\n\n");
else
printf("\n\nfile successfully closed\n\n");
getch();
//code to check the chances of a number being a prime when number%pause==0
// pause is a variable entered by the user
double chance = 1.0;
for (long k=1;k<1000000;k++)
{
if (k%(pause/1000)==0) printf("%ld ",prime[k]);
chance -= double(1.0/double(prime[k])*chance);
if (k%pause==0) {printf("\t%f\t%ld",chance,k); getch(); }
}
//code to check primes within a range (maximum 1,000,000,000,000)
// you must have the first 1,000,000 primes loaded from a file or calculated
into the array (prime[1000000])
__int64 lower,upper;
printf("\n\nplease enter the lower range.\n\nplease select a number between
5 and 10^12 (1, and 12 zeroes) , or 0 to exit\n");
fflush(stdin);
scanf("%I64",&lower);
if (!nth) return 0;
printf("please enter the lower range.\n\nplease select a number between 5
and 10^12 (1, and 12 zeroes)");
fflush(stdin);
scanf("%I64",&upper);
if (upper<5 || upper>pow(10.0,12.0) || lower<5 || lower>pow(10.0,12.0))
{ printf("invalid range...exiting"); getch(); return 0; }
for (__int64 l=lower;l<=upper;l+=2,flag=1)
{
//check all primes that satisfy sqrt(i) >= prime
for (long m=2;prime[m]<long(sqrt(double(l))+1);m++)
{
if (l%prime[m]==0)//i is not a prime, go back to the first loop
{
flag=0;
break;
}
}
if (flag)
// now we know that i is a prime (since it's not divisible by any prime that
( root(i) >= prime )
{
printf("\t%I64",l);
flag=0;
}
else
continue;
}
//****************************/
I'm a newbie to this group. my apologies if I break any rules.
I've wrote a simple program to find the first 1,000,000 primes, and to find
all primes within any range (up to 200 * 10^12)
it's pretty efficient, it took 15 minutes to compute the first 1,000,000
primes.
I would like to here your opinion about it
p.s. I'm not a mathematician, in fact, my math level is primary school
(hardly).
please Cc all replies to (e-mail address removed)
replace the aesqq with (hrneo) before the @,
thanks
michael
//compiler: ms visual c++ 7.0
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
long nth,pause;
//************collect info from user**************
printf("\t***WELCOME***\nthis program displays all prime numbers up to about
2 billion\nsince the output proccess taks a lot of resources,if you wish to
display large numbers, it is recommended that you set the Nth prime to be
larger then 100");
printf("\n\nplease enter the Nth prine to be displayed, if you select 10,
the 10th, 20th, 30th... primes will be displayed.\n\nplease select a number
between 1 and 10,000, or 0 to exit\n");
fflush(stdin);
scanf("%d",&nth);
if (!nth) return 0;
printf("please enter after how meny numbers to pause. plese select a number
between 2 and 10,000\n");
fflush(stdin);
scanf("%d",&pause);
//************end collect info from user**************
//create and arry to store the first 5000 primes:
long *prime = new long[1000000];long counter=3;bool flag =1;
prime[0]=1;prime[1]=2;prime[2]=3; //set the first three primes
printf("\n\n\t\t---finished\n\n"); getch();
// i is the number that will be checked if it's a prime
//loop with i = 5 initially, add 2 to i evey cycle,terminate if the counter
is greater then 10000000
for (long i=5;counter<1000000;i+=2,flag=1)
{
//check all primes that satisfy sqrt(i) >= prime
for (long j=2;prime[j]<long(sqrt(double(i))+1);j++)
{
if (i%prime[j]==0)//i is not a prime, go back to the first loop
{
flag=0;
break;
}
}
if (flag)
// now we know that i is a prime (since it's not divisible by any prime that
( root(i) >= prime )
{
prime[counter]=i;
counter++;
if(counter%nth==0) printf("\t%ld",i);
if (counter%(nth*pause)==0) getch();
flag=0;
}
else
continue;
}
printf("\n\n***************************finished******************\n\npress
any key to exit");
getch();
return 0;
}
/**************************
//code to store the first 1000000 primes to a binary file:
//place the code at the END of the file
FILE *fp=fopen("15000pri.bin","r+b");
if (!fp) printf("unable to open file");
fwrite(prime,sizeof(prime)*1000000,1,fp);
if (fclose(fp))
printf("\n\nfile not closed\n\n");
else
printf("\n\nfile successfully closed\n\n");
getch();
//code to get the first 1000000 primes from the binary file:
//place the code right after the declaration of *prime
FILE *fp=fopen("15000pri.bin","r+b");
if (!fp) printf("unable to open file");
fread(prime,sizeof(prime)*1000000,1,fp);
if (fclose(fp))
printf("\n\nfile not closed\n\n");
else
printf("\n\nfile successfully closed\n\n");
getch();
//code to check the chances of a number being a prime when number%pause==0
// pause is a variable entered by the user
double chance = 1.0;
for (long k=1;k<1000000;k++)
{
if (k%(pause/1000)==0) printf("%ld ",prime[k]);
chance -= double(1.0/double(prime[k])*chance);
if (k%pause==0) {printf("\t%f\t%ld",chance,k); getch(); }
}
//code to check primes within a range (maximum 1,000,000,000,000)
// you must have the first 1,000,000 primes loaded from a file or calculated
into the array (prime[1000000])
__int64 lower,upper;
printf("\n\nplease enter the lower range.\n\nplease select a number between
5 and 10^12 (1, and 12 zeroes) , or 0 to exit\n");
fflush(stdin);
scanf("%I64",&lower);
if (!nth) return 0;
printf("please enter the lower range.\n\nplease select a number between 5
and 10^12 (1, and 12 zeroes)");
fflush(stdin);
scanf("%I64",&upper);
if (upper<5 || upper>pow(10.0,12.0) || lower<5 || lower>pow(10.0,12.0))
{ printf("invalid range...exiting"); getch(); return 0; }
for (__int64 l=lower;l<=upper;l+=2,flag=1)
{
//check all primes that satisfy sqrt(i) >= prime
for (long m=2;prime[m]<long(sqrt(double(l))+1);m++)
{
if (l%prime[m]==0)//i is not a prime, go back to the first loop
{
flag=0;
break;
}
}
if (flag)
// now we know that i is a prime (since it's not divisible by any prime that
( root(i) >= prime )
{
printf("\t%I64",l);
flag=0;
}
else
continue;
}
//****************************/