Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number.

The smallest pair of amicable numbers is (220, 284). They are amicable because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220.

The first ten amicable pairs are:

(220, 284), (1184, 1210),

(2620, 2924) (5020, 5564),

(6232, 6368) (10744, 10856),

(12285, 14595) (17296, 18416),

(63020, 76084) (66928, 66992).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.*; public class Amicable { int sum(int n) { int i, s=0; for(i=1;i<=n/2;i++) { if(n%i==0) //Finding factors s = s+i; } return s; } void print(int n) { int i, s1, s2; for(i=1;i<=n;i++) { s1 = sum(i); //The sum of factors of i s2 = sum(s1); //Finding the sum of factors of s1 /*If i is equal to sum of factors of s1 then i and s1 are amicable * also to prevent the same two numbers from repeating the second condition is used. */ if(i==s2&&i<s1) System.out.println(i+", "+s1); } } public static void main() { Scanner sc = new Scanner(System.in); Amicable ob = new Amicable(); int x; System.out.println("Enter a number: "); x = sc.nextInt(); System.out.println("All amicable numbers between 1 and "+x+" are: "); ob.print(x); } } |

Output:

## Leave a Reply