Not Yet Answered # C Program to find the largest difference between two prime numbers.

WaltP 2,905 Discussion Starter claretm csurfer 422 WaltP 2,905 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

Start by writing a program to determine the prime numbers less than 1,000,000. Then modify the program to test the differences.

0

Thanks for the reply.

If this gives all the prime numbers less than 1000000,how can i modify this to get the largest 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;
}
```

*Edited 3 Years Ago by Reverend Jim*: Fixed formatting

0

Thanks for the reply.

If this gives all the prime numbers less than 1000000,how can i modify this to get the largest difference between two consecutive prime numbers?

Well you need to know one thing very clearly that your program is not generating prime numbers up to 1000000 as you have mentioned. Your program just generates prime numbers till 1000 i,e sqrt(1000000).

And to find the max this is what you need to do:

```
int i=2,j=3,max=0;
// Setting two variables i and j to first two prime numbers
// Setting max difference to 0 to start with
while( j <= N ) // To ensure higher prime doesn't go beyond N
{
if( (j-i) > max ) //If this difference > Present max value
max=j-i; //Assign this difference to max
i=j; //Shifting i's value to next prime i,e j
j++; //Increment j by 1 so that its value changes
while( prime[j] != true && j < N ) j++;
// Move j's value till it reaches next prime or N
}
printf("Maximum Difference between consecutive prime numbers is %d",max);
```

Hope you can get the above code.(Its quite cumbersome) This will definitely do your work.

0

Hope you can get the above code.(Its quite cumbersome) This will definitely do your work.

Hope you get a good grade for the program, **csurfer**.**claretm** certainly doesn't deserve it.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

the function that I created to find the ...

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...