Prime number program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 4
Reputation: Mafia619 is an unknown quantity at this point 
Solved Threads: 0
Mafia619 Mafia619 is offline Offline
Newbie Poster

Prime number program

 
0
  #1
Sep 11th, 2009
Hey, I've got an assignment from my computer teacher in school.
I've got to make a program that displays the prime numbers in a range and also display the number of prime numbers that are present.
And i'm only supposed to use the basic header files.....that means the program should start of something like this -:

#include<iostream.h>
#include<conio.h>
#include<math.h> \\if needed
int main()
{




so can anyone please help me do this..............thank you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,435
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 186
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Prime number program

 
0
  #2
Sep 11th, 2009
1) First create a prime number generator
2) The adjust it accodignly to what you need it.

A prime number is one where its 2>= | n | < MAX, n != even, n is evenly divisible its self and 1 only.

A few primes are 2,3,5,7,11.

5 is a prime because its divisible only by 1 and its self evenly.
5/5 = 1 with 0 remainder
5/1 = 5 with 0 remainder

5 mod 4 != 0
5 mod 3 != 0
5 mod 2 != 0


Some hints. Create a isPrime(int num), function.
  1. bool isPrime(int num)
  2. {
  3. //if num is equal to 2 then return true;
  4.  
  5. //check if num is divisible by anything else other than its self
  6. for( i = 2; i < num; i++)
  7. {
  8. // if num mod i is equal to 0 then num is not a prime number so return false
  9. }
  10.  
  11. //if it passed the loop then it is a prime number so return true
  12. }


Now inside your main use a for loop to check is a number is prime
  1. //inside your main
  2. for i is equal to 2, until MAX
  3. {
  4. //call isPrime with the argument being i. check if it return true
  5. // if it return true then print to screen that it is a prime
  6. //else do nothing
  7. }

After you get this working, then change it to what you need it to be.
Last edited by firstPerson; Sep 11th, 2009 at 3:52 pm.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 977
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Prime number program

 
0
  #3
Sep 11th, 2009
It's <iostream> not <iostream.h>, avoid <conio.h>, and you probably won't need <cmath>.

Now let me freak your mind: You're using Turbo C++, or tutorials for it? Right?

There's a simple bit scanning method you can throw together with the links I posted on another thread: http://www.daniweb.com/forums/thread204950.html
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: Mafia619 is an unknown quantity at this point 
Solved Threads: 0
Mafia619 Mafia619 is offline Offline
Newbie Poster

Re: Prime number program

 
-1
  #4
Sep 13th, 2009
well the thing is that .....i use an older version of C++ ie. Borland C++ 4.5
so i need the program based on that format .......right now i've got a program that kinda looks like this-:

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int prime=0,r1,r2,n=0;
cout<<"\n Enter range \t";
cin>>r1>>r2;
for(int i=r1;i<=r2;i++)
{
prime++;

for(int j=0;j<=sqrt(i);j++);
{
prime=i;
if(i%2==0)
{
n++;
}
else if
{
cout<<prime;
}
}
}
cout<<"\n Number of primes \t";
cout<<n;
getch();
return 0;
}

but the necessary out put does not come.........so please help me out.......or my crazy teacher at skool is gonna screw me .....
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Prime number program

 
0
  #5
Sep 13th, 2009
Originally Posted by Mafia619 View Post
but the necessary out put does not come.........
Of course the necessary output doesn't come. You didn't expect it to, did you? You have no algorithm to test for/calculate prime numbers. There are a lot of ways to do this. firstPerson listed one way. If you don't have any other algorithm that you want to use, implement his. He's posted a skeleton that you can work off of.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,435
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 186
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Prime number program

 
0
  #6
Sep 14th, 2009
  1. bool isPrime(int num)
  2. {
  3. if(num < 2 ) return false;
  4. else if(num == 2) return true;
  5.  
  6. for(int i = 2; i < ceil ( sqrt(num) ); i++)
  7. if(num % i == 0) return false;
  8. return true;
  9. }
  10.  
  11. void printPrime(const int Max = 100)
  12. {
  13. for(int i = 2; i < MAX; i++)
  14. if( isPrime(i)) cout << i <<" is Prime\n";
  15.  
  16. }
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: thebluestar is an unknown quantity at this point 
Solved Threads: 0
thebluestar thebluestar is offline Offline
Light Poster
 
0
  #7
Nov 8th, 2009
Originally Posted by firstPerson View Post
  1. bool isPrime(int num)
  2. {
  3. if(num < 2 ) return false;
  4. else if(num == 2) return true;
  5.  
  6. for(int i = 2; i < ceil ( sqrt(num) ); i++)
  7. if(num % i == 0) return false;
  8. return true;
  9. }
  10.  
  11. void printPrime(const int Max = 100)
  12. {
  13. for(int i = 2; i < MAX; i++)
  14. if( isPrime(i)) cout << i <<" is Prime\n";
  15.  
  16. }
When I test your code , it did not work correctly everytime
I also do not find what is the problem , I'm confused in this line
for(int i = 2; i < ceil ( sqrt(num) ); i++)
because if n=9 then it's prime
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #8
Nov 8th, 2009
Originally Posted by thebluestar View Post
When I test your code , it did not work correctly everytime
I also do not find what is the problem , I'm confused in this line
for(int i = 2; i < ceil ( sqrt(num) ); i++)
because if n=9 then it's prime
Try changing the < in line 6 to <=.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 58
Reputation: Skeen is an unknown quantity at this point 
Solved Threads: 1
Skeen's Avatar
Skeen Skeen is offline Offline
Junior Poster in Training
 
0
  #9
Nov 8th, 2009
Hey Mafia, I got about the same task some time ago, and I wrote my code using a bitwise buffer to store all uneven numbers from 3 till the entered number, and then by using 'The Sieve of Eratosthenes' to sort out the numbers, simply picking the first one, and removing all that divides with this from the buffer. Then picking the next number like and dividing that thoughout the buffer.

The buffer will be like this;
3 5 7 9 11 13 15 ...

The program now finds 3, writes that to the screen, then does the following, by dividing out the buffer;
5 7 11 13 15 ...

Then it findes 5, and does the same:
7 11 13 ...

And so on, atm I'm trying mulithread the application using OpenMP (Started progamming this summer, so I'm not really good at multitheading applications yet), but as single threaded it's able to find all the primenumbers in the domain of 2-100.000.000 (5761454Primes) in just under 12seconds , so it's quite effective. ^^that's using about 6mb of ram, and 2GHz, on my Laptop , so I'm quite satisfied about the code. There are quicker ways, but my professor was impressed, as he was expecting a simple primetest algorithm, like take a number, use it in a modulo loop, to check if any number above to divided with it returns 0. This will work ofc. however it's VERY slow, and ineffecient, so use 'The Sieve of Eratosthenes' method, if you're able to. The bitwise buffer, is simply to use less memory - and to speed up things a bit .

EDIT: The program only found 5761453, as I include 2 myself ofc
Btw; Code is quite messy and comments are written in danish, so wont really post it here, I'm sorry :o. Would simply be too messy to understand.

EDIT2: Here's a sample of my first sketch of a simple test algoritm, which does the job, but slowly:
  1. //the calulation function.
  2. void calc (unsigned int number, unsigned int i) //using "void" instead of "int", because the funktion doesn't need to return a value.
  3. {
  4. //define variables
  5. bool Primecheck; //bool, that indicates wether the number is a prime or not (1 = Prime, 0 = Not a prime). Bool is used instead of int, since it only has to store 2 values (1 and 0).
  6. unsigned int a = 0; //set a = 1, used to number the primes.
  7. unsigned int j;
  8.  
  9. //write welcome message
  10. cout << endl << "The program is finding primes in the domain of \"" << i << " ---> " << number << "\"" << endl; //tell the entered domain on the screen
  11. out << "The program is finding primes in the domain of \"" << i << " ---> " << number << "\"" << endl; //same as above, just to the file.
  12.  
  13. //Calcul funktion
  14. for (; i<=number; i++) //run the "i" loop, when "i" is less or equal (<=) to the input number., then +1 for each completed cycle.
  15. {
  16. Primecheck=1; //set the boolic value of primecheck = true, for the "i" loop. (to avoid a negativ loop, basically you'll have to set "not prime" each cycle, because everything is considered a prime at this point.)
  17.  
  18. //Filtering function, filters all non primes, and sets "primecheck = 0", if the tested number isn't a prime.
  19. for (j=2; ((j < i) && (Primecheck==1)); j++) //run the "j" loop, run this until the "j" value is equal to the "i" value, by plussing 1, each time the cycle completes, starting from 2.
  20. {if ((i%j)==0){Primecheck=0;}} //if "i" divided by "j" equals "0 in remainder", then set "Primecheck = 0" (number is not a prime). If i%j=0, then that's an devident, and therefore "i" isn't a prime number.
  21.  
  22. //Screen / file output
  23. if(Primecheck!=0) //if number is a prime, then write output.
  24. {
  25. a++; //add +1 to "a", simply to be ready number the next prime.
  26. cout << a << ". " << "Prime:\t"<< i << endl; //screen output - write "a" (1 as default (used to show which number in the prime number chain the prime number is)), then write "Prime:" then the number (the prime) "i".
  27. }
  28.  
  29. //terminate for "i" loop, when "i" has reached the max domain number.
  30. }
  31.  
  32. //write end message
  33. cout << a << " primes was found; in the given domain!" << endl << endl; //announce the number of primes found on the screen.
  34.  
  35. //terminate function
  36. }
^^Bad English, over commenting, and stuff, I wrote this code like a week after I learn'd that C++ even existed, so I know it sucks

// Skeen
// Skeen
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: Mehdia is an unknown quantity at this point 
Solved Threads: 0
Mehdia Mehdia is offline Offline
Newbie Poster
 
0
  #10
26 Days Ago
main()
{
int i,j=2,ch=0;
clrscr();
printf(
Reply With Quote Quick reply to this message  
Reply


Message:


Thread Tools Search this Thread



Tag cloud for c++, math, numbers, prime, primenumbersinrange
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC