Hello guys, I know it's been asked a lot about it here, but that's what I should do (though I'm not quite sure what the teacher wants, is n should be 100, 1000 and further, or use prime numbers. The "first draft" is due this Sunday, the "final draft" is due next week. I'm a complete noob in programming, but really do want to learn it. So, here's my homework (2 parts):
1. Write a program that computes and displays the first 100 prime numbers. Be sure to design your program to include a function (which you must write yourself) called isPrime() whose specification is a follows:
bool isPrime (long long n);
// Returns true if n is a prime, else false
2. Write program that examines the conjecture above as follows:
Write a loop that sets a variable n to 100, 1000, 10,000, 100,000, 1,000,000, 10,000,000 in turn. For each of the specified values of n, your program should compute the sum of the logarithms of all the primes from 2 to n, and display the sum of the logs of the primes, the number n, and the ratio of these two quantities computed to 5 decimal places.
The results should be displayed in a table with 6 rows (for each of the 6 values of n) and 3 columns the sum of the relevant logarithms, n, and the ratio). The correct results are given later in this document.
You should be able to make some changes to your solution to part 1 to solve part 2.
The correct output should look like this:
Sum of logs n Ratio
83.72839 100 0.83728
956.24527 1000 0.95625
9895.99138 10000 0.98960
99685.38927 100000 0.99685
998484.17503 1000000 0.99848
9995179.31786 10000000 0.99952
Press any key to quit:
My ideas:
My idea is that this code somehow will help me with the part 1. Not sure though. I need to give only e^n part, not the primary
#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;
}
return 0;
}