The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

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.

Message:

How to write a program for amicable pair

Posted by Rajeev Mutalik on February 13, 2002 at 1:47 AM

Hi Chris,
I have written a program in java which will take a number as input from the keyboard and finds the number which will form the amicable pair or else displays message saying :
"It has got no number to form amicable pair"

/*********************************************************************************************
Program ID : D:\Rajeev\TrialProgs\Amicable.java
Programmed By : Rajeev Mutalik
Date : Wednesday, February 13, 2002
Purpose : To find a number for a given number by the user which makes it an
amicable pair.
*********************************************************************************************/
import java.io.*;

public class Amicable
{
public static void main(String args[])
{
int amic1=0, amic2=0;
String ch=null;
try
{
do
{
BufferedReader br = null;
try//outer try
{
System.out.println("Please Enter a Number:");
br = new BufferedReader(new InputStreamReader(System.in));
String am1 = br.readLine();
//String am2 = br.readLine();
try//inner try
{
amic1 = Integer.parseInt(am1);
}//End of inner try
catch(NumberFormatException nfe)

{
System.out.println("Invalid Number...");
System.exit(0);
}
int sumamic1 =0,sumamic2=0;

for(int i=1;i<=amic1/2;i++)
{
if(amic1%i==0)
sumamic1+=i;
}
amic2 = sumamic1;
boolean amicflag=false;
for(int k=1;k<=sumamic1/2;k++)
{
if(sumamic1%k==0)
sumamic2+=k;
if((sumamic1==amic2)&&(sumamic2==amic1))
{
System.out.println("Amicable Pair is:"+amic1+":"+amic2);
amicflag=true;
}
}
if(!amicflag)
System.out.println("It has got no number to form amicable pair");
}//End of outer try
catch(Exception e)
{
System.out.println("Error...");
}
System.out.println("Do u want to try more (Yes=y)");
ch = br.readLine();
}while(ch.equalsIgnoreCase("Y"));
}//End of first try
catch(Exception e)
{
System.out.println("Error...");
System.exit(0);
}
}//End of main
}


> 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 Amicable.java, 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)






Replies:

Sponsored Links



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