Sieve of Eratosthenes fstreams

I am trying to write a Sieve of Eratosthenes using files to append and retrieve primes. The purpose of this program is for me to gain an understanding in how to use files in a long list of saved numbers. Later I might want to use this program or another version of it to do Project Euler math problems.

The problem this program has with it is that it seems to save 3, 4, 5, 6, 7, 8, 9, and 10 as primes before it stops saving anything.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>
#include <cstdlib>

int main()
{

	// Open primeFile for writing.  Save the int 2.  Close the file.
	std::ofstream outPrimeFile{ "primeFile.txt", std::ios::out };
	if (!outPrimeFile)
	{
		std::cerr << "File could not be initially opened." << std::endl;
		exit(EXIT_FAILURE);
	}
	int prime = 2;
	outPrimeFile << prime;
	outPrimeFile.close();

	// Allow user to set prime limit.  Initialize primeCheck bool.
	int limit = 0;
	std::cout << "This program finds all primes to an indicated limit."
		<< std::endl << "Enter upper limit: ";
	std::cin >> limit;
	bool primeCheck = true;

	// Iterate i to limit.
	for (int i = 3; i <= limit; i++)
	{
		std::cout << "New for cycle.  i = " << i << std::endl;

		// Open primeFile for reading.
		std::ifstream readPrimeFile{ "primeFile.txt", std::ios::in };
		if (!readPrimeFile)
		{
			std::cerr << "File could not be opened for reading." << std::endl;
			exit(EXIT_FAILURE);
		}

		// Test i by itterating through the primes in primeFile.txt.
		primeCheck = true;
		while (readPrimeFile >> prime && primeCheck == true && i < (prime*prime))
		{
			std::cout << "New while cycle.  i = " << i << std::endl;

			if (i % prime == 0)
			{
				primeCheck = false;
			}
		}
		readPrimeFile.close();

		// If primeCheck survived, append i to primeFile.txt.
		if (primeCheck == true)
		{
			
			// Append i to prime.txt
			std::ofstream appPrimeFile{ "primeFile.txt", std::ios::app };
			if (!appPrimeFile)
			{
				std::cerr << "File could not be appended to." << std::endl;
				exit(EXIT_FAILURE);
			}
			std::cout << "Appending i with value " << i << std::endl;
			appPrimeFile << i;
			appPrimeFile.close();
		}

	}

	// Open primeFile.txt for printing a list of primes.
	std::ifstream readPrimeFile{ "primeFile.txt", std::ios::in };
	if (!readPrimeFile)
	{
		std::cerr << "File could not be opened for reading." << std::endl;
		exit(EXIT_FAILURE);
	}
	while (readPrimeFile >> prime)
	{
		std::cout << prime << std::endl;
	}


}
it looks like 'i' should be modified in the while loop?

while (readPrimeFile >> prime && primeCheck == true && i < (prime*prime))
{
std::cout << "New while cycle. i = " << i << std::endl;

if (i % prime == 0)
{
primeCheck = false;
}
}
Last edited on
That equality did need to be changed.

1
2
3
4
5
6
7
8
9
while (readPrimeFile >> prime && primeCheck == true && i < (prime*prime))
		{
			std::cout << "New while cycle. i = " << i << std::endl;

			if (i % prime == 0)
			{
				primeCheck = false;
			}
		}

to
1
2
3
4
5
6
7
8
9
while (readPrimeFile >> prime && primeCheck == true && i > (prime*prime))
		{
			std::cout << "New while cycle. i = " << i << std::endl;

			if (i % prime == 0)
			{
				primeCheck = false;
			}
		}


Thanks for that, but the program still saves 3, 4, 5, 6, 7, 8, 9, and 10 as primes, then stops saving anything.

The thinking here is that this while loop is going through all the primes saved within primeFile.txt. These primes are represented with int prime. i is the current number being checked against it. i needs to be checked against the entire sequence unless it is found to not be a prime or it is found to be less than the square of the latest prime in a sequence.

For example if we are testing 13 to be a prime we would check 2, then 3, then 5. Once we get to 5, we do not need to continue to 7. Knowing whether or not 13 is divisible by 5 is enough.
Last edited on
Actually I just found something interesting that might explain the problem if I only understood files better. I modified the code to display the values saved in prime.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>
#include <cstdlib>

int main()
{

	// Open primeFile for writing.  Save the int 2.  Close the file.
	std::ofstream outPrimeFile{ "primeFile.txt", std::ios::out };
	if (!outPrimeFile)
	{
		std::cerr << "File could not be initially opened." << std::endl;
		exit(EXIT_FAILURE);
	}
	int prime = 2;
	outPrimeFile << prime;
	outPrimeFile.close();

	// Allow user to set prime limit.  Initialize primeCheck bool.
	int limit = 0;
	std::cout << "This program finds all primes to an indicated limit."
		<< std::endl << "Enter upper limit: ";
	std::cin >> limit;
	bool primeCheck = true;

	// Iterate i to limit.
	for (int i = 3; i <= limit; i++)
	{
		std::cout << "New for cycle.  i = " << i << ".  prime = " << prime << std::endl;

		// Open primeFile for reading.
		std::ifstream readPrimeFile{ "primeFile.txt", std::ios::in };
		if (!readPrimeFile)
		{
			std::cerr << "File could not be opened for reading." << std::endl;
			exit(EXIT_FAILURE);
		}

		// Test i by itterating through the primes in primeFile.txt.
		primeCheck = true;
		while (readPrimeFile >> prime && primeCheck == true && i > (prime*prime))
		{
			std::cout << "New while cycle.  i = " << i << ".  prime = " << prime << std::endl;

			if (i % prime == 0)
			{
				primeCheck = false;
			}
		}
		readPrimeFile.close();

		// If primeCheck survived, append i to primeFile.txt.
		if (primeCheck == true)
		{
			
			// Append i to prime.txt
			std::ofstream appPrimeFile{ "primeFile.txt", std::ios::app };
			if (!appPrimeFile)
			{
				std::cerr << "File could not be appended to." << std::endl;
				exit(EXIT_FAILURE);
			}
			std::cout << "Appending i with value " << i << ".  prime = " << prime << std::endl;
			appPrimeFile << i;
			appPrimeFile.close();
		}

	}

	// Open primeFile.txt for printing a list of primes.
	std::ifstream readPrimeFile{ "primeFile.txt", std::ios::in };
	if (!readPrimeFile)
	{
		std::cerr << "File could not be opened for reading." << std::endl;
		exit(EXIT_FAILURE);
	}
	while (readPrimeFile >> prime)
	{
		std::cout << prime << std::endl;
	}


}


output:
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
This program finds all primes to an indicated limit.
Enter upper limit: 15
New for cycle.  i = 3.  prime = 2
Appending i with value 3.  prime = 2
New for cycle.  i = 4.  prime = 2
Appending i with value 4.  prime = 23
New for cycle.  i = 5.  prime = 23
Appending i with value 5.  prime = 234
New for cycle.  i = 6.  prime = 234
Appending i with value 6.  prime = 2345
New for cycle.  i = 7.  prime = 2345
Appending i with value 7.  prime = 23456
New for cycle.  i = 8.  prime = 23456
New while cycle.  i = 8.  prime = 234567
Appending i with value 8.  prime = 234567
New for cycle.  i = 9.  prime = 234567
Appending i with value 9.  prime = 2345678
New for cycle.  i = 10.  prime = 2345678
Appending i with value 10.  prime = 23456789
New for cycle.  i = 11.  prime = 23456789
Appending i with value 11.  prime = 23456789
New for cycle.  i = 12.  prime = 23456789
Appending i with value 12.  prime = 23456789
New for cycle.  i = 13.  prime = 23456789
Appending i with value 13.  prime = 23456789
New for cycle.  i = 14.  prime = 23456789
Appending i with value 14.  prime = 23456789
New for cycle.  i = 15.  prime = 23456789
Appending i with value 15.  prime = 23456789
Press any key to continue . . .
Topic archived. No new replies allowed.