Results 1 to 4 of 4
Thread: Boolean help, again.
 04252012, 09:48 PM #1Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Boolean help, again.
My Code:
Java 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; } } }
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.
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!
 04252012, 10:08 PM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,068
 Blog Entries
 3
 Rep Power
 14
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.
 04252012, 10:14 PM #3Senior Member
 Join Date
 Apr 2012
 Location
 New York State of Confusion, USA
 Posts
 137
 Blog Entries
 1
 Rep Power
 0
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:
Java Code:return PropDiv(x)==PropDiv(y);
 04252012, 10:20 PM #4Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Similar Threads

Boolean
By must0995 in forum New To JavaReplies: 1Last Post: 04132012, 10:18 PM 
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09132011, 12:01 AM 
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05052011, 09:04 AM 
boolean even or odd?
By shazakala in forum New To JavaReplies: 3Last Post: 04172011, 08:02 AM 
use boolean as 0 or 1
By joost_m in forum New To JavaReplies: 10Last Post: 04132010, 11:22 AM
Bookmarks