![]() |
|
#1
|
|||
|
|||
Plz help me to write this program..Hi All,
Who can i write a program that tests a number if its "prime" or not regards |
|
#2
|
|||
|
|||
|
You must start on your own and come up with something and then ask specific questions when you are stuck. can't write your code for you
Hint: prime numbers are not naturally divisible by anything except themselves and 1. Hint: all the prime numbers are odd except 2. Examples: 2(only even prime number), 3, 5, 7, (not 9), 11 .... |
|
#3
|
|||
|
|||
|
Hi
Thanks machinated, Thats My program ________________________ CPP / C++ / C Code:
What do you say? Regards Last edited by dsmith : 07-May-2004 at 07:55.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#4
|
||||
|
||||
|
Hi X-man, welcome to GIDForums.
First of all let me direct you to this thread. Therein is a lengthy discussion of prime numbers with various solutions and code snippets. As for your code, I think that you are on the right track, but it is not complete. 49 is at least one number I can think of off the top of my head that would be shown as prime by your program. I think using what you have and some of the algorithms shown in that other thread, you should be able to solve this thing quite easily. Good Luck, d |
|
#5
|
||||
|
||||
|
Quote:
__________________
-Aaron |
|
#6
|
||||
|
||||
|
i help u code 1 example of prime number
actually there is many way to write but is almost similar i not sure my code is short enough and efficiency you try to understand it you can reference but try not copy, cause programming is not just have a solution they is many solution, is either efficiency or not. CPP / C++ / C Code:
__________________
challenges are make life interesting, overcome them is make life meaningful. |
|
#7
|
|||
|
|||
Thanks AllHi all,
Thanks for your replay I wrote the program befor i see your replys Here is my "program" CPP / C++ / C Code:
|
|
#8
|
||||
|
||||
|
I don't see why there is a need for a 'for' loop of 1000 when you have the number to validate. Well, you may not looking forward for effeciancy, but it is a good practice!
![]() __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#9
|
||||
|
||||
|
Quote:
The for loop should definitely be changed. Following are progressively better loops: 1) Don't loop to 1000. The number is your absolute max: for ( a = 2 ; a <= num ; a ++) 2) Thinking about the loop, you also obviously only have to go thru half the numbers: for ( a = 2 ; a < num/2 ; a ++) 3) Not so obviously, the real limit is the square root of the number: for ( a = 2 ; a < sqrt(num) ; a ++) For this you need to add the math header file. 4) Check for 2 first, the the loop can start at 3 and skip all even numbers: CPP / C++ / C Code:
Finally, inside the loop all you need is: CPP / C++ / C Code:
So the entire program can become: CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#10
|
||||
|
||||
|
Oops, the line prime = 1; should be b = 1; in the final code above.
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 11:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
| Need help with a C program (Long) | McFury | C Programming Language | 3 | 29-Apr-2004 21:06 |
| Will pay money for someone to write a semi complex program | bstan | Computer Programming Advertisements & Offers | 0 | 23-Feb-2004 04:17 |
| How to write simple program like ........? | laputa9000 | C++ Forum | 3 | 29-Oct-2003 13:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The