User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 396,892 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,227 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 13234 | Replies: 20
Reply
Join Date: Jun 2005
Posts: 7
Reputation: dark7angelx07 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dark7angelx07 dark7angelx07 is offline Offline
Newbie Poster

Program that determines if a number is prime

  #1  
Jun 30th, 2005
Hi,
I need help writing a function that determines if a number is prime. It has to print all the prime numbers between 1 and 10000
this is what i have...where i put a " */* " is where i need your help.....

#include<iostream>

using std::cout;
using std::endl;

#include<iomanip>

using std::setw;

/* write prototype for function prime */

int main()
{
  int count = 0;

  cout << "The prime numbers from 1 to 10000 are:\n";

  for (int loop =2; loop <= 10000; ++loop)

     if ( /* make call to function prime */  ) {
        ++count;
        cout << setw( 6 ) << loop;

     if ( count % 10 == 0)
        cout << '\n';
}

  cout << '\n' << "There were " << count
         << " prime numbers\n";

 return 0;
}
  bool prime (int n)
{
  for ( int i = 2; /*write loop condition */; i++ )

     if( /*write code to test if n is divisible by i */)
        return false;

   return true;
}
<< moderator edit: added [code][/code] tags >>

I need all the help i can get
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: Program that determines if a number is prime

  #2  
Jun 30th, 2005
to call the function prime, it is just to add:
prime(loop)

a simple loop condiction is:
(n/2)+1 > i
since after 1/2 of the val of n there cant be a fraction of the n number.

and the test are simple ennuf:
if (n%i == 0)
Reply With Quote  
Join Date: Jun 2005
Location: Troy
Posts: 1,277
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 36
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: Program that determines if a number is prime

  #3  
Jun 30th, 2005
To find if a number n is prime, you can just loop through all the numbers from 2 to (n - 1) and see if any of them divide evenly, using the modulus operator. So your loop ending condition could be (i < n), and you know that i divides into n if (n % i) equals 0.

This is a dumb algorithm, though. Instead, you can loop through all the numbers from 2 to the square root of n and test them. No need to go all the way up to (n - 1). Because if a number more than the square root of n divides into n, then a number less than the square root of n also works.

You might also want to try figuring out a method that doesn't use a prime testing function, which uses an array (maybe some other time).

Making the call to the function prime would be the same as making any function call, would it not? prime(loop).
Reply With Quote  
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Program that determines if a number is prime

  #4  
Jun 30th, 2005
Someone commented that to see if n is a prime you only have to check possible divisors < sqrt(n). In addition, you can decrease the computing time a lot by testing the divisor 2 , then 3, 5, 7..., skipping every other integer. The reason that this works is once you've determined that 2 is not a divisor you don't have to check 4, 6,8...or any even integer because if one of them were a divisor then so would 2 be a divisor. With a little ingenuity you can extend this to not testing multiples of 3 once you've determined that 3 is not a divisor, etc.

Actually, to produce a table of all the primes < 100000 (or, for that matter, of 1 million or ten million) there's a much better way called the sieve of Eritosthenes. (I think I spelled that all wrong). You can find it in many books. It's blindingly fast, using no divisions and a short code does the trick. Maybe this is what your instructor really wanted.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Program that determines if a number is prime

  #5  
Jul 1st, 2005
I think I read somewhere that to find out if a number is a prime, you dont have to check dividing upto n-1. Upto squareroot(n) is enough. check it out.
Reply With Quote  
Join Date: Jul 2005
Posts: 15
Reputation: cpp noob is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
cpp noob cpp noob is offline Offline
Newbie Poster

Re: Program that determines if a number is prime

  #6  
Jul 1st, 2005
i got a similar program too :

# include<iostream.h>
# include<conio.h>

void main()

{
	clrscr();

	int isprime(int);
	int value;

	//input
	cout<<"Enter a number to verify whether it is prime or not "<<endl;
	cout<<"Enter -1 to stop"<<endl;
	cin>>value;

	//output
	while (value!=-1)
		{

		if (isprime(value)==1)
			{
			cout<<"Yes the number is prime"<<endl;
			}
		else
			{
			cout<<"Sorry, not a prime"<<endl;
			}
		getch();
		clrscr();
		cout<<"Enter a number to verify whether it is prime or not "<<endl;
		cin>>value;

		}
}

	//calculation
	int isprime(int valuex)
		{
		int x,j;
		x=1;

		for (j=2 ; j<valuex/2 ; j++)
			{
			if ((valuex%j)==0) x=0;
			}

		if(x!=0) return 1;
		else return 0;
		}
<< moderator edit: added [code][/code] tags >>

:lol:
created on 6/25/2005
Reply With Quote  
Join Date: Jun 2005
Posts: 7
Reputation: dark7angelx07 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dark7angelx07 dark7angelx07 is offline Offline
Newbie Poster

Re: Program that determines if a number is prime

  #7  
Jul 1st, 2005
Hi , I had a question again for what you said above.....

So I would just write..... if prime(loop) ...Is that how I should write it??
OR if (prime(loop)) ???

What should I write for the prototype for function prime? (first line)


Thank you so much for helping me.....
Reply With Quote  
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: Program that determines if a number is prime

  #8  
Jul 1st, 2005
Your program doesn't make use of the efficiencies I mentioned. For one thing, you test possible factors up to valuex/2 but you only have to go up to sqrt(valuex), which is much smaller. (For example, sqrt(100) is 10, much less that 100/2.) Also you increment trial divisors by one rather than 2. Finally, once you find a divisor (and so, you know the number's not a prime), you contiinue to try more divisors!

Here's my version.
#include <iostream>
using namespace std;

bool isaprime(long);

bool isaprime(long x)
{   long n;
    // is 2 a divisor?
    if ( x%2 == 0)
       return false;
    n = 3;
    while (n*n <=x)
    {   if (x%n == 0)
           return false;
        n += 2;
    }
    return true;
}

int main()
{   long x;
    while (1)
    {   cout <<"Enter a positive integer to check for primality, 0 to exit: ";
        cin >> x;
        if (x == 0)
        {   cout << "Thanks for the fun. Come again.\n";
            return 1;
        }
        if (isaprime(x))
           cout <<x<<" is a prime\n";
        else
           cout <<x<<" is not a prime\n";

   }

}
<< moderator edit: fixed [code][/code] tags (forward slash) >>
Reply With Quote  
Join Date: Jun 2005
Posts: 16
Reputation: mina1984 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
mina1984 mina1984 is offline Offline
Newbie Poster

Re: Program that determines if a number is prime

  #9  
Jul 2nd, 2005
cpp noob
using your program, which tests for the integer input by the user,
how you could test all the numbers from 1 to 10,000? I duno i was all curious about it too.

Mina
Reply With Quote  
Join Date: Jul 2005
Posts: 15
Reputation: cpp noob is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
cpp noob cpp noob is offline Offline
Newbie Poster

Re: Program that determines if a number is prime

  #10  
Jul 2nd, 2005
i lucky have this program creted on the same day

# include<iostream.h>
# include<conio.h>

void main()

{
clrscr();
int value, x, i, j;

//input
cout<<"Enter the value to which you want to find the prime numbers ";
cin>>value;

//calculation
for ( i=3 ; i<=value ; i++)
{
x=1;

for (j=2 ; j<i ; j++)
{
if ((i%j)==0) x=0;
}

//output
if(x!=0) cout<<i<<" is a prime number"<<endl;
}
getch();
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:13 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC