Given any number n, we can determine the sum of its divisors. Like 30 has divisors 1,2,3,5,6,10 and 15, the sum of these divisors is 42.
Pretty much, if you sum all of the divisors of say, 220, you get 284. Conversely, if you sum all of the divisors of 284 then you get 220. Two numbers x, y are an amicable pair if the sum of the divisors of x is y, and at the same time, the sum of divisors of y is x.
As part of the program, I am required to write a static function, that accepts an integer as input. It returns the sum of the divisors of that integer.
this is what I have so far. Am I on the right track? What do I need to do?
-Thanx
import java.util.Scanner;
class Amicable {
public static void main (String[] args) {
Scanner input=new Scanner (System.in);
System.out.print("Input value to search for Amicable numbers: ");
x=input.nextInt();
System.out.println();
int y = sumOfDivisors(x);
int z = sumOfDivisors(y);
if (x == z && x != y)
{
if (x < y)
{
System.out.println("Amicable pairs found:");
System.out.println(x, y);
}
}
}
}
public static int sumOfDivisors(int x) {
{
int sum = 0;
int temp;
for (int i = 1; i <= x/2; i++)
{
if (x % i == 0)
sum += i;
}
return sum;
}
}