# Boolean help, again.

• 04-25-2012, 09:48 PM
Zigster
Boolean help, again.
My Code:
Code:

```public class Problem021 {                 public static void main(String[] args){                                 int a, b;                 b=0;                 a=220;                 if (AmiNum(a, PropDiv(a))){                         System.out.println(a);                         b+=a;                 }                         }                 public static int PropDiv(int x){                 int a, b;                 b = 0;                 for (a = 1; a < x; a++){                         if (x % a == 0){                                 b += a;                         }                 }                 return b;         }         public static boolean AmiNum(int x, int y){                 if (PropDiv(x)==PropDiv(y)){                         return true;                 }                 else{                         return false;                 }         } }```
This is what I'm trying to solve:
Quote:

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.
I'm testing with a=220, since they give you the answer to sum of the "proper divisors", and PropDiv method gives me the sum of the proper divisions successfully; a=220 PropDiv(a) gives me 284.

So my PropDiv method works. But if I run this, it prints nothing, even though PropDiv(220) = 284 and PropDiv(284) = 220.

What am I doing wrong?

Cheers!
• 04-25-2012, 10:08 PM
sunde887
Re: Boolean help, again.
If you call amINum on a and propDiv(a) you will get false.

Let a = 220, then propDiv(a) = 284.

So, when you apply amINum(a, propdiv(a)), you are comparing propDiv(a) = 284 and propDiv(propDiv(a)) = 220. Since 220 != 284, amINum returns false.
• 04-25-2012, 10:14 PM
jlczuk
Re: Boolean help, again.
So in main, you have AmiNum(a, PropDiv(a)). Therefore inside AmiNum you have a comparison that is equivalent to PropDiv(a)==PropDiv(PropDiv(a)). Is that really what you intended?

Also, When you use an if statement to return true if the things being evaluated are true, you can simply return the things being evaluated. Wow, does that sound confusing...here's the example based on your AmiNum:
Code:

`return PropDiv(x)==PropDiv(y);`
What you used isn't wrong, this is just a way to do it shorthand that is just as easy to read. If you were trying to print results for debugging, then your original way is the way to go.
• 04-25-2012, 10:20 PM
Zigster
Re: Boolean help, again.
Quote:

Originally Posted by sunde887
If you call amINum on a and propDiv(a) you will get false.

Let a = 220, then propDiv(a) = 284.

So, when you apply amINum(a, propdiv(a)), you are comparing propDiv(a) = 284 and propDiv(propDiv(a)) = 220. Since 220 != 284, amINum returns false.

Thanks! I didn't know you could write propDiv(propDiv(a)), so I don't even need the boolean! Awesomeness.