![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2004
Posts: 2
Rep Power: 0
![]() |
Anyone know where I can find some really basic source code for displaying x number of prime numbers? I already made one but it's kinda long and confusing and I'm wondering if I could simplify it.
|
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Post your code, and we'll see if we can help make your code simpler.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I remember writing a function in either C++ or VB on here to find primes, but I can't find it. Try searching, but I can't guarantee anything.
|
|
|
|
|
|
#4 |
|
Programmer
|
The problem with primes is there is that no formula exists that can find them reliably. If x has an upperbound, it might just be easier to just create an array or somesuch that has all the primes you need already worked out. Otherwise you'd have to check whether each subsequent number was a prime or not. The easiest way to work that would be to begin with 2 (the first prime, 1 technically doesn't count) and working upward, check each following odd integer by modding it by all of the primes you've already found that are less than its square root. Sounds a little complicated, but that's the fastest, most efficient way I can think of to do it offhand. Removing some of the requirements, like modding against all lower numbers instead of just the primes lower than the root, would simplify it quite a bit, but it would also be much less efficient.
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Well, it's been proven that there is a way to find the primes, but it's even slower than that, and technically impossible. Check out http://www.musicoftheprimes.com/ - it's pretty interesting.
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jan 2005
Posts: 1
Rep Power: 0
![]() |
Here is a program in C++ that will display all of the primes less than a given number (the variable CAP in this program). check it out and see what you think. you could add 1, maybe two lines of code to make it display a certain number of primes. I am quite a beginner, so if there are any errors, let me know. The one thing I haven't been able to figure out is how to initiate the array i have labeled 'Prime' to the size of CAP. In other words, I only want the array to be one integer longer than the number the user inputs (or the program already contains) for CAP. I'm not sure if that made any sence, but check out the code. Thanks, and I hope it helps.
#include <iostream.h>
int main()
{
/* Creating an array with each position
equal to the value of that position
a.k.a. Prime[4]=4 and so on*/
int CAP = 100;
/*CAP is the upper bound on the list of primes
could add an input so that the user defines CAP*/
int Prime[101];
int x=0, y, z;
while(x<CAP)
{
Prime[x]=x;
x++;
};
/* Deleting all multiples of lower primes*/
y=2;
while(y<CAP)
{
if(Prime[y]!=NULL)
{
z = 2*y;
while(z<CAP)
{
Prime[z]=NULL;
z+=y;
}
}
y++;
};
/*displaying results*/
x=0;
while(x<CAP)
{
if(Prime[x]!=NULL)
{cout << x << "\n";}
x++;
}
getchar();
return 0;
} |
|
|
|
|
|
#7 |
|
Professional Programmer
|
here's code that finds primes in Java, i'm sure you won't have a problem figuring it out.
public static void checkPrime(boolean[] a)
{
for (int nextPrime = 2; nextPrime < a.length; nextPrime++)
for(int i = nextPrime + 1; i <= a.length - 1; i++)
if (i % nextPrime == 0 && a[i] == true)
a[i] = false;
} |
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2005
Posts: 1
Rep Power: 0
![]() |
//x is numer. a[] is denom. y is iter
//r is range, n keeps track of # of //primes within a[4 to 258333] //if you have any commons or a better //algorithm than this //contact me @ //yue868686@163.com Yue Hu //santiam christian high school //this program uses 3 to find primes between 3-23, then use the results to //to find all primes between 23-625 #include<iostream> #include<iomanip> #include<cmath> using namespace std; void main() { unsigned long x=3,y,a[250]={3},n=0,r=25; while(1) { for(;x<r;x+=2) { y=0; while(x%a[y] && a[y]<sqrt(x)) y++; if(a[y]>sqrt(x)) { a[n++]=x; cout<<setw(8)<<x; } } r*=r; } } |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
We've just had a thread about prime numbers crop up in the C++ forum.
http://programmingforums.org/forum/s...ead.php?t=3936 This might be of interest here, too. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|