- #1
killerkhangg
- 4
- 0
Perfect Number Program Java Methods
A perfect number is a positive integer equal to the sum of all its divisors (including 1 but excluding the number itself). For example:
28 = 1 + 2 + 4 + 7 + 14
According to Euclid, any solution to (2n-1) that results in a prime number will indicate that (2n-1)(2n-1) will be a perfect number. Thus, since 23-1 = 7, which is prime, 23-1(23-1) = 28, which is a perfect number. All even perfect numbers can be found in this fashion.
Write a program that will find the first n even perfect numbers, by testing the first formula to find primes, then using the second formula to find perfect numbers when the first reveals a prime answer. Use a long type rather than an int type to hold your responses. Find the first six perfect numbers.
This is what i got so far. please tell me what I am doing wrong
/**
* @(#)perfectnumber.java
*
*
* @author
* @version 1.00 2011/11/27
*/
import java.util.Scanner;
public class perfectnumber {
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
int Number;
int Sum=0;
System.out.println("Place an interger in the range of 2 to 100");
Number=input.nextInt();
if(Number<2 || Number>100){
System.out.println("Number does not match the range!");
}
else
for(int i=2; i<Number; i++)
{
for(int j=1; j<i; j++){
if(i%j==0) //i mod j=0
Sum+=j; //part of the sum
}
if(Sum==i){
System.out.println("Perfect Number:" + i);
for(int factor=1; factor<1; factor++){
if(i%factor==0)
System.out.print(factor + "+");
}
System.out.println("="+ i);
}
Sum=0;
}
}
}
A perfect number is a positive integer equal to the sum of all its divisors (including 1 but excluding the number itself). For example:
28 = 1 + 2 + 4 + 7 + 14
According to Euclid, any solution to (2n-1) that results in a prime number will indicate that (2n-1)(2n-1) will be a perfect number. Thus, since 23-1 = 7, which is prime, 23-1(23-1) = 28, which is a perfect number. All even perfect numbers can be found in this fashion.
Write a program that will find the first n even perfect numbers, by testing the first formula to find primes, then using the second formula to find perfect numbers when the first reveals a prime answer. Use a long type rather than an int type to hold your responses. Find the first six perfect numbers.
This is what i got so far. please tell me what I am doing wrong
/**
* @(#)perfectnumber.java
*
*
* @author
* @version 1.00 2011/11/27
*/
import java.util.Scanner;
public class perfectnumber {
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
int Number;
int Sum=0;
System.out.println("Place an interger in the range of 2 to 100");
Number=input.nextInt();
if(Number<2 || Number>100){
System.out.println("Number does not match the range!");
}
else
for(int i=2; i<Number; i++)
{
for(int j=1; j<i; j++){
if(i%j==0) //i mod j=0
Sum+=j; //part of the sum
}
if(Sum==i){
System.out.println("Perfect Number:" + i);
for(int factor=1; factor<1; factor++){
if(i%factor==0)
System.out.print(factor + "+");
}
System.out.println("="+ i);
}
Sum=0;
}
}
}
Last edited by a moderator: