473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lost on Perfect Numbers program

20 New Member
-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 9293
JosAH
11,448 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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 New Member
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 New Member
There is a fairly comprehensive old thread talking about coding perfect numbers in java at http://bytes.com/forum/thread17338.html

To learn more about perfect numbers in general check out
http://mathworld.wolfram.com/PerfectNumber.html

To answer your last post you can get user input with
Expand|Select|Wrap|Line Numbers
  1. String text  = in.readLine();
  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 New Member
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 New Member
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 New Member
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 Recognized Expert MVP
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 Recognized Expert Top Contributor
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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 Recognized Expert Top Contributor
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 Recognized Expert MVP
? I'm pretty sure 8128 is a perfect number.
You're right; I manually miscalculated it

kind regards,

Jos
Nov 13 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

8
14705
by: | last post by:
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...
19
4411
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
1
3485
by: psalmprax | last post by:
who can write a C-program on perfect numbers from 1 to 1000?
4
8089
by: The 1 | last post by:
Q-1 Can sm1 suggest a program to check whether a given no is perfect square or not?? Q-2 Also sm1 suggest a program to check whether a given no is perfect power or not?? * PEfect power is a no...
4
1535
Sheepman
by: Sheepman | last post by:
Could someone please walk me through the following program. It's only 15 lines. My comments and questions are below dealing mostly with the array and "for" loops. Hopefully my questions make sense as...
2
2372
by: jayoh12 | last post by:
create a program that will let the user to enter a number..determine if the number is perfect or not... e.g 6 is perfect # bec. its factors are 1,2,3 and 6. 1+2+3+6-6=6 6=6
2
12668
by: wkid87 | last post by:
Using C# Microsft Visual C#, Console Application I'm needing help with the user entering in a number to tell if it perfect or not. I have the perfect number figure out. Here in the instruction: ...
12
2428
by: hipfunkyjive | last post by:
Basically my program has to find perfect numbers in a range 1- 1000 it works , kinda but it displays a few numbers that are not perfect for example the first Perfect numbers are : 6 28 496 my...
4
4259
by: Busgosu | last post by:
i dont know how to upload in a way that you can see it clearly.. so im just going to copy and paste what i have so far... sorry. :S #include <iostream> using namespace std; #include <cmath>...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.