Hi i with this perfect number program Please help

  • Thread starter killerkhangg
  • Start date
  • Tags
    Hi Program
In summary: System.out.print(factor + "+"); }else{ System.out.println("="+ i); } } } Sum=0; }ynthesis(){}usterity(){} //End of method} }
  • #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;
}
}

}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2


killerkhangg said:
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.
That makes no sense at all. What you meant to say was
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.​
There's a big difference between what you wrote and what I wrote. If you want help, you need to get the question right first.
 
  • #3


sorry that's what my teacher gave me. i was really confuse on how to write a program putting that equation on
 
Last edited by a moderator:
  • #4


killerkhangg said:

Homework Statement



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.


Homework Equations





The Attempt at a Solution


this is what i have so far, please tell me what i am doing wrong
When you post code here at Physics Forums, please put your code between [noparse]
Code:
 ...
[/noparse] tags. I have done that for you.

The problem description says to use the long type. Your variables are of type int.

Also, indenting your code makes it easier to read. The code you posted was not indented at all, and this makes it more difficult to see what is included in loops and so on.

Instead of asking us to tell you what you are doing wrong, tell us what the program is doing that is incorrect.
killerkhangg said:
Code:
/**
* @(#)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;
      }
    }
}
 
  • #5


This program that i wrote only find the first 4 perfect number and i am suppose to find the first 6 perfect number. And my teacher expect me to put those two equations above on the program which i really don't know how that works. Thank you very much. It would really help if you guys can tell me
 
  • #6
Did you see what DrGreg wrote? Your program should calculate 2n - 1 for various values of n, check each to see if it's a prime. If so, then 2n - 1(2n - 1) will be a perfect number.

As I noted in my earlier post, you should be using longs, not ints.
 
  • #7
I know i see what he wrote. I switch the variables to long but how would you put it into code. I'm guessing Math.squrt.2n-1? i missed class for 1 week due to intestine problems so i didn't get these notes. Sorry
 
  • #8
killerkhangg said:
I know i see what he wrote. I switch the variables to long but how would you put it into code. I'm guessing Math.squrt.2n-1?
?
The name of the function is sqrt, but you wouldn't use it to calculate powers of 2. You can write a function that calculates powers of 2, as in the following:
Code:
long powerOfTwo(int exponent)
{
  long result = 1;
  for (int i = 0; i < exponent; i++)
  {
     result *= 2;
  }
  return result;
}


killerkhangg said:
i missed class for 1 week due to intestine problems so i didn't get these notes. Sorry
 

Related to Hi i with this perfect number program Please help

1. What is a perfect number program?

A perfect number program is a computer program that calculates and identifies perfect numbers. A perfect number is a positive integer that is equal to the sum of its proper divisors (divisors excluding the number itself). For example, the first perfect number is 6, because 1+2+3=6.

2. How does a perfect number program work?

A perfect number program works by looping through all the numbers and checking if they are perfect numbers. To check if a number is perfect, the program calculates the sum of its proper divisors and compares it to the number itself. If they are equal, then the number is a perfect number.

3. What is the purpose of a perfect number program?

The purpose of a perfect number program is to identify and calculate perfect numbers. Perfect numbers have been studied in mathematics for centuries and have many interesting properties. By creating a program, we can easily explore and discover these properties.

4. Is a perfect number program useful?

While a perfect number program may not have practical applications, it is a great tool for learning and understanding mathematical concepts. It also serves as a fun programming exercise to improve problem-solving skills.

5. Can I contribute to a perfect number program?

Yes, you can contribute to a perfect number program by suggesting improvements, reporting bugs, or even writing code. Many open-source perfect number programs are available on platforms like GitHub, and they welcome contributions from the community.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top