What is the code to the assignment? Please provide the code for problem 22.8 and
ID: 3807384 • Letter: W
Question
What is the code to the assignment? Please provide the code for problem 22.8 and 22.10.
22.8 : The problem states to find all prime numbers up to 10,000,000,000.
Requirements:
-The code should store the prime numbers in a binary data file named PrimeNumbers.dat. When a new prime number is found it should be appended to the file.
-To find whether a new number is prime, the program should load the prime numbers from the file to an array of the long type of size 10,000. If no number in the array is a divisor for the new number, continue to read the next 10000 prime numbers from the data file, until a divisor is found or all the numbers in the file are read. If no divisor is found the new number is prime.
- Since this program takes a long time to finish, you should run it as a batch job from a UNIX machine. If the machine is shut down and rebooted, your program should resume by using the prime numbers stored in a binary data file rather than start over from scratch.
In 22.8 the program is supposed to find the number of prime numbers that are less than or equal to 10, 100, 1,000, 10,000, 100,000, 1,000,000, 10,000,000, 1000,000,000, 1,000,000,000, and 10,000,000,000. And this program should read the data from PrimeNumbers.dat.
Explanation / Answer
// C++ program to print all primes smaller than or equal to 10,000,000,000.
#include <bits/stdc++.h>
using namespace std;
#include <fstream>
void findprime(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[10,000,000,000+1];
memset(prime, true, sizeof(prime));
for (int p=2; p*p<=10,000,000,000; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i=p*2; i<=10,000,000,000; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int p=2; p<=n10,000,000,000; p++)
if (prime[p])
cout << p << " ";
int append()
{
std::ofstream outfile;
PrimeNumbers.open("prime[p]", std::ios_base::app);
return 0;
}
}
// Driver Program to test above function
int main()
{
int n = 30;
cout << "Following are the prime numbers smaller "
<< " than or equal to " << 10,000,000,000 << endl;
findprime(n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.