![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| Given any positive integer, N, First of all, deal with your special cases, 0 and 1. Then test each number from 2 upto the square root of N (Which you can truncate to an int) to see if its a potential divisor. if any of these numbers are evenly divisble, then N is not prime. Otherwise, N is prime Try to work out the steps on paper in pseudocode before creating the program if you're not sure. |
| The Following 2 Users Say Thank You to Bench For This Useful Post: | ||
rpgfan3233 (07-24-2007), TeraTask (07-25-2007) | ||
| |||
| I agree. No problem is terribly difficult once you figure out some of the most important steps properly. ![]() Pseudocode is a great tool that I use somewhat often. I highly recommend it if you have trouble figuring out the right bit of code to do what you want. ![]() I'd also like to recommend making 2 a special case as well, since it is the only prime number that is also even. From there, you can test every odd number (3, 5, 7, 9, etc.) up to the square root of N, as Bench mentioned. It saves a lot of time to test only the odd numbers and it saves even more time when you test only up to the square root of N. However, if you don't know how to use the square root or don't want to, just N/2 will do as long as you aren't working with a large number N. I hope we helped you a bit more. Good luck with your code! ![]() __________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| |||
| Below is some pseudocode that I created. All it does is find the greatest divisor (e.g. for the number 93, the biggest integer that can evenly be divided is 31). After finding it, it outputs the original number, followed by prime or composite. If it is composite, the greatest divisor is also outputted. Note that 2 is specially treated as it is an exception to the statement "all even numbers are not prime". The details of the code:
Code: Create greatest_divisor As Integer
Create number As Integer
Create number_backup As Integer
Create loop_variable As Integer
number = 2
number_backup = 2
greatest_divisor = 1
loop_variable = 3
Print "Enter a positive integer: "
Input number
number_backup = number
Comment
I'm assuming division with integers actually
does integer division like it does in C
(it is meant to be C code after all...)
End Comment
While loop_variable <= (number / 2)
Comment '%' is the remainder operator (the Mod operator in this case)
If (number % loop_variable) = 0 Then
greatest_divisor = loop_variable
End If
loop_variable = (loop_variable + 2)
End While
Comment We still need to test multiples of 2 such as 2, 4, 8, etc.
If (number % 2) != 1 Then
While (number % 2) != 1
greatest_divisor = (greatest_divisor * 2)
number = (number / 2)
End While
greatest_divisor = (greatest_divisor / 2)
End If
Print number_backup
Comment
2 is a special case, just like a number such as 1
End Comment
If number_backup < 2 Then
Print " NOT PRIME AND NOT COMPOSITE\n"
Else If (greatest_divisor == 1) Or (number_backup == 2) Then
Print " PRIME\n"
Else
Print " COMPOSITE ", greatest_divisor, "\n"
End If __________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
TeraTask (07-25-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |