Results 1 to 4 of 4
  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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;
    		}
    	}
    }
    This is what I'm trying to solve:
    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!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

  3. #3
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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);
    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.

  4. #4
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Boolean help, again.

    Quote Originally Posted by sunde887 View Post
    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.

Similar Threads

  1. Boolean
    By must0995 in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 10:18 PM
  2. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  3. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  4. boolean even or odd?
    By shazakala in forum New To Java
    Replies: 3
    Last Post: 04-17-2011, 08:02 AM
  5. use boolean as 0 or 1
    By joost_m in forum New To Java
    Replies: 10
    Last Post: 04-13-2010, 11:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •