# Prime numbers between two integers

Page 1 of 1

## 14 Replies - 6409 Views - Last Post: 10 May 2010 - 07:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=172845&amp;s=3fc83f4efcff22f0dd5ccec0bdc69af9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

# Prime numbers between two integers

Posted 09 May 2010 - 08:35 PM

The task is simple really. Read two integers from a file in this format:
10 20
30 40
53 60
60 101

then take those two integers and output all prime numbers including but not less than or exceeding both integers in this format:
11 13 17 19
31 37
53 59
61 67 71 73 79 83 89 97 101

When I run this code it doesn't do anything. Console comes up but it just sits there like its thinking. Best I can figure is that im not reading anything in because my input format is wrong. What do you think?
```ifstream inFile3;
int number1, number2;
inFile3.open("primeranges.txt");
if (inFile3.fail())
{
cerr << "File could not be read.\n";
return 3; // Return a unique error code.
}
cout << endl;
inFile3 >> number1 >> number2;

while (number1 != number2)
{
if (number1 % 2 != 0)
{
cout << number1;
++number1;
}
}
inFile3.close();

```

Is This A Good Question/Topic? 0

## Replies To: Prime numbers between two integers

### #2 KYA

• g++ jameson.cpp -o beverage

Reputation: 3145
• Posts: 19,185
• Joined: 14-September 07

## Re: Prime numbers between two integers

Posted 09 May 2010 - 08:48 PM

You need a better prime check, 2 isn't the only divisor.

As for the hang, that's because the increment is inside the conditional (thus only increments [in yours] if it is divisible by two), fixed:

```#include <iostream>
#include <fstream>
using namespace std;

//check other divisible stuff, not just 2
bool isPrime(int num){
for(int i = 2; i*i <= num; i++){
if (num%i==0) return false;
}
return true;
}
int main(){
ifstream inFile3;
int number1, number2;
inFile3.open("primeranges.txt");
if (inFile3.fail())
{
cerr << "File could not be read.\n";
return 3; // Return a unique error code.
}
cout << endl;
inFile3 >> number1 >> number2;

cout << number1 << " " << number2 << endl;
while (number1 <= number2)
{
if (isPrime(number1)){
cout << number1 << " ";
}
++number1; //move me outside
}
inFile3.close();

return 0;
}

```

### #3 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 08:51 PM

oh ok lemme see if I can figure it out now.lol.

### #4 noclaf

Reputation: 26
• Posts: 94
• Joined: 18-April 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 08:51 PM

. number1 = 10, number2 = 20
. is (number1 == number2) ? no. jump inside the while loop.
. is (number1 % 2 != 0) ? no. skip the if clause.
. is (number1 == number2) ? no. jump inside the while loop.
. is (number1 % 2 != 0) ? no. skip the if clause.
. is (number1 == number2) ? no. jump inside the while loop.
. is (number1 % 2 != 0) ? no. skip the if clause.
. is (number1 == number2) ? no. jump inside the while loop.
. is (number1 % 2 != 0) ? no. skip the if clause.

```file.open("numbers.txt");
while (! file.eof) {
file >> min >> max;
for (int i = min; i != max; i++) {
if (isprime(i)) cout << i << " ";
}
cout << endl;
}
```

This is actually a good assignment, because it gives you the opportunity to tackle several small problems. Small problems should use small solutions.

edit: looks like KYA beat me to it...

This post has been edited by noclaf: 09 May 2010 - 08:51 PM

### #5 sarmanu

• D.I.C Lover

Reputation: 966
• Posts: 2,362
• Joined: 04-December 09

## Re: Prime numbers between two integers

Posted 09 May 2010 - 08:53 PM

Use a while loop, to extract the data. Why? Because if you simply use inFile3 >> number1 >> number2; it will extract only the first two numbers from your file. So, try this:
```while (inFile3 >> number1 >> number2)
{
// You have number1 and number2 here. At every
// iteration, two numbers from the file are extracted.
// At first iteration, number1 is 10 and number2 is 20.
// At second, number1 is 30 and number2 is 40, and so on ...
}

```

For prime checker, use KYA's function.

This post has been edited by sarmanu: 09 May 2010 - 08:56 PM

### #6 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 08:54 PM

after reviewing my code and what you have said I think a FOR loop would better suit this purpose.

### #7 noclaf

Reputation: 26
• Posts: 94
• Joined: 18-April 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 09:00 PM

sarmanu, on 09 May 2010 - 09:53 PM, said:

Sure, but like i stated, it's pseudo code. I'm not into spoonfeeding people who're obviously doing homework.

Aside: KYA's isPrime checker needs improvement. Why start at 2 and increment 1 at a time? Check the edge case (even), start at 3, and increment by 2 at a time:

```int isPrime(int num) {
if (num % 2 == 0)
return 0;

int i;
for (i = 3; i * i < num; i += 2)
if (num % i == 0)
return 0;

return 1;
}
```

This post has been edited by noclaf: 09 May 2010 - 09:05 PM

### #8 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 09:05 PM

lol you aren't spoonfeeding me. I do appreciate the help and yes it is homework. However completion of the homework takes second to understanding the concepts. If you do not want to teach or help do not reply. Thanks.

I didnt refresh between your post and KYA so when I posted my previous his was the only comment on the page. I wasn't trolling for more info KYA's response was sufficient to get me headed in the right direction. Thanks again though

This post has been edited by GCheek: 09 May 2010 - 09:08 PM

### #9 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 09:12 PM

sarmanu, on 09 May 2010 - 08:53 PM, said:

Use a while loop, to extract the data. Why? Because if you simply use inFile3 >> number1 >> number2; it will extract only the first two numbers from your file. So, try this:
```while (inFile3 >> number1 >> number2)
{
// You have number1 and number2 here. At every
// iteration, two numbers from the file are extracted.
// At first iteration, number1 is 10 and number2 is 20.
// At second, number1 is 30 and number2 is 40, and so on ...
}

```

For prime checker, use KYA's function.

thanks for that I figured it would read in the first two numbers but I wasnt sure it would read in the second two. I figured I would cross that bridge when I got there.

GCheek, on 09 May 2010 - 08:54 PM, said:

after reviewing my code and what you have said I think a FOR loop would better suit this purpose.

This was a response to KYA's initial post on this thread.

### #10 KYA

• g++ jameson.cpp -o beverage

Reputation: 3145
• Posts: 19,185
• Joined: 14-September 07

## Re: Prime numbers between two integers

Posted 09 May 2010 - 09:21 PM

GCheek, on 09 May 2010 - 08:54 PM, said:

after reviewing my code and what you have said I think a FOR loop would better suit this purpose.

Only if you know how many lines are going to be in the file time ahead of time. Otherwise a while loop is the way to go here.

### #11 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 09:23 PM

This all makes sense thank you KYA for taking the time to explain these concepts to me. This is how I learn best.

### #12 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 10:26 PM

Basically if I divide all numbers between 1 and min and keep use count to keep track of all of those numbers that divide without a remainder. If count = 2 meaning the number can ONLY be divided by 1 and itself then isPrime is true. Then cout that number to console because it is prime, increment min number and repeat this process until all numbers between minNum and maxNum have been evaluated. Then read in my second set of numbers and repeat the entire process I should be good.lol.

### #13 GCheek

Reputation: 6
• Posts: 92
• Joined: 31-January 10

## Re: Prime numbers between two integers

Posted 09 May 2010 - 10:59 PM

so this is where I have gotten. I'm pretty sure the strange output I am getting has something to do with the way I am reading in my numbers from file?
```//Because I am using integers in my data file with no decimals I am using variables of type int.
//I also set my divisor value to 0 because I am going to incement this.
int minNum, maxNum, divisor=1, count = 0;
//Here I open the stream and tell it what file to draw its data from.
inFile3.open("primeranges.txt");
//This statement will alert the user if the file could not be found or simply could not be opened.
if (inFile3.fail())
{
cerr << "File could not be read.\n";
return 3; // This error code will be displayed if the file is unable to be read or opened.
}
else
{
//This loop will execute so long as the file is able to be read.
while (inFile3)
{
//If the file can be read the first two integers will be read in as minNum and maxNum.
inFile3 >> minNum >> maxNum;
//As long as minNum is less than or equal to maxNum this loop will execute.
while(minNum <= maxNum)
{
//If you can divide minNum by the divisor and get zero as the remainder the count will increment plus one.
while(minNum % divisor == 0)
{
//increment count and divisor for next iteration of loop.
count++;
divisor++;
}
//If count = 2 then that means the number can only be divisible by 1 and itself and will output to console as a prime.
if (count = 2)
{
cout << minNum <<" ";
}
}
}
}
inFile3.close();

```

any suggestions?

### #14 KYA

• g++ jameson.cpp -o beverage

Reputation: 3145
• Posts: 19,185
• Joined: 14-September 07

## Re: Prime numbers between two integers

Posted 10 May 2010 - 07:16 AM

Are you not allowed to write a small prime checking function? It's cleaner then looping through divisors of a number.

The other issue is here:

``` if (count = 2)
{
cout << minNum <<" ";
}

```

Assignment operator rather then equality.

### #15 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11138
• Posts: 41,840
• Joined: 27-December 08

## Re: Prime numbers between two integers

Posted 10 May 2010 - 07:46 AM

You could also use the Sieve of Eratosthenes to generate primes into a vector from 2 through the largest number you read in. Then, just print from a-b for each pair you read in.