Prime numbers between two integers

breakdown in communication?

Page 1 of 1

14 Replies - 5866 Views - Last Post: 10 May 2010 - 07:46 AM Rate Topic: -----

#1 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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;
}


Was This Post Helpful? 1
  • +
  • -

#3 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 noclaf  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Prime numbers between two integers

Posted 09 May 2010 - 08:51 PM

Nope. Your data is being read correctly. There's a larger problem.

Follow your logic through the while loop.

. 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.
.....ad nauseum.


You're also not reading the entire file. Just the first line. My advice is to think about this in small sections. Here's some pseudo code:

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

Was This Post Helpful? -1
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • 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.
@noclaf - eof is bad.

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

Was This Post Helpful? 1
  • +
  • -

#6 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 noclaf  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Prime numbers between two integers

Posted 09 May 2010 - 09:00 PM

View Postsarmanu, on 09 May 2010 - 09:53 PM, said:

@noclaf - eof is bad.


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

Was This Post Helpful? 0
  • +
  • -

#8 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 GCheek  Icon User is offline

  • D.I.C Head

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

Re: Prime numbers between two integers

Posted 09 May 2010 - 09:12 PM

View Postsarmanu, 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.
@noclaf - eof is bad.


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.

View PostGCheek, 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.
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Prime numbers between two integers

Posted 09 May 2010 - 09:21 PM

View PostGCheek, 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.
Was This Post Helpful? 1
  • +
  • -

#11 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 GCheek  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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.
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,873
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1