forked from Minor-lazer/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmicable Number
More file actions
54 lines (45 loc) · 1.05 KB
/
Amicable Number
File metadata and controls
54 lines (45 loc) · 1.05 KB
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
// amicable pairs in an array.
#include <bits/stdc++.h>
using namespace std;
int sumOfDiv(int x)
{
// 1 is a proper divisor
int sum = 1;
for (int i = 2; i <= sqrt(x); i++)
{
if (x % i == 0)
{
sum += i;
if (x / i != i)
sum += x / i;
}
}
return sum;
}
bool isAmicable(int a, int b)
{
return (sumOfDiv(a) == b &&
sumOfDiv(b) == a);
}
int countPairs(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (isAmicable(arr[i], arr[j]))
count++;
return count;
}
int main()
{
int arr1[] = { 220, 284, 1184,
1210, 2, 5 };
int n1 = sizeof(arr1) / sizeof(arr1[0]);
cout << countPairs(arr1, n1)
<< endl;
int arr2[] = { 2620, 2924, 5020,
5564, 6232, 6368 };
int n2 = sizeof(arr2) / sizeof(arr2[0]);
cout << countPairs(arr2, n2);
return 0;
}