448,548 Members | 1,183 Online Need help? Post your question and get tips & solutions from a community of 448,548 IT Pros & Developers. It's quick & easy.

# Loop, Perfect Numbers

 P: n/a I have to write a program that displays all of the perfect numbers less than 1000. I know I need 2 for loops and at least one if statement. What I need to know is, is there any method in Java to test if a number is perfect? Thanks. Jul 17 '05 #1
8 Replies

 P: n/a co*********@bellsouth.net wrote: I have to write a program that displays all of the perfect numbers less than 1000. I know I need 2 for loops and at least one if statement. What I need to know is, is there any method in Java to test if a number is perfect? Thanks. What do you mean "Perfect" number? do you mean prime? Jul 17 '05 #2

 P: n/a What do you mean "Perfect" number? A perfect number is one that equals the sum of all the numbers that divide evenly into it. Example - 6 is perfect because 1, 2, and 3 divide evenly into it and their sum is 6. Jul 17 '05 #3

 P: n/a co*********@bellsouth.net wrote: What do you mean "Perfect" number? A perfect number is one that equals the sum of all the numbers that divide evenly into it. Example - 6 is perfect because 1, 2, and 3 divide evenly into it and their sum is 6. See http://home1.pacific.net.sg/~novelway/MEW2/lesson1.html Class java.math.BigInteger has a method isProbablePrime(I)Z which would probably work. :) However if you use Euclid's algorithm the largest number you'll ever need to test for primality is 31, or 63 at the outside (by this time it is obvious that the next perfect number will be bigger than 1000). For numbers of that size almost any primality test you can think of will be good enough ... -- Chris Gray ch***@kiffer.eunet.be /k/ Embedded Java Solutions Jul 17 '05 #4

 P: n/a wrote in message news:... I have to write a program that displays all of the perfect numbers less than 1000. I know I need 2 for loops and at least one if statement. What I need to know is, is there any method in Java to test if a number is perfect? Thanks. No there isn't, and that's the point of the exercise. I won't write the program for you, but I'll show you how to go about it. First, you need to scan through all numbers, checking if they are perfect. i.e. For Each Of These (hint) candidate perfect numbers, you need to go through all the numbers below it to see if it is a factor. For Each Of These (hint) candidate factors, you need to check if it is a factor, and if it is, then add it to a running total. to follow the instructions as a computer would: For each candidate perfect number: start with a running total of zero For each candidate factor: is the candidate perfect number divisible by the candidate factor? (you need to do some maths here) if so, then add the factor to the running total Go back to next candidate factor All possible factors have now been checked, and the running total is now the sum of all factors. Is the running total equal to the candidate perfect number? if so, the candidate IS a perfect number! Advance to next candidate perfect number I hope you see how this set of instructions would guide a non-mathematician to find all the perfect numbers. Jul 17 '05 #5

 P: n/a On Sat, 13 Mar 2004 09:08:09 -0500, coxmarshal wrote: I have to write a program that displays all of the perfect numbers less than 1000. I know I need 2 for loops and at least one if statement. What I need to know is, is there any method in Java to test if a number is perfect? Thanks. Here, Try this... // procedure DoYourOwnHomework( int DONE ) { const HARDWORK=1; for (int STUDY=0; (STUDY = STUDY + HARDWORK); STUDY < DONE) { OpenBook(); ReadText(); Think(); } if (UNDERSTOOD) { WriteProgram(); TurnItIn() } else { Worry(); Kvetch(); DropOut(); } } Jul 17 '05 #6

 P: n/a co*********@bellsouth.net wrote: I have to write a program that displays all of the perfect numbers less than 1000. I know I need 2 for loops and at least one if statement. You don't need any loops or ifs. public class PerfectNumbers { public static void main(String[][] a) { System.out.println(6); System.out.println(28); System.out.println(496); } } :-) Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. Jul 17 '05 #7

 P: n/a While I agree that you should do your own homework, I assume that its a Java assignment and not a Math one ! therefore a little hint wont hurt. Euclid had come up with an algorithm to generate perfect numbers. You can find more details here: http://www-gap.dcs.st-and.ac.uk/~his...umbers.html#s1 This extract should help though: There exists an elegant and sure method of generating these numbers, which does not leave out any perfect numbers and which does not include any that are not; and which is done in the following way. First set out in order the powers of two in a line, starting from unity, and proceeding as far as you wish: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096; and then they must be totalled each time there is a new term, and at each totalling examine the result, if you find that it is prime and non-composite, you must multiply it by the quantity of the last term that you added to the line, and the product will always be perfect. If, otherwise, it is composite and not prime, do not multiply it, but add on the next term, and again examine the result, and if it is composite leave it aside, without multiplying it, and add on the next term. If, on the other hand, it is prime, and non-composite, you must multiply it by the last term taken for its composition, and the number that results will be perfect, and so on as far as infinity. Good luck ! Ian Jul 17 '05 #8

 P: n/a Ian wrote: There exists an elegant and sure method of generating these numbers, which does not leave out any perfect numbers and which does not include any that are not; and which is done in the following way. You mean someone's finally solved the mystery of the odd perfect number? Do you have a source for this? Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. Jul 17 '05 #9

### This discussion thread is closed

Replies have been disabled for this discussion. 