Results 1 to 4 of 4
Thread: Boolean help, again.
- 04-25-2012, 09:48 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Boolean help, again.
My Code:
This is what I'm trying to solve: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; } } }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.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!
- 04-25-2012, 10:08 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #3
Senior 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:
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.Java Code:return PropDiv(x)==PropDiv(y);
- 04-25-2012, 10:20 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Similar Threads
-
Boolean
By must0995 in forum New To JavaReplies: 1Last Post: 04-13-2012, 10:18 PM -
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 12:01 AM -
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05-05-2011, 09:04 AM -
boolean even or odd?
By shazakala in forum New To JavaReplies: 3Last Post: 04-17-2011, 08:02 AM -
use boolean as 0 or 1
By joost_m in forum New To JavaReplies: 10Last Post: 04-13-2010, 11:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks