-5

Have to create program that list all perfect number( sum of factors = number ) 1 - 1000.

  • this is for a java class, need to only use "for" loops

I have checked my code 100 times and getting no output, I am missing a logical error somewhere, could someone help me out?

public static void main(String[] args)
{
  // variables
  int total = 0;
  final int LIMIT = 1000;

  // for loop to test all numbers 1-1000
  for(int i = 1; i <= LIMIT; i++)
  {
    // if statement
    if((i != 1) && (total == i - 1))
    {
      // prints perfect number
      System.out.println((i - 1) + " is a perfect number");
      // resets total value
      total = 0;
    }
    // gets and add factors as total
    for(int divider = 1; divider < i; divider++)
    {
      if((i % divider) == 0)
      {
        total += divider;
      }
    }
  }
}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
user1726845
  • 109
  • 3
  • 10

6 Answers6

4

Your big problem is that you only reset total if you find a perfect number. If you don't find a perfect number, you continue adding divisors for the next number to the old total. You need to start fresh for every i.

Rearranging your program in the following way should help:

public static void main(String[] args) {
    final int LIMIT = 1000;
    for (int i = 0; i <= LIMIT; i++) {
        // Declare total here, inside the loop, so values from previous
        // iterations are discarded.
        int total = 0;
        for (/* your code here */) {
            // add up divisors
            // your code here
        }
        // compare to i, rather than always computing the total for the
        // previous number and comparing to that.
        if (/* your code here */) {
            // print output
            // your code here
        }
    }
}
user2357112
  • 260,549
  • 28
  • 431
  • 505
2

You should move total = 0; outside of your if statement. Your total is adding up and never being reset.

Uxonith
  • 1,602
  • 1
  • 13
  • 16
  • It's not just that, look at where he's adding up `total` and where he's checking it. – Dennis Meng Jun 09 '14 at 21:05
  • He's checking it in the following iteration. I don't know why he's doing that, but it should still function as expected, possibly with the exception of the loop ending one iteration prematurely. – Uxonith Jun 09 '14 at 21:06
  • Ah, yeah I see what you mean. Though I'd imagine checking the given iteration would make it more understandable. – Dennis Meng Jun 09 '14 at 21:08
  • I couldn't agree more :) I was being a bit lazy and then @user2357112 decided to post a nice answer. I would imagine he could have pasted the OP's code into those comments though :\ – Uxonith Jun 09 '14 at 21:09
  • Sure. I'm assuming he didn't so that the OP would fill in the blank rather than just copy-pasting what he put down. – Dennis Meng Jun 09 '14 at 21:11
0

I reccomend to split your algorithm to different parts, so you can focus on smaller tasks at time.

  1. Find factors of number i. A method that gets a number and return an arrays of it's factors.
  2. Sum factors together. A simple method that takes an array and returns it's the sum.
  3. Main loop: just check if sum(factors(i)) == i

You can start with the more easy ones (Hint: 2 and 3) and then maybe search for some not-totally-inefficient ways to implement 1

kajacx
  • 12,361
  • 5
  • 43
  • 70
  • There's a good chance he's just learning to program and is just now beginning to learn about loops and hasn't gotten to creating methods yet :) – Uxonith Jun 09 '14 at 21:13
0

(total == i - 1) will never match when you start with total=0 and i>1.

0

You were close. Just need to re-evaluate your logic a bit.

public static void main(String[] args) {
        // variables
        int total = 0;
        final int LIMIT = 1000;

        // for loop to test all numbers 1-1000
        for (int i = 1; i <= LIMIT; i++) {

            // gets and add factors first
            for (int divider = 1; divider < i; divider++) {
                if ((i % divider) == 0) {
                    total += divider;
                }

            }

            // then check if sum == number
            // also just print i instead of - 1
            if ((i != 1) && (total == i)) {
                // prints perfect number
                System.out.println((i) + " is a perfect number");

            }

            // alway reset when we are done with a number
            total = 0;

        }

    }
binary_assemble
  • 394
  • 3
  • 17
0
 for(int i=1;i<1000;i++)
    {
        int k=0;

        for(int j=1;j<i;j++)
        {
            if(i % j==0)
            {   
                k+=j;
            }

        }
        if(k==i)
        {
            System.out.println(k);
        }
    }