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;
    }

Instead of:

for ( int j = 2; j <= num; j++){

use

for ( int j = 2; j <= sqrt(num); j++){

This will shorten the run time by a large magnitude.

Regards,
Ubaidullah Nubar.

But how do I make it write 100 first prime numbers? Not until some number, but exactly 100 numbers. And also, what should I do in the second question? Should I do it so that it writes e^100, e^100, etc? Or e^1, e^2, e^3, e^5, and all prime numbers?

P.S. Looks like I start getting it - the program should find a sum of logarithms of all primes from task 1, such as e^2+e^3+...+e^n

Instead of:

for ( int j = 2; j <= num; j++){

use

for ( int j = 2; j <= sqrt(num); j++){

This will shorten the run time by a large magnitude.

Regards,
Ubaidullah Nubar.

No it won't, Every time you go through the loop, sqrt(num) will be recalculated. If you want to shorten the run time, use:

sq = sqrt(num);
for (j = 2; j <= sq; j++){
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>

using namespace std;

int IsPrimeNumber(int i);
int main()
{
		int i, primesFound;
  
		i			= 1;
		primesFound = 0;

	  while(primesFound < 100)
	  {
		i++;
		if(IsPrimeNumber(i) == 1)
		{
			cout << i << " ";
			primesFound++;
		}
	  }
		string line;

    cout << "Please press <Enter> to continue: ";
    getline(cin, line);

	i = 1;
	primesFound = 0;

	double logSum = 0.0;
	double ratio = 0.0;


	  while(primesFound < 100)
	  {
		i++;
		if(IsPrimeNumber(i) == 1)
		{
			double long i2 = i + 0;
			logSum = logSum + log(i2);
			primesFound++;
		}
	  }

	  ratio = logSum/100;

	  cout <<"\nSum of logs    n     Ratio" << endl;
	  cout << setprecision(5) << logSum << endl; 
	  // cout << setprecision(5) <<logSum << primesFound << ratio << endl;
	  

	scanf("\nPress a key to exit");

	return 0;
}

 int IsPrimeNumber(int i)
  {
		int isPrime = 1;

		for(int j = 2; j <= (i/2); j++)
		{
			if((i%j) == 0) 
			{
				isPrime = 0;
			}
		}

		return isPrime;
  }

laurenmarierich, your code works, but it's kind of messy - I think it'd be better if it was 107, It was divided as 1 and on a new line 07. Also, it has to contain

bool isPrime (long long n);

. Also, what function should I add so that it would calculate logarithms?

sorry that i didn't clean it up before i posted it. my calculation is wrong for the sum of logs. i'll let you know if i figure it out.

Any news? any help? ANYTHING?

Acutally, it's due today. So, what do I write to find the "e^isPrime"? And how do I set the precision point to find the result of
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
??????????????????????????????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.