Hello! I seem to be having some difficulties working on an assignment in which we need to find the Perfect Numbers of a number by creating a method that finds them. There is already a test that uses our created method so that it can print out the first 4 or 5 perfect numbers. A perfect number is a number whose sum of its proper divisors (any number that divides into it between 1 and n-1) is equal to the number itself. The first perfect number is 6: 1 + 2 + 3 = 6. The next is 28. We write it as "isPerfect(n)", and it must be a class (static) method, have one input parameter of the type "int." The "isperfect(n)" method returns a boolean value (ex: the return type of the isPerfect(n) method is boolean. The program that tests it is as such:

public class PerfectTest { public static void main(String[] args) { int i;

for (i = 1; i < 10000; i++) if ( Perfect.isPerfect(i) ) System.out.println( i ); } }

So far I'm out of ideas as it seems to complicated to even begin. Could anyone please lend a helping hand?

The idea is that you show us what you have so far, so we can help you with your specific problems. By the looks of it, you need a Perfect class which has a static isPerfect method, so that is a starting point. Inside the method you will have to loop from 2 to n-1 to find all the divisors and the modulus operator "%" will come in handy there.

Actually, going to Math.sqrt(n) will be enough. If a number i is a divisor, then so is n / i. Special cases you will have to look out for are 1 (don't add n itself), and Math.sqrt(n) - if this is an integer, don't add it twice.

[ UD: Rob, your contribution is appreciated, but this is a homework question, and we don't want to deprive the original poster of all learning opportunities. Thus I have removed the code part. ] [ November 08, 2007: Message edited by: Ulf Dittmer ]

I really appreciate the help, thank you. OK! Here's what I've got: public class Perfect { public boolean isPerfect(n){ //I'm thinking of the T/F answer I'll need later// int letter_n = n; }

public void isPerfect(int n){ //I know in the directions it says "isPerfect(n)" but it also says to . use "int n" so I'm kind of confused as to what I need to do// int n; int i; int x; //The adding of multiples seems like a pretty complex step, so . I'm guessing I'll need another variable, but it seems that I might always need another one.//

isPerfect = true;

for (int i = 0; i <= n-1; i++) { if (n % i == 0) i == x //a factor, though I don't know how I could keep doing this for how ever many factors there happen to be// } //this is where I'm really stuck. I don't know how to save all of the factors, then add them together later on. What do you guys suggest I do???

Thanks,

Reggie

RWilson
Greenhorn

Joined: Sep 25, 2007
Posts: 8

posted

0

Hey, thanks again. I think I figured it out. There's only one problem with booleans that I can't figure out. However, whenever I copy my code (its in "nedit" it won't paste here. Do you know why this is and how I can fix it?

Originally posted by RWilson: this is where I'm really stuck. I don't know how to save all of the factors, then add them together later on. What do you guys suggest I do???

Do you need to save the factors themselves? Or do you just need the added value? If the latter is the case, declare a variable (probably called sum), that will be initially 0 and gets each factor added as you encounter them.

As for your code, there is one real bug: your loop starts at 0. Then you execute n / i, which will cause an exception as you're dividing by 0. Start the loop at 1 instead.

RWilson
Greenhorn

Joined: Sep 25, 2007
Posts: 8

posted

0

Thanks Rob! I finally got it under control. Do you know, though, how I could copy/paste my code onto here (saving me a lot of time re-typing the code)?