Ibrewster
Ibrewster

Reputation: 187

Perfect Number program java

I am supposed to create a perfect number class using the following pseudocode:

For i from 2 to “very large”,
    For j from 2 to √i,
         if (j evenly divides i),
              accumulate the sum j and i/j
    if √i is an integer
         subtract √i ... you added it twice
    if the sum of divisors == i
         Print the number ... it’s perfect!

So here is my version. It runs, but it doesn't do what I want at all. It just runs and produces nothing as an output. Can someone tell me what is wrong with my program? It's bothering me so much.

import java.util.Scanner;

public class PerfectNumber {

public static void main(String[] args) {
  double sum = 0
  double newsum = 0;
  for (int i = 2; i < 1000000; i++) {
     for (int j = 2; i<Math.sqrt(i); j++){
        if (i%j==0){
           sum = j + (i%j);

        }
        if (Math.sqrt(i)==(int)i){ 
        newsum = sum - Math.sqrt(i);
        }   
        if (sum == 0) {
        System.out.println(sum + "is a perfect number");
        }

}
}
}
}

Upvotes: 1

Views: 2199

Answers (7)

nam4lpha
nam4lpha

Reputation: 79

Thanks for watched

  public boolean testPerfect(int n){
    int i=1;
    int sum=0;
    while(i<n){
        if(n%i==0)
        {
            sum+=i++;
        }
        else{
        i++;}
    }
    if (sum==n){
        return true;
    }
    return false;
}

Upvotes: 0

James
James

Reputation: 22247

According to the pseudocode you want to move the second and third if test outside of the inner loop

for (int i = 2; i < 1000000; i++) {
    double iroot = Math.sqrt(i);
    int sum = 1;
    for (int j = 2; j <= iroot; j++){
        if (i % j == 0){
            sum = sum + j + i / j;
        }
    }
    if (iroot == (int) iroot) {
        sum = sum - iroot;
    }
    if (sum == i) {
        System.out.println(sum + "is a perfect number");
    }
}

Upvotes: 0

raja Shekar
raja Shekar

Reputation: 21

Here is the simplest and easiest form you can write a program for perfect number....this code gives perfect number within 25 ...you can change as you want

import java.util.Scanner;
public class PerfectNumber {
    public static void main(String[] args) {
        int n,i,j,count=0;

        for(i=2;i<=25;i++) {
            for(j=1;j<=i;j++) {
                if(i%j ==0)    /*count increments if a reminder zero*/ {
                    count++;
                }
            }
            /*since a perfect number is divided only by 1 and itself
              if the count is 2 then its a prime number...*/

            if(count==2)
                System.out.println(i);
            count=0;
        }
        return 0;
    }
}

Upvotes: 0

Tomek
Tomek

Reputation: 543

Few mistakes according to the algorithm:

  1. sum = j + (i%j); should be changed to sum = j + (i/j);

  2. This piece:

    if (Math.sqrt(i)==(int)i){ 
        newsum = sum - Math.sqrt(i);
    }   
    if (sum == 0) {
        System.out.println(sum + "is a prime number");
    }
    

Should be under upper "for"

  1. Math.sqrt(i)==(int)i would never be true unless i is 1. If you want to check this that way you should write Math.sqrt(i)==((int) Math.sqrt(i))

There are much more errors, the simplest way to do it is:

double sum = 0;
for (int i = 1; i <= 10000; i++) {
    for (int j = 1; j < i; j++) {
        if (i % j == 0) {
            sum += j;
        }
    }
    if (i == sum) {
        System.out.println(sum + " is a prime number");
    }
    sum = 0;
}

Upvotes: 2

amk
amk

Reputation: 70

for(n=1;n<=number;n++){ //calculates the sum of the number.
 int i=1;
 int sum = 0;
  while(i<n){
    if(n%i==0)
         sum+=i;
        i++;
  }
        if(sum==n){ //if the sum is equal to its sum :
          System.out.print(n+": ");
          for (int j = 1;j<n;j++){
              if(n%j==0){
              System.out.print(j+" ");
          }
          }
          System.out.println();
      }
  }

Upvotes: 0

AlbertFG
AlbertFG

Reputation: 157

public static void main(String[] args){
    int min = 2; 
    int max = 1000000;
    int sum = 0;
    for (; min <= max; min++,sum = 0) { 
        for (int e = 1; e < min; e++)
            sum += ((min % e) == 0) ? e : 0;

        if (sum == min){           
            System.out.println(sum);
        }          
    }      
}

Upvotes: 1

rgettman
rgettman

Reputation: 178263

Your code contains several mistakes. Here is the corrected code, commented with the changes.

// newsum isn't needed; declare sum to be int to avoid floating-point errors
int sum = 0;
for (int i = 2; i < 1000000; i++) {
    // Start with 1; every natural number has 1 as a factor.
    sum = 1;
    // Test if j, not i, is less than the square root of i.
    for (int j = 2; j <= Math.sqrt(i); j++){
        if (i % j == 0){
            // Add to sum; don't replace sum.  Use i / j instead of i % j.
            sum = sum + j + (i / j);
            // Move test inside this if; test if j is square root of i
            if (j*j == i){
                // I used j because we know it's the square root already.
                sum = sum - j;
            }
        }
        // Move print outside of inner for loop to prevent multiple 
        // printings of a number.
        // Test if sum equals the number being tested, not 0.
        if (sum == i) {
             // Space before is
             System.out.println(sum + " is a perfect number");
        }
    }
}

Output:

6 is a perfect number
28 is a perfect number
496 is a perfect number
8128 is a perfect number

Upvotes: 1

Related Questions