Prime number finder,finds 6 more primes than it should [closed]
up vote
-1
down vote
favorite
I'm new to programming and i cant seem to find the mistake that makes my program find 6 more primes than it should.The correct answer for the given range is 904533.
In addition,suggested run time is around 5 seconds while mine is around 8.
Hope someone could help me, thank you in advance.
#define MINNUM 3990000000
#define MAXNUM 4010000000
#include <stdio.h>
int main()
{
unsigned int r,j,i,checker,k,primecount=0,d,d_save; //
unsigned int a;
unsigned long long res=1;
printf("Checking range [3990000000,4010000000] for primes...");
for (k=MINNUM+1;k<=MAXNUM-2;k+=2)
{
checker = 0;
if(k % 3 != 0)
{
d = k - 1; // Reset variables
res = 1;
r = 0;
//Create (2^r)*d= n-1
while(d%2==0){
r++;
d/=2;
}
//printf("%u can be written as : (2^%d)*%llun",k-1,r,d);
d_save=d; //saves d for each j loop
do{
for(j=1;j<=3;j++)
{
d=d_save;
res=1;
if (j==1) a=2;
if (j==2) a=7;
if (j==3) a=61;
//Calculate a^d mod k
while (d>0)
{
// When y is odd
if (d & 1)
res = (res*a) % k;
// When y is even
d = d>>1; //Same as y = y/2
a = (a*a) % k;
}
//Miller Rabin's c
if (res == 1 || res == k-1) { checker=1; continue;}
while (r!=0 )
{
res = (res * res) % k;
if (res == 1) { checker=0; break;}
if (res == k-1) { checker=1; break;}
r--;
}
}
}
while (checker==1);
//printf("check %u %dn",k,checker);
if (checker==1) primecount++;
}
}
printf("primes are %u",primecount);
}
c primes
closed as off-topic by Simon Forsberg♦ Nov 16 at 23:01
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Simon Forsberg
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
I'm new to programming and i cant seem to find the mistake that makes my program find 6 more primes than it should.The correct answer for the given range is 904533.
In addition,suggested run time is around 5 seconds while mine is around 8.
Hope someone could help me, thank you in advance.
#define MINNUM 3990000000
#define MAXNUM 4010000000
#include <stdio.h>
int main()
{
unsigned int r,j,i,checker,k,primecount=0,d,d_save; //
unsigned int a;
unsigned long long res=1;
printf("Checking range [3990000000,4010000000] for primes...");
for (k=MINNUM+1;k<=MAXNUM-2;k+=2)
{
checker = 0;
if(k % 3 != 0)
{
d = k - 1; // Reset variables
res = 1;
r = 0;
//Create (2^r)*d= n-1
while(d%2==0){
r++;
d/=2;
}
//printf("%u can be written as : (2^%d)*%llun",k-1,r,d);
d_save=d; //saves d for each j loop
do{
for(j=1;j<=3;j++)
{
d=d_save;
res=1;
if (j==1) a=2;
if (j==2) a=7;
if (j==3) a=61;
//Calculate a^d mod k
while (d>0)
{
// When y is odd
if (d & 1)
res = (res*a) % k;
// When y is even
d = d>>1; //Same as y = y/2
a = (a*a) % k;
}
//Miller Rabin's c
if (res == 1 || res == k-1) { checker=1; continue;}
while (r!=0 )
{
res = (res * res) % k;
if (res == 1) { checker=0; break;}
if (res == k-1) { checker=1; break;}
r--;
}
}
}
while (checker==1);
//printf("check %u %dn",k,checker);
if (checker==1) primecount++;
}
}
printf("primes are %u",primecount);
}
c primes
closed as off-topic by Simon Forsberg♦ Nov 16 at 23:01
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Simon Forsberg
If this question can be reworded to fit the rules in the help center, please edit the question.
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Simon Forsberg♦
Nov 16 at 23:01
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm new to programming and i cant seem to find the mistake that makes my program find 6 more primes than it should.The correct answer for the given range is 904533.
In addition,suggested run time is around 5 seconds while mine is around 8.
Hope someone could help me, thank you in advance.
#define MINNUM 3990000000
#define MAXNUM 4010000000
#include <stdio.h>
int main()
{
unsigned int r,j,i,checker,k,primecount=0,d,d_save; //
unsigned int a;
unsigned long long res=1;
printf("Checking range [3990000000,4010000000] for primes...");
for (k=MINNUM+1;k<=MAXNUM-2;k+=2)
{
checker = 0;
if(k % 3 != 0)
{
d = k - 1; // Reset variables
res = 1;
r = 0;
//Create (2^r)*d= n-1
while(d%2==0){
r++;
d/=2;
}
//printf("%u can be written as : (2^%d)*%llun",k-1,r,d);
d_save=d; //saves d for each j loop
do{
for(j=1;j<=3;j++)
{
d=d_save;
res=1;
if (j==1) a=2;
if (j==2) a=7;
if (j==3) a=61;
//Calculate a^d mod k
while (d>0)
{
// When y is odd
if (d & 1)
res = (res*a) % k;
// When y is even
d = d>>1; //Same as y = y/2
a = (a*a) % k;
}
//Miller Rabin's c
if (res == 1 || res == k-1) { checker=1; continue;}
while (r!=0 )
{
res = (res * res) % k;
if (res == 1) { checker=0; break;}
if (res == k-1) { checker=1; break;}
r--;
}
}
}
while (checker==1);
//printf("check %u %dn",k,checker);
if (checker==1) primecount++;
}
}
printf("primes are %u",primecount);
}
c primes
I'm new to programming and i cant seem to find the mistake that makes my program find 6 more primes than it should.The correct answer for the given range is 904533.
In addition,suggested run time is around 5 seconds while mine is around 8.
Hope someone could help me, thank you in advance.
#define MINNUM 3990000000
#define MAXNUM 4010000000
#include <stdio.h>
int main()
{
unsigned int r,j,i,checker,k,primecount=0,d,d_save; //
unsigned int a;
unsigned long long res=1;
printf("Checking range [3990000000,4010000000] for primes...");
for (k=MINNUM+1;k<=MAXNUM-2;k+=2)
{
checker = 0;
if(k % 3 != 0)
{
d = k - 1; // Reset variables
res = 1;
r = 0;
//Create (2^r)*d= n-1
while(d%2==0){
r++;
d/=2;
}
//printf("%u can be written as : (2^%d)*%llun",k-1,r,d);
d_save=d; //saves d for each j loop
do{
for(j=1;j<=3;j++)
{
d=d_save;
res=1;
if (j==1) a=2;
if (j==2) a=7;
if (j==3) a=61;
//Calculate a^d mod k
while (d>0)
{
// When y is odd
if (d & 1)
res = (res*a) % k;
// When y is even
d = d>>1; //Same as y = y/2
a = (a*a) % k;
}
//Miller Rabin's c
if (res == 1 || res == k-1) { checker=1; continue;}
while (r!=0 )
{
res = (res * res) % k;
if (res == 1) { checker=0; break;}
if (res == k-1) { checker=1; break;}
r--;
}
}
}
while (checker==1);
//printf("check %u %dn",k,checker);
if (checker==1) primecount++;
}
}
printf("primes are %u",primecount);
}
c primes
c primes
asked Nov 16 at 22:44
Δημήτρης Σπανάκης
92
92
closed as off-topic by Simon Forsberg♦ Nov 16 at 23:01
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Simon Forsberg
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Simon Forsberg♦ Nov 16 at 23:01
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Simon Forsberg
If this question can be reworded to fit the rules in the help center, please edit the question.
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Simon Forsberg♦
Nov 16 at 23:01
add a comment |
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Simon Forsberg♦
Nov 16 at 23:01
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Simon Forsberg♦
Nov 16 at 23:01
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Simon Forsberg♦
Nov 16 at 23:01
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Simon Forsberg♦
Nov 16 at 23:01