C++ prime number program help
Below is my code for printing prime numbers up to a given input by user. However, my calculations are not working correctly and my output is printing twice. Any idea? :sad:
#include
#include
using namespace std;
void prime_num(int);
int main()
{
cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;
prime_num(num);
}
void prime_num( int num)
{
int check_prime = 0;
for ( int i = 0; i <= num; i++)
{
check_prime = 1;
for ( int j = 2; j <= i/2; j++)
{
if ( i % j == 0 )
check_prime = 0;
if ( check_prime != 0 )
{
cout << i << endl;
}
}
}
}
djbsabkcb
Junior Poster in Training
92 posts since Jun 2005
Reputation Points: 14
Solved Threads: 0
It is printing the number multiple times, becuse of:
for ( int j = 2; j <= i/2; j++)
{
if ( i % j == 0 )
{
check_prime = 0;
}
if ( check_prime != 0 )
{
cout << i << endl;
}
}
here you are printing out the number when check prime != 0.
but untill i%j == 0 the program are going to ouput that the number is prime, even when it aint.
this is bad design, and I think you need to rewrite your function.
check out: http://www.daniweb.com/techtalkforums/thread27064-prime.html
zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
I see what you are saying but that still isn't right because 1 is not a prime number
djbsabkcb
Junior Poster in Training
92 posts since Jun 2005
Reputation Points: 14
Solved Threads: 0
Here's what I went with. Note you want to do a break if you find out it's divisible as there is no point in checking all the numbers beyond it for divisibility..
#include <iostream>
using namespace std;
void prime_num(int);
int main()
{
cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;
prime_num(num);
}
void prime_num( int num)
{
bool isPrime=true;
for ( int i = 0; i <= num; i++)
{
for ( int j = 2; j <= num; j++)
{
if ( i!=j && i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
cout <<"Prime:"<< i << endl;
}
isPrime=true;
}
}
Enter a number and I will generate the prime numbers up to that number: 100
Prime:1
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Note you can check your results here: http://www.utm.edu/research/primes/lists/small/1000.txt
I see what you are saying but that still isn't right because 1 is not a prime number
djbsabkcb
Junior Poster in Training
92 posts since Jun 2005
Reputation Points: 14
Solved Threads: 0
So Picky ;). As you wish:
#include <iostream>
using namespace std;
void prime_num(int);
int main()
{
cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;
prime_num(num);
}
void prime_num( int num)
{
bool isPrime=true;
for ( int i = 2; i <= num; i++)
{
for ( int j = 2; j <i; j++)
{
if ( i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
cout <<"Prime:"<< i << endl;
}
isPrime=true;
}
}
Enter a number and I will generate the prime numbers up to that number: 150
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Prime:101
Prime:103
Prime:107
Prime:109
Prime:113
Prime:127
Prime:131
Prime:137
Prime:139
Prime:149
thanks, that is correct.
its funny you can sit in front of this screen and try to debug for hours and once you figure it out it was something small to begin with. Very frustrating.
djbsabkcb
Junior Poster in Training
92 posts since Jun 2005
Reputation Points: 14
Solved Threads: 0
You bumped a 2 year-old thread to say that? ^_^
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
So I've been testing these algorithms, and really, they're SO slow >.<, I myself made a prime number calculator, when I needed to regenerate prime numbers for some RSA encryption, however, I used this program; It's able to find all the prime numbers from 3-50.000.000 in about 2½second.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TYPE register unsigned int
int main()
{
TYPE n, p=1, i=0, j, count=1;
register bool done;
register clock_t Time;
printf("250.000.000 = ~500mb ram..\n\n Calculate primes up to: ");
scanf("%d", &n);
printf("\nCalculating...\n");
n /= 2;
TYPE *buffer = (unsigned int*)malloc(n * sizeof(unsigned int));
if (buffer == 0)
{
printf("Could not allocate RAM!\n\n");
return 0;
}
Time = clock();
for (i=0; i != n; i++)
{
buffer[i] = i*2+3;
}
i = 0;
while (1)
{
done = true;
for (j=i; j < n; j++)
{
if (buffer[j] > p)
{
p = buffer[j];
done = false;
break;
}
}
i = j+1;
if (!done)
{
while (j+p < n) buffer[j+=p] = 0;
}
else
{
break;
}
}
for (unsigned int i=0; i != n; i++)
{
if (buffer[i])
{
count++;
}
}
printf("\n...Calculation done :)\n %d Primenumbers found in %1.3f seconds!\n", count, (clock()-Time)/(float)CLOCKS_PER_SEC);
free(buffer);
return 0;
}
And it's just a based upon the simple (and slow) Sieve of Eratosthenes algorithm! If you're to write anything useful, you should use the Sieve of Atkin algorithm!
Skeen
Junior Poster in Training
77 posts since Nov 2009
Reputation Points: 10
Solved Threads: 2