prime number generator

closed account (98qGz8AR)
i'm supposed to write a program that
1) finds all prime numbers from 2 to 10,000,000 in iterations powers of 10 (10^2, 10^3, etc.)
2) outputs the sum of the logarithms of all those primes and
3) outputs the ratio of the n value and the current running sum of logs for each iteration.

this is what i have so far, we're supposed to have a boolean function as the prime tester, but this just isn't working. i'm getting all kinds of weird output and i don't know what's wrong. any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

bool isPrime(int q);

int main (){
    int e = 2; // e is for exponent. it goes from 2 to 7.
    int n; // n is the assigned variable = 10^e
    int p = 2; // p is for prime.  it is the last prime number found.
    float sum; // sum is the running total.

    do{
    	n = pow(10, e);

    	if(isPrime(p) == true)
    		sum += log(p);

		cout << fixed << setprecision(5) << sum;
		cout << "   ";
		cout << n;
		cout << "   ";
		cout << fixed << setprecision(5) << sum/n;
		cout <<"\n";

		e++;
		p++;
    
       }while(e < 8 && p < n);
}

bool isPrime(int q)
{
    int f; //f is the number each potential prime is divided by

	for(f=2;f<q;f++)
    {
        if(q%f == 0)
        	return false;
        else
		return true;
    }
}
In regards to your 'all kinds of weird output': Double check your variables, you may catch an issue somewhere.

Edit: (To clarify - This isn't just a random silly comment, I actually do see an issue. :) )
Last edited on
think carefully about your logic between Lines 39 and 42.

in particular, would the for() loop on Line 37 ever run to completion?

there may be other problems, but that's what I see, at a glance.
closed account (98qGz8AR)
cool, thanks guys/ladies!
i had been staring at it for too long and had gotten sloppy.
i think i'm close to getting the prime checker loop to work now.
closed account (98qGz8AR)
now i got the bool function to work properly, and i think it's getting called to right, but i'm still getting negative numbers as output. here is the revised code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <math.h>
#include <iomanip>
#include <stdio.h>
using namespace std;

bool isPrime(int candidate);

int main (){
    int e = 2; // e is for exponent. it goes from 2 to 7.
    int n; // n is the assigned variable = 10^e
    int p = 2;// p is for prime.  it is the last prime number found.
    double sum; // sum is the running total.

    do{
    	n = pow(10, e);

    	if(isPrime(p) == true)
    		sum += log(p);
    	if(isPrime(p) == false)
    		p++;

		cout << fixed << setprecision(5) << sum;
		cout << "   ";
		cout << n;
		cout << "   ";
		cout << fixed << setprecision(5) << sum/n;
		cout <<"\n";

		e++;
		p++;

    }while(e < 8 && p < n);
}
bool isPrime(int candidate)
{
	int ping = 0; // a prime number should have no more than one divisor greater than 1
	int factor = 2; // f is the factor the potential prime is divided by
    int remainder; //z is the remainder of the division of the prime and the factor

    remainder = candidate%factor;
			while(candidate > factor){
			if(remainder != 0)
				factor++;
			if(remainder == 0){
				ping++;
				factor++;
			}
			}
			if(ping <= 1)
				return true;
			if(ping > 1)
				return false;
}

i thought that by assigning value "2" to p inside int main and making it a do...while loop would let me get 2 into the running total as the first prime, but now i think it's screwing it up.
Topic archived. No new replies allowed.