![]() |
|
#1
|
|||
|
|||
prime numbers helpHi im having problem with prime numbers i need to find all the prime numbers from 1 -100 and this is what i code a very simple code
CPP / C++ / C Code:
Last edited by LuciWiz : 27-Aug-2006 at 06:58.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#2
|
||||
|
||||
Re: prime numbers helpQuote:
Please read that obnoxious "Read This First" post to see how to properly post code and ask good questions that are answerable. Also, search these forums to see other discussions about prime numbers. It'll save you a lot of time -- you won't have to wait for an answer. Primes have been asked about many times. As a noob, you should learn how to format your code now before you get into bad habits. See this for some ideas. And getch() not a good choice to pause the program. In C++, cin is better. It's part of the language. getch() is not a Standard C++ function. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
|||
|
|||
Re: prime numbers helptnx for the help i completed it^^.... and also can you check my code if i have done something wrong....tnx here's the code
Code:
__________________________________________________ ___________________________________ ooooopsss i think i know how to do the other way......is my coding right??? cause it's running good.... here's the code Code:
|
|
#4
|
|||
|
|||
Re: prime numbers helpQuote:
Both of your examples print out all of the 49 odd numbers from 3 to 99. Actually there are 25 prime numbers less than 100, and here they are: Code:
So, I don't think you have quite finished. Here's my take on the problem statement, expressed in a "top-down" computer-like language. Why do I say "computer-like"? Because I am already thinking of loops. Why do I say "top-down"? Because this is a big picture of the goal, without any particular attention to little details. Code:
That's easy enough to say, but how do we do the "good stuff"? (Determine whether a given number is prime.) Let's just look at this part: For a given number, x, (where x is greater than or equal to 2 and less than 100) how can we determine whether x is a prime number? Here's a "brute force" way: Code:
In something that is more like a C program: CPP / C++ / C Code:
Note to more experienced programmers: Now, let's not get embroiled in some discussion about how this is not a very efficient method for finding prime numbers; I know that just as well as you. My point was to attempt to use the starting position of the person requesting help, and show one way of approaching the problem. If any one has other elementary ways of telling a beginning programmer how to do this, I welcome any and all contributions (and I'll bet other beginners will, too). Regards, Dave "Implement first; optimize later." ---davekw7x |
|
#5
|
|||
|
|||
Re: prime numbers helpthe better and more efficient way is:
CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: prime numbers helpQuote:
Actually, there are 26 - you missed out 1 (definition of a prime number: "a number only divisible by 1 and itself") __________________
Michael Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++). |
|
#7
|
||||
|
||||
Re: prime numbers helpQuote:
That is the definition, but 1 is the exception! Every rule has an exception. <- even this one Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#8
|
||||
|
||||
Re: prime numbers helpQuote:
Actually the definition goes more like this: Quote:
Want more? Quote:
One justification for not allowing 1 to be considered a prime number in the definition: Quote:
Etc., etc. (and etc.). Regards, Dave |
|
#9
|
|||
|
|||
Re: prime numbers helpQuote:
Not allowing 1 to be considered a prime number: It's not a rule; it is a definition, accepted with good reason by modern mathemeticians. A definition is inarguable. Regards, Dave |
|
#10
|
|||
|
|||
Re: prime numbers helpQuote:
__________________
Michael Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++). |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Python script: Prime numbers | crystalattice | Python Forum | 4 | 16-Apr-2006 09:19 |
| subscript error in coding | warborules | C Programming Language | 6 | 27-Nov-2005 18:16 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 09:36 |
| prime numbers | quasimof | C++ Forum | 1 | 01-Nov-2004 20:35 |
| Help w/ prime # determination | crystalattice | C Programming Language | 17 | 18-Apr-2004 22:43 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The