Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 21st, 2004, 1:53 PM   #1
Quiet Chaos
Newbie
 
Join Date: Dec 2004
Posts: 2
Rep Power: 0 Quiet Chaos is on a distinguished road
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.
Quiet Chaos is offline   Reply With Quote
Old Dec 21st, 2004, 2:08 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Old Dec 23rd, 2004, 7:35 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 24th, 2004, 3:51 PM   #4
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
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?
EdSalamander is offline   Reply With Quote
Old Dec 24th, 2004, 5:18 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 5th, 2005, 5:15 PM   #6
salexand
Newbie
 
Join Date: Jan 2005
Posts: 1
Rep Power: 0 salexand is on a distinguished road
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;
}
salexand is offline   Reply With Quote
Old Jan 5th, 2005, 9:43 PM   #7
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
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;
  }
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old May 18th, 2005, 6:31 PM   #8
william hu
Newbie
 
Join Date: May 2005
Posts: 1
Rep Power: 0 william hu is on a distinguished road
Thumbs up reply to the prime program

//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;
}
}
william hu is offline   Reply With Quote
Old May 20th, 2005, 3:17 PM   #9
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
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.
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC