-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem21.cpp
51 lines (28 loc) · 894 Bytes
/
problem21.cpp
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
#include <iostream>
#include <cmath>
#include <list>
#define LIMIT 10000
using namespace std ;
unsigned long sum_of_divisors( int n ) {
unsigned long sum_of_divisors = 0 ;
for ( int i = 1 ; i <= sqrt(n) ; i++ ) {
if ( n % i == 0 ) { sum_of_divisors += i ; sum_of_divisors += n/i ; }
}
return sum_of_divisors-n ;
}
int main ( int argc , char** argv ) {
unsigned long d , d1 , sum_of_amicables=0 ;
list<int> amicables ;
for ( int a = 1 ; a < LIMIT ; a++ ) {
d = sum_of_divisors(a) ;
for ( int b = a+1 ; b < LIMIT ; b++ ) {
d1 = sum_of_divisors(b) ;
if ( d == b && d1 == a && a != b ) { cout << a << " " << b << endl ; amicables.push_back(d) ; amicables.push_back(d1) ; break ; }
}
}
for( list<int>::iterator it = amicables.begin() ; it != amicables.end() ; it++ ) {
sum_of_amicables += *it ;
}
cout << sum_of_amicables << endl ;
return 0 ;
}