The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


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.
Reply
 
LinkBack Thread Tools    Display Modes   
  #11 (permalink)  
Old 07-23-2007, 12:53 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
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.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 2 Users Say Thank You to Bench For This Useful Post:
rpgfan3233 (07-24-2007), TeraTask (07-25-2007)
  #12 (permalink)  
Old 07-24-2007, 12:45 AM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
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.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #13 (permalink)  
Old 07-25-2007, 10:11 AM
hnkplaya hnkplaya is offline
Novice
Join Date: Jun 2007
Posts: 7
iTrader: (0)
hnkplaya is on a distinguished road
yes but it is suppose to composite the largest divisible number of the value typed in. I dont know that is why I asked because I was not sure what I was doing there

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #14 (permalink)  
Old 07-25-2007, 12:08 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
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:
  1. Create and initialize variables
  2. User inputs value and value is stored in both number and number_backup
  3. If number is greater than 2 (0 and 1 are neither prime nor composite), then the following happens:
    * Test for division by odd values via loop_variable as the odd value. If divisible, the value of loop_variable is stored in greatest_divisor.
    * Test for division by 2 repeatedly until number is no longer even, in which case it is no longer evenly divisible by 2. During the test, greatest_divisor is multiplied by 2 and number is divided by 2.
    * Once the test is finished, it has multiplied too many times, so it must be divided by 2.
  4. Now greatest_divisor holds the value of the largest integer that can evenly divide the original number entered.
  5. The original number entered is then outputted to the screen and a test begins:
    * If the original number entered is less than 2, it cannot be prime and it also cannot be composite.
    * If the greatest_divisor is only 1, that means that it could not evenly be divided by something other than itself, meaning it is prime. If the original number entered was 2, then that is also prime.
    * If the above conditions are not satisfied (greatest_divisor is not equal to 1 and the original number is not 2), then the number is composite.
  6. The appropriate rest of code is outputted and then the program ends.
Here is 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.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
TeraTask (07-25-2007)
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 05:44 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50