# Perfect Numbers

RWilson

Greenhorn

Posts: 8

posted 8 years ago

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?

Thanks

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?

Thanks

bart zagers

Ranch Hand

Posts: 234

posted 8 years ago

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

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.

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.

posted 8 years ago

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 ]

[ 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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

RWilson

Greenhorn

Posts: 8

posted 8 years ago

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

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

Posts: 8

posted 8 years ago

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.

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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

RWilson

Greenhorn

Posts: 8

posted 8 years ago

Just copy paste it, and put it inside a code block:

[ code]You code goes here[/code] (remove the space after the [)

[ November 10, 2007: Message edited by: Rob Prime ]

[ code]You code goes here[/code] (remove the space after the [)

[ November 10, 2007: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions