| | |
Prime number program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
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!
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!
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.
Now inside your main use a for loop to check is a number is prime
After you get this working, then change it to what you need it to be.
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.
C++ Syntax (Toggle Plain Text)
bool isPrime(int num) { //if num is equal to 2 then return true; //check if num is divisible by anything else other than its self for( i = 2; i < num; i++) { // if num mod i is equal to 0 then num is not a prime number so return false } //if it passed the loop then it is a prime number so return true }
Now inside your main use a for loop to check is a number is prime
C++ Syntax (Toggle Plain Text)
//inside your main for i is equal to 2, until MAX { //call isPrime with the argument being i. check if it return true // if it return true then print to screen that it is a prime //else do nothing }
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
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
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
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 .....
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 .....
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
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.
C++ Syntax (Toggle Plain Text)
bool isPrime(int num) { if(num < 2 ) return false; else if(num == 2) return true; for(int i = 2; i < ceil ( sqrt(num) ); i++) if(num % i == 0) return false; return true; } void printPrime(const int Max = 100) { for(int i = 2; i < MAX; i++) if( isPrime(i)) cout << i <<" is Prime\n"; }
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
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
0
#7 Nov 8th, 2009
•
•
•
•
C++ Syntax (Toggle Plain Text)
bool isPrime(int num) { if(num < 2 ) return false; else if(num == 2) return true; for(int i = 2; i < ceil ( sqrt(num) ); i++) if(num % i == 0) return false; return true; } void printPrime(const int Max = 100) { for(int i = 2; i < MAX; i++) if( isPrime(i)) cout << i <<" is Prime\n"; }
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
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:
^^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
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:
C++ Syntax (Toggle Plain Text)
//the calulation function. void calc (unsigned int number, unsigned int i) //using "void" instead of "int", because the funktion doesn't need to return a value. { //define variables 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). unsigned int a = 0; //set a = 1, used to number the primes. unsigned int j; //write welcome message cout << endl << "The program is finding primes in the domain of \"" << i << " ---> " << number << "\"" << endl; //tell the entered domain on the screen out << "The program is finding primes in the domain of \"" << i << " ---> " << number << "\"" << endl; //same as above, just to the file. //Calcul funktion for (; i<=number; i++) //run the "i" loop, when "i" is less or equal (<=) to the input number., then +1 for each completed cycle. { 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.) //Filtering function, filters all non primes, and sets "primecheck = 0", if the tested number isn't a prime. 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. {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. //Screen / file output if(Primecheck!=0) //if number is a prime, then write output. { a++; //add +1 to "a", simply to be ready number the next prime. 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". } //terminate for "i" loop, when "i" has reached the max domain number. } //write end message cout << a << " primes was found; in the given domain!" << endl << endl; //announce the number of primes found on the screen. //terminate function }

// Skeen
// Skeen
![]() |
Similar Threads
- Urgent: Plz Help me,Prime Number Program Problem (C++)
- C++ prime number program help (C++)
- C++ prime number program help (C++)
- Prime Number program.. with a twist (C)
Other Threads in the C++ Forum
- Previous Thread: .lib, .dll, .h - How to combine them in project?
- Next Thread: Win32 text
| Thread Tools | Search this Thread |
Tag cloud for c++, math, numbers, prime, primenumbersinrange
.dll ada algorithm api array arrays assignment based beginner binary bitmap c# c++ char char* class code commentinghelp compile compression console conversion data degrees delayload desktop development download drawing embedded encryption enum equation error examples file fstream function functions game gdi+ getvalue gnu graph ifstream int java linux lnk2019 mandelbrot math microsoft modal newbie number numbers objects output parallel partnership php picturebox pointers prime primenumbersinrange problem program programming project projects python question radians read recursion recursive round rpg search set statistics store string strings struct symbian system technology template templates text time timer tree url vb visual void web win32







