Prime numbers between and upper and lower bound

Back story: I have a test tomorrow and my teacher usually makes us type a code from scratch. This time, he typed a code during class while we watched on the projector, then printed the code out for us to study and told us this would be the code we need to type for the test and to know it exactly. The code is suppose to take an lower bound and upper bound and display all the prime numbers in between them.
Well, about an hour ago I typed the program up multiple times, exactly as it is printed on the paper, and the output is incorrect. I don't know if i keep repeatedly typing it wrong, or if he typed it up too fast in class and messed up. here's the 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
#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

int main()
{
	unsigned __int64 lb, ub, i, j, sqrNum;
	bool isPrime = true;
	
	do
	{
		cout << "Enter a lower bound >= 3: ";
		cin >> lb;
		cout << "Enter an upper bound less than the lower bound: ";
		cin >> ub;
	}while(lb < 3 || ub < lb);
	
	if(lb % 2 == 0)
		lb++;
	if(ub % 2 == 0)
		ub--;
	
	for(i = lb; i <= ub; i += 2)
	{
		sqrNum = sqrt(i);
		isPrime = true;
		for(j = 3; j < sqrNum; j += 2)
		{
			if(i % j == 0)
			{
				isPrime = false;
				break;
			}
		}
		if(isPrime)
			cout << i << " is prime" << endl;
	}
}
The fastest way to find prime numbers.
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
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

bool isPrime(int n)
{
	static bool *testPrime = NULL;
	static int testSize = 0;

	int i;
	int k;

	if(n > testSize) 
	{
		if(testPrime) delete [] testPrime;
		testPrime = new bool[n + 1];
		testSize = n;

		for(i = 0; i <= testSize; i++) testPrime[i] = false;
		testPrime[1] = true;

		for(i = 1; i <= (testSize + 1) / 2; i++)
		{
			if(testPrime[i]) continue;

			k = i + i;
			while(k <= n)
			{
				testPrime[k] = true;
				k += i;
			}		
		}
	}

	if(testPrime[n]) return false; else return true;
}

int main()
{
	int i;
	unsigned int lowerBound, upperBound;
	
	do
	{
		cout << "- Enter a lower bound >= 3 : "; cin >> lowerBound;
	} while(lowerBound < 3);

	do
	{
		cout << "Enter an upper bound greater the lower bound : "; cin >> upperBound;
	} while(upperBound <= lowerBound);

	for(i = lowerBound; i <= upperBound; i++)
	{
		if(isPrime(i)) cout << i << " is prime" << endl;
	}

	cin.clear();
	cin.get();
	return 0;
}


- Enter a lower bound >= 3 : 20
Enter an upper bound greater the lower bound : 100
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime


http://cpp.sh/7zrl
http://www.cplusplus.com/forum/beginner/201756/#msg961339
see, the thing is, I am new to c++, and most of the code you just posted is different from what I am use to seeing in my class, sorry
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
#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

bool isPrime(int i)
{
	int k, N;
	if(i <= 1) return false; else N = sqrt(i);
	for(k = 2; k <= N; k++) if(i % k == 0) return false; return true;
}

int main()
{
	int i;
	unsigned int lowerBound, upperBound;
	
	do
	{
		cout << "- Enter a lower bound >= 3 : "; cin >> lowerBound;
	} while(lowerBound < 3);

	do
	{
		cout << "- Enter an upper bound greater the lower bound : "; cin >> upperBound;
	} while(upperBound <= lowerBound);

	for(i = lowerBound; i <= upperBound; i++)
	{
		if(isPrime(i)) cout << i << " is prime" << endl;
	}

	cin.clear();
	cin.get();
	return 0;
}


- Enter a lower bound >= 3 : 20
- Enter an upper bound greater the lower bound : 100
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime

http://cpp.sh/7q3nh
Last edited on
Topic archived. No new replies allowed.