Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Prime numbers

Prime numbers
Thread Tools
jme
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Helsinki, Finland
Status: Offline
Reply With Quote
Apr 26, 2001, 03:59 AM
 
Could someone submit a simple prime number seeking algorithm in C for me? Rather with the iostream library (cout, not printf).

jme
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 27, 2001, 07:31 PM
 
Mersenne? Regular?

It should be really easy to find one: just search on Google for "Prime Number Algorithm."

This is a pretty terrible algorithm, but it'll do...

Code:
int i, j, found = 0; for (i = 2; i < PRIME_MAX; i++, found = 0) { for (j = 2; j < ((int)sqrt(i))+1; j++) { if (i % j == 0) { found = 1; break; } } if (found) cout << "Hey! " << i << " is a prime number!\n"; }
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:10 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2