3 Replies - 1462 Views - Last Post: 20 November 2007 - 07:19 PM Rate Topic: -----

#1 jrice528  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 07

question about lower and upper bound ints

Post icon  Posted 20 November 2007 - 03:51 PM

Ok, I have been working on a program to determine primes... First part was to write a program that determines the prime numbers between 2 and n.

The next part is to determine and print the prime numbers between a user-specified lower bound and user-specified upper bound.

How exactly would I go about doing that?

I have read mytext and looked on net, and cant find anything... heres the code i have so far

it would have to be in the area of the
cout<<"Please enter a number: ";
cin>>b;



#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;


const unsigned W = 10;


int main(int argc, char *argv[])
{
   const unsigned arraySize = 500;
   bool a[arraySize];
   unsigned b, x;
   unsigned counter = 0;
	

cout<<"This program is programming assignemnt #5."<<endl;
cout<<"THE SIEVE OF ERATOSTHENES."<<endl;
cout<<"By Jeremy Rice of CSCI 111."<<endl;


//Initializing elements to 1 
		
for (int i = 0; i < arraySize; i++)			 
{
   a[i] = 1;
}	   
// For array subscript 2, all elements beyond 2 in the array that
// are multiples of 2 will be set to zero; for array subscript 3, 
// all elements beyond 3 in the array that 
// are multiples of 3 will be set to zero


for (int i = 2; i * i < arraySize; i++)
	if (a[i])
	   for (int j = i + i; j < arraySize; j += i)
		   a[j] = 0;	  
										   

// print all subscripts set to 1

cout<<"Please enter a number: ";
cin>>b;
for (int j = 2; j < b; j++)
{
   if (a[j] == 1)
	{
	   counter++;
	   cout<<setw(W)<<counter + 1<<endl;
	} 
	   else
		  counter++; 
}		
system("pause");
return 0;
}




This post has been edited by jrice528: 20 November 2007 - 03:52 PM


Is This A Good Question/Topic? 0
  • +

Replies To: question about lower and upper bound ints

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: question about lower and upper bound ints

Posted 20 November 2007 - 04:08 PM

this does present a little bit of a problem.

The most naive approach is to write an "isPrime(n)" function that checks to see if n is prime by seeing if it is divisible by any numbers in the range 2 - sqrt(n)... but, if you think about it you only need to see if the n is divisible by prime numbers between 2 and sqrt(n). Since you have a function for finding primes between 2 and sqrt(n) you can now optimize the isPrime function to only check primes.

Once you have isPrime(n) made then loop from the lower bound to the upper bound of the users range, exit the loop at the first prime found.
Was This Post Helpful? 0
  • +
  • -

#3 jrice528  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 07

Re: question about lower and upper bound ints

Posted 20 November 2007 - 04:50 PM

I am not suppose to use a function to determine the prime, still really new to programing, and the rest kind of lost me =/ here is a sample of how the program should run.


 prime 105
105 is not a prime number
> prime 35 75
There are 10 prime numbers between 35 and 75:		
37		41		43		47		53	 
59		61		67		71		73

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: question about lower and upper bound ints

Posted 20 November 2007 - 07:19 PM

well then just impliment the class isPrime logic... it does not really have to be a function -- that just makes your program cleaner.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1