469,352 Members | 1,671 Online

# Lost on Perfect Numbers program 20
-Alright im supposed to make a program that tests for perfect numbers. I have to use 2 methods and 3 methods if you count the main method.

-My first method has to return a boolean value of true or
false if the argument is or isn't a perfect number. I found that all perfect numbers either end in a 6 or an 8 so i was thinking i could just use something like...

Expand|Select|Wrap|Line Numbers
1. if (value1 % 2 = 0)
2.  {
3. value1 = Perfect number
4.  }
5. else
6.  {
7. value1 = Not perfect
8.  }
-Im confused on that if/else statement though because im not supposed to print out anything until my next method. And i know that if/else statement wont work so anyone know how to fix it?

-My last method has to take the users integer argument and print out the factors of the argument in descending order (largest to smallest). This is the part of the code im most confused on. I have no clue how to do this

- When my program is done its supposed to look like...

Please enter a possible perfect number: 6
6:3 2 1

or

Please enter a possible perfect number: -2
-2:NOT PERFECT

If anyone has any ideas or can help me i would really appreciate the help.
Nov 7 '08 #1
17 8951 JosAH
11,448 Expert 8TB
What is the definition of a perfect number? Start from there and forget the
divisability by six or eight. That is just a necessary but not a sufficient
condition.

kind regards,

Jos
Nov 8 '08 #2
nickels
20 well a a perfect number is defined as a positive integer which is the sum of its proper positive divisors. Like

6 = 1 + 2 + 3

28 = 1 + 2 + 4 + 7 + 14

but they found all perfect numbers end in 8 and 6 so thats why i came up with that idea
Nov 8 '08 #3
JosAH
11,448 Expert 8TB
well a a perfect number is defined as a positive integer which is the sum of its proper positive divisors. Like

6 = 1 + 2 + 3

28 = 1 + 2 + 4 + 7 + 14

but they found all perfect numbers end in 8 and 6 so thats why i came up with that idea
That's what I wrote: it is just a necessary but not a sufficient condition, i.e. is
8 a perfect number, or 16 or 18 or 26? You have to find the divisors of a number
and add them to be able to see whether or not a number is a perfect number.

Do you know how to find the divisors of a number?

kind regards,

Jos
Nov 8 '08 #4
nickels
20 That's what I wrote: it is just a necessary but not a sufficient condition, i.e. is
8 a perfect number, or 16 or 18 or 26? You have to find the divisors of a number
and add them to be able to see whether or not a number is a perfect number.

Do you know how to find the divisors of a number?

kind regards,

Jos
no i have no idea how im going to find the divisors of a number. Im thinking some kind of loop though?
Nov 9 '08 #5
JosAH
11,448 Expert 8TB
no i have no idea how im going to find the divisors of a number. Im thinking some kind of loop though?
Yep, and also have a look at the % (modulo) operator: a % b is the remainder of
a when you divide it by b; e.g. 7 % 3 == 1, 10 % 4 == 2, 12 % 3 == 0 etc.

Here's a simple loop:

Expand|Select|Wrap|Line Numbers
1. int a= 12;
2. for (int b= 1; b <= a; b++)
3.    if (a%b == 0) System.out.println(b);
4.
Try it for a couple of values for 'a' and see how you can use that loop.

kind regards,

Jos
Nov 9 '08 #6
nickels
20 well idk how im gonna do it because how u have the variable a=12, the loop has to check the users input value which could be like 232434 lol
Nov 9 '08 #7
steveyjg
10 http://mathworld.wolfram.com/PerfectNumber.html

Expand|Select|Wrap|Line Numbers
2. int x = Integer.parseInt(text);
or use java.util.Scanner
Expand|Select|Wrap|Line Numbers
1. Scanner keyboard = new Scanner(System.in);
2. int number = keyboard.nextInt();
You'll definitely need to write a loop that finds divisors. JosAH's loop works. However int b just needs to incremented to a/2 since a divisor won't be larger than half the number except of course the number itself (number * 1 = number). Or better yet the loop should only go up to floor(sqrt(number)) since the larger divisor can be calculated with the smaller divisor.

When a divisor is found you'll want to add it to a cumulative sum to see if the sum of the divisors are equal to the inputted number. Or since you need to output the divisors later anyway just store them in an array of ints then add them and see if the sum is equal to the inputted number.

So according to the Wolfram site the first eight perfect numbers are 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, and 2305843008139952128. The last three are out of the range of the int data type (2^31 or 2147483648). Also they would take a very long time to calculate anyway.

Hope this helps. There is plenty of code around the internet about perfect numbers. Look at some samples to help you along.
Nov 10 '08 #8
nickels
20 so something like

for (int b = 1; b < value; b++)
{
if ( value % b == 0)
{
somehow store the number
}

idk how i would store the number lol help?
Nov 10 '08 #9
nickels
20 alright i found a loop that helps solve it, but i need to some how keep track of the divisors and output them. this loop would work but it doesnt really keep track of the divisors. this is my whole code so far

Expand|Select|Wrap|Line Numbers
1.  import java.util.Scanner;
2.
3.  public class test
4.  {
5.  public static void main ( String args[] )
6.  {
7.  Scanner in = new Scanner ( System.in );
8.
9.  int value, value1;
10.
11.
12.  do
13.  {
14.  System.out.print ( "How many numbers would you like to test? " );
15.  value = in.nextInt();
16.  }
17.  while (value < 1);
18.
19.  for (int a = 0; a < value; a++)
20.  {
21.  System.out.printf ( "Please enter a possible perfect number ");
22.  value1 = in.nextInt();
23.  }
24.
25.
26.  public static testPerfect()
27.  {
28.
29.  sumSoFar = 0;
30.
31.  for ( a = 1; a < value1; a = a + 1 )
32.  {
33.    if ( value1 % a == 0)
34.       sumSoFar = sumSoFar + i;
35.  }
36.
37.
38.
39. }
40. }
Nov 10 '08 #10
nickels
20 alright right now im at

Expand|Select|Wrap|Line Numbers
1.  import java.util.Scanner;
2.
3.  public class test
4.  {
5.  public static void main ( String args[] )
6.  {
7.  Scanner in = new Scanner ( System.in );
8.
9.  int value, value1, sumSoFar = 0, sumofDivisors;
10.
11.
12.  do
13.  {
14.  System.out.print ( "How many numbers would you like to test? " );
15.  value = in.nextInt();
16.  }
17.  while (value < 1);
18.
19.  for (int a = 0; a < value; a++)
20.  {
21.  System.out.printf ( "Please enter a possible perfect number ");
22.  value1 = in.nextInt();
23.  }
24.
25.
26. for ( b = 1; b < value1; b = b + 1 ) {
27.    if ( value1 % b == 0)
28.       sumSoFar = sumSoFar + b;
29. }
30. sumOfDivisors = sumSoFar;
31.
32. if ( value1 == sumOfDivisors )
33.    System.out.println( "The value " + numberToCheck + " is PERFECT" );
34. else
35.    System.out.println( "The value " + numberToCheck + " is NOT PERFECT" );
36.
37.
38. }
39. }
but i get these compile errors

test.java:26: cannot find symbol
symbol : variable b
location: class test
for ( b = 1; b < value1; b = b + 1 ) {
^
test.java:26: cannot find symbol
symbol : variable b
location: class test
for ( b = 1; b < value1; b = b + 1 ) {
^
test.java:26: cannot find symbol
symbol : variable b
location: class test
for ( b = 1; b < value1; b = b + 1 ) {
^
test.java:26: cannot find symbol
symbol : variable b
location: class test
for ( b = 1; b < value1; b = b + 1 ) {
^
test.java:27: cannot find symbol
symbol : variable b
location: class test
if ( value1 % b == 0)
^
test.java:28: cannot find symbol
symbol : variable b
location: class test
sumSoFar = sumSoFar + b;
^
test.java:28: operator + cannot be applied to int,b
sumSoFar = sumSoFar + b;
^
test.java:28: incompatible types
found : <nulltype>
required: int
sumSoFar = sumSoFar + b;
^
test.java:30: cannot find symbol
symbol : variable sumOfDivisors
location: class test
sumOfDivisors = sumSoFar;
^
test.java:32: cannot find symbol
symbol : variable sumOfDivisors
location: class test
if ( value1 == sumOfDivisors )
^
10 errors

anyone know how to fix these
Nov 10 '08 #11
JosAH
11,448 Expert 8TB
Compare your lines #19 (correct) and #26 (where the problem occurs):

Expand|Select|Wrap|Line Numbers
1. for (int a = 0; a < value; a++) // <--- line #19
2.  {
3.  System.out.printf ( "Please enter a possible perfect number ");
4.  value1 = in.nextInt();
5.  }
6.
7.
8. for ( b = 1; b < value1; b = b + 1 ) { // <--- line #26
9.
Do you notice anything?

kind regards,

Jos
Nov 10 '08 #12
jkmyoung
2,057 Expert 2GB
If this is a confusing post, please ignore it. I'm not sure of your mathematics level.

As an alternative (maybe faster?) way of solve your problem, instead of finding all divisors of your number, have you considered prime factoring?
Eg, let your number be n = a^e1 * b^e2 * c^e3 *...

where a, b, c, ... are prime numbers and
e1, e2, e3, ... are the corresponding exponents.

Therefore all factors of the number n are in the form
a^f1 * b^f2 * c^f3* ...
where 0 <= f1 <= e1, 0<=f2<=e2, ....etc

If you sum up all factors
Sum(f1 = 0 to e1) of [Sum (f2 = 0 to e2) of .... [a^f1 * b^f2 * c^f3* ...]...]]
Using the sum of geometric series you get
= [Sum (f2 = 0 to e2) of .... (a^(e1+1) - 1)/ (a-1) *[b^f2 * c^f3* ...]...]]
= (a^(e1+1) - 1)/(a+1) * (b^(e2+1) - 1)/(b+1) * ....
Which for a perfect number = 2 * n

So if you found the prime factorization, you could sum up the numbers much faster by using the proper multiplication.
============
If you really wanted to cheat, you could check if the number is in the form 2^n * (2^(n+1) - 1) where (2^(n+1) - 1) is prime. eg, for:
n=1: (2)(3) = 6
n=2: (4)(7) = 28
n=3: (8)(15) = 120 (15 is not prime)
n=4 (16)(31) = 496 ...
etc.
Nov 10 '08 #13
JosAH
11,448 Expert 8TB
127 is prime but 64*127 == 8128 is not a perfect number. That check is just
a necessary but not a sufficient condition. Finding the factors of a number n
is just a O(n) operation so a predicate for a perfect number just takes O(n)
(neglecting the costs for a division etc)

kind regards,

Jos
Nov 10 '08 #14
nickels
20 ok i have my code here
Expand|Select|Wrap|Line Numbers
1. import java.util.Scanner;
2.
3. public class test
4. {
5. public static void main(String[]args)
6. {
7.
8. Scanner in = new Scanner ( System.in );
9.
10. int sum=0, x=0, value1 = 0, value;
11.
12. do
13.  {
14.  System.out.print ( "How many numbers would you like to test? " );
15.  value = in.nextInt();
16.  }
17.  while (value < 1);
18.
19. for (int a = 0; a < value; a++)
20.  {
21.  System.out.printf ( "Please enter a possible perfect number ");
22.  value1 = in.nextInt();
23.
24. public static testPerfect()
25. {
26. for(int factor=1; factor < value1; factor++){
27.
28. x = value1 % factor;
29.
30. if(x==0)
31.
32. sum=sum+factor;}
33. }
34.
35. public static printFactors()
36. {
37.
38. if(sum!=value1)
39. {
40. System.out.printf("%d:NOT PERFECT", value1);
41. }
42.
43. if(sum==value1)
44. {
45. System.out.printf ( "%d: ", value1);
46.
47.
48. for(int factor=1;factor<value1;factor++)
49. {
50. x=value1%factor;
51. if(x==0)
52. System.out.printf("%d ", factor);
53. }
54. }
55. }
56. System.out.print("\n");
57. sum=0;
58. }
59. }
60. }
however i need 2 public static method

first one is testPerfect() and it goes around this part of my code

Expand|Select|Wrap|Line Numbers
1. for(int factor=1; factor < value1; factor++){
2.
3. x = value1 % factor;
4.
5. if(x==0)
6.
7. sum=sum+factor;}
the other method is to be called printFactors() and its to go around
Expand|Select|Wrap|Line Numbers
1. if(sum!=value1)
2. {
3. System.out.printf("%d:NOT PERFECT", value1);
4. }
5.
6. if(sum==value1)
7. {
8. System.out.printf ( "%d: ", value1);
9.
10.
11. for(int factor=1;factor<value1;factor++)
12. {
13. x=value1%factor;
14. if(x==0)
15. System.out.printf("%d ", factor);
16.
17. }
18. }
19. System.out.print("\n");
20. sum=0;
im not exactly sure how to do this every time i try i get compile errors, anyone know how to do this?
Nov 11 '08 #15
JosAH
11,448 Expert 8TB
At line #24 you're trying to define a (static) method inside the body of a for loop
(started at line #19). It is not allowed to define a method inside the body of
another method.

kind regards,

Jos

ps. Please do read that tutorial because you're not up to the basics yet.
Nov 11 '08 #16
jkmyoung
2,057 Expert 2GB
127 is prime but 64*127 == 8128 is not a perfect number.
? I'm pretty sure 8128 is a perfect number.
Nov 13 '08 #17
JosAH
11,448 Expert 8TB
? I'm pretty sure 8128 is a perfect number.
You're right; I manually miscalculated it

kind regards,

Jos
Nov 13 '08 #18

 8 posts views Thread by | last post: by 19 posts views Thread by gk245 | last post: by 1 post views Thread by psalmprax | last post: by 4 posts views Thread by The 1 | last post: by 4 posts views Thread by Sheepman | last post: by 2 posts views Thread by jayoh12 | last post: by 2 posts views Thread by wkid87 | last post: by 12 posts views Thread by hipfunkyjive | last post: by 4 posts views Thread by Busgosu | last post: by reply views Thread by ioana budai | last post: by reply views Thread by zhoujie | last post: by reply views Thread by SioSio | last post: by 2 posts views Thread by mgbsher | last post: by reply views Thread by NeoPa | last post: by reply views Thread by dDule | last post: by 1 post views Thread by Marylou17 | last post: by 3 posts views Thread by mopufo | last post: by 1 post views Thread by Darius9023 | last post: by