2

So my assignment is to find all the "magic numbers" within a range of numbers (input by the user). A magic number is a number whose factors (except itself) sums up to that number. So 6 would be a magic number because it's factors besides itself are 1,2 and 3 which sum up to 6. I have stared at this code for some time now and cannot figure out for the life of me why it won't print out the magic number. Any help would be appreciated.

public class MagicNumber {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.print("What is the top of the range?");
    int range = IO.readInt();
    if (range <= 0 ) {
        IO.reportBadInput();
    }
    int sumOfFactors = 0;
    for (int i = 1 ; i <= range ; i++) {
        for (int m = 1 ; m < i; m++) {
            if (i % m == 0) {
                sumOfFactors = sumOfFactors + m;
            }
            if (sumOfFactors == i) {
                System.out.println(i);
                    }
            }
        }
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
biohax2015
  • 281
  • 2
  • 6
  • 15
  • 7
    Don't stare at it, ***debug*** it. Either run it through a debugger or sprinkle generously with println statements to find out exactly why it's behaving as it's behaving. – Hovercraft Full Of Eels Oct 21 '12 at 02:57
  • Just out of curiosity, is this property what is usually called [perfect numbers](http://en.wikipedia.org/wiki/Perfect_number)? For instance, the prime factors of 28 are 2 (multiplicity 2) and 7, but the proper divisors of 28 are 1, 2, 4, 7, and 14 (which sum to 28). Is 28 a "magic number"? – Ted Hopp Oct 21 '12 at 03:01
  • One thing I can see is that you are not resetting the `sumOfFactors ` back to zero. – Bhesh Gurung Oct 21 '12 at 03:03
  • Tedd- yes this is the same thing as perfect numbers. – biohax2015 Oct 21 '12 at 03:11

5 Answers5

2

You are testing whether sumOfFactors == i while you are still summing factors. You need to move that outside the m loop. Then you need to set sumOfFactors to 0 before starting the m loop each time through the i loop, not just once at the start of the looping.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

try this:

 public class MagicNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.print("What is the top of the range?");
        int range = IO.readInt();
        if (range <= 0 ) {
            IO.reportBadInput();
        }

        for (int i = 1 ; i <= range ; i++) {
   int sumOfFactors = 0;
            for (int m = 1 ; m < i; m++) {
                if (i % m == 0) {
                    sumOfFactors = sumOfFactors + m;
                }
    }
                if (sumOfFactors == i) {
                    System.out.println(i);
                        }

            }
        }
    }
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • I don't think so, because I want it to print out every magic number WITHIN that range. So for every number between one and range, I'd want to check if it's a magic number and then print it out. – biohax2015 Oct 21 '12 at 03:06
  • The OP is trying to print the magic numbers (some `m`s) which are `<= range`. – Bhesh Gurung Oct 21 '12 at 03:07
0

I I think you need to initialize int sumOfFactors = 0; within first for loop and move the second if, out of second for loops since you need to compare the sum of total factors against the current number as below:

for (int i = 1 ; i <= range ; i++) {
    int sumOfFactors = 0;  //<--Moved inside
    for (int m = 1 ; m < i; m++) {
        if (i % m == 0) {
            sumOfFactors = sumOfFactors + m;
        }
     }
     if (sumOfFactors == i) {  // <-- moveed out of second loop
       System.out.println(i);
     }
}
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

Just referring to this Magic number program http://getprogramcode.com/2013/11/java-program-to-check-for-a-magic-number/ and thought of writing this.

public class HelloWorld{

     public static void main(String []args){
        static int number=5432;
        while(number>9){
            int tot = calculateSum(number);
            System.out.println(tot);
        }
     }

     private static int calculateSum(int num){
         int sum = 0;
         while (num > 0) {
            int a = num % 10;
            sum = sum+ a;
            num = num / 10;
        }
         number = sum;
         return sum;
     }
}
Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
0
public class Magic {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int b =778393;
        int e=0,c;

        while (b>=10) {
            while (b>0) {
                c=b%10;
                b=b/10;
                e=e+c;
            }
            b=e;
            e=0;
            System.out.println(b);
        }
        if (b==1) {
            System.out.println("It is a magic no."+b);
        } else {
            System.out.println("Not"+b);
        }
    }
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82