The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


amicable pair

Posted by Anand Rajan on February 20, 2002 at 10:56 AM


here u go with some kind of a pseudo code. I guess rajeev's solution would work,except that it just looks for that number alone and not for all numbers in the range.

To make it simple :

// increament by 1 if inputvalue is odd
//create a hashtable
for(int number=1;number<= inputValue;i++) {

for int(i=1;number/2;i++){
if(number % i == 0)
amicabletotal += i;
} // i loop
amicabletotal += number;
amicabletotal = 0; //reset total for the next iteration
} // number loop

// loop through hashtable for each key(1).
// Get the value for the key(1).This is amicablevalue1
// if (a key(2) was present with the amicablevalue1 retrieved){
// Get the value of the key(2).This is amicablevalue2
// if ( amicablevalue2 = key1) u got a hit
//end of loop

// U are never bound to get a duplication in this because the keys are unique...

> I have this problem and i don't really know how to start it. Any basic ideas would be appreciated. I don't need the program just an idea of how I can do this. Thankz

> Amicable Number Problem. Given any integer n, we can determine �the sum of its divisors.� For example, 30 has divisors 1, 2, 3, 5, 6, 10, and 15, the sum of these divisors is 42. Or consider 35, its divisors are 1, 5, and 7, and their sum is 13.

> Now consider the numbers 220 and 284. They have a very interesting property. If you sum of all of the divisors of 220, you get 284. Conversely if you sum all of the divisors of 284 then you get 220. Two numbers x, y are called an �amicable pair� if the sum of the divisors of x is y, and at the same time, the sum of the divisors of y is x.

> The program you will write, named, begins by requesting an integer n be input from the user. It then computes and prints out all amicable pairs x, y where at least one of the numbers x or y is smaller than n. The program terminates after it has completed the computation. The program should not duplicate any values.

> Here is a sample run:

> Input value to search for Amicable numbers: 10000

> Amicable pairs found:
> (220, 284)
> (1184, 1210)
> (2620, 2924)
> (5020, 5564)
> (6232, 6368)


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us