•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,590 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 2,896 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: 13256 | Replies: 20
![]() |
•
•
Join Date: Jun 2005
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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.....
<< moderator edit: added [code][/code] tags >>
I need all the help i can get
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;
}I need all the help i can get
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).
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).
•
•
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation:
Rep Power: 4
Solved Threads: 1
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.
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.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
Join Date: Jul 2005
Posts: 15
Reputation:
Rep Power: 4
Solved Threads: 0
i got a similar program too :
<< moderator edit: added [code][/code] tags >>
:lol:
created on 6/25/2005
# 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;
}:lol:
created on 6/25/2005
•
•
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation:
Rep Power: 4
Solved Threads: 1
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.
<< moderator edit: fixed [code][/code] tags (forward slash) >>
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";
}
}•
•
Join Date: Jul 2005
Posts: 15
Reputation:
Rep Power: 4
Solved Threads: 0
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();
}
# 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();
}
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Other Threads in the C++ Forum
- Previous Thread: A Vector of a Class
- Next Thread: Write a calculator program using a do-while loop




Linear Mode