Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Amicable Numbers

807569May 20 2006 — edited May 20 2006
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;

      }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 17 2006
Added on May 20 2006
1 comment
360 views