-
Notifications
You must be signed in to change notification settings - Fork 61
/
AmicablePair.java
80 lines (73 loc) · 1.67 KB
/
AmicablePair.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.java.numbers;
import java.util.ArrayList;
/* Amicable Pair
*
* Amicable numbers are two different numbers so related that the
* sum of the proper divisors of each is equal to the other number.
*
* A proper divisor of a number is a positive factor of that number
* other than the number itself
*
* Proper divisors of 20 is 1, 2, 4, 5, 10
* 20 is the proper divisor
*
* say a = 220, b = 284
*
* Proper divisors of a = 220 are
* 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110
* sum = 284
*
* Proper divisors of b = 284 are
* 1, 2, 4, 71, 142
* sum = 220
*
* So a & b are Amicable Pair
* Other Amicable Pairs are
* (1184, 1210)
* (2620, 2924)
* (5020, 5564)
* (6232, 6368)
* (10744, 10856)
* (12285, 14595)
* (17296, 18416)
* (63020, 76084)
* (66928, 66992)
*/
public class AmicablePair {
public static void main(String[] args) {
int a = 5020;
int b = 5564;
ArrayList<Integer> aFactors = factors(a);
ArrayList<Integer> bFactors = factors(b);
int aSum = 0, bSum = 0;
for(int i=0;i<aFactors.size();i++)
aSum += aFactors.get(i);
for(int i=0;i<bFactors.size();i++)
bSum += bFactors.get(i);
if(aSum == b && a == bSum)
System.out.println(a+" & "+b+" are Amicable Pair");
else
System.out.println(a+" & "+b+" are NOT Amicable Pair");
}
private static ArrayList<Integer> factors(int num){
ArrayList<Integer> factors = new ArrayList<>();
factors.add(1);
for(int i=2;i<num;i++){
if(num % i == 0){
factors.add(i);
factors.add(num/i);
}
if(num/i <= i)
break;
}
return factors;
}
}
/*
OUTPUT
220 & 284 are Amicable Pair
OUTPUT
200 & 300 are NOT Amicable Pair
OUTPUT
5020 & 5564 are Amicable Pair
*/