Prime NUmbers

Largest Difference between two consecutive prime numbers

Page 1 of 1

3 Replies - 1415 Views - Last Post: 10 February 2009 - 08:10 PM Rate Topic: -----

#1 claretm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-February 09

Prime NUmbers

Posted 10 February 2009 - 04:50 AM

Hi All,

I have written a program to find all the prime numbers less than 1000000.
Can somebody help me to modify this code,so that I can get the biggest difference between two consecutive prime numbers?

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	const int N = 1000000;
	const int SQR_ROOT_N = (int) (sqrt ((double) N));
	bool prime[N + 1];
	
	prime[0] = false;  // 0 is not prime
	prime[1] = false;  // 1 is not prime
	
	for (int i = 2; i <= N; i++)
		 prime[i] = true;  // flag all numbers as prime initially
		 
	for (int i = 2; i <= SQR_ROOT_N; i++)
	{
		if (prime[i]) // no need for inner loop if i is not prime
		{
			for (int j = 2 * i; j <= N; j+=i)
				 prime[j] = false;  // j is a multiple of i, thus not prime.
		}
	}
	
	// display prime numbers
	cout << "Here are the prime numbers\n\n";
	for (int i = 2; i <= N; i++)
	{
		if (prime[i])
			cout << i << endl;
	}
	
	return 0;
}

MOD EDIT: Please :code:
Thanks, gabehabe :)

Is This A Good Question/Topic? 0
  • +

Replies To: Prime NUmbers

#2 headways_millennia  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 03-February 09

Re: Prime NUmbers

Posted 10 February 2009 - 08:04 AM

Hi,

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
const int N = 1000000;
const int SQR_ROOT_N = (int) (sqrt ((double) N));
bool prime[N + 1];



prime[0] = false; // 0 is not prime
prime[1] = false; // 1 is not prime

for (int i = 2; i <= N; i++)
prime[i] = true; // flag all numbers as prime initially

for (int i = 2; i <= SQR_ROOT_N; i++)
{
if (prime[i]) // no need for inner loop if i is not prime
{
for (int j = 2 * i; j <= N; j+=i)
prime[j] = false; // j is a multiple of i, thus not prime.
}
}

// display prime numbers
cout << "Here are the prime numbers\n\n";

int cnt = 0;			 // count gap size
int lgDiff[3] = {0}; // gap size, lower prime bound, upper prime bound

for (int i = 2; i <= N; i++)
{
	cnt++;
	if (prime[i]){
		cout << i << endl;
		if(cnt > lgDiff[0]){
			lgDiff[0] = cnt; // gap between this prime and last prime is larger than previous gap
			lgDiff[2] = i;   // get upper bound
		}
		cnt = 0;
		
	}
	
}

lgDiff[1] = (lgDiff[2] - lgDiff[0]); // grab lower bound prime

cout << '\n';
cout << lgDiff[0] << '\n';
cout << lgDiff[1] << '\n';
cout << lgDiff[2] << '\n';

int a;
cin >> a;

return 0;
}



I got:

492227 - 492113

... close eh?
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,669
  • Joined: 20-September 08

Re: Prime NUmbers

Posted 10 February 2009 - 07:58 PM

Quote

...I got: 492227 - 492113 ... close eh?


Nice code ... do you write it? Very cool approach :^:

Shalom,
David
Was This Post Helpful? 0
  • +
  • -

#4 headways_millennia  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 03-February 09

Re: Prime NUmbers

Posted 10 February 2009 - 08:10 PM

View PostDavid W, on 10 Feb, 2009 - 06:58 PM, said:

Quote

...I got: 492227 - 492113 ... close eh?


Nice code ... do you write it? Very cool approach :^:

Shalom,
David


Yes, I wrote it. Thank you David. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1