-
Notifications
You must be signed in to change notification settings - Fork 0
/
amicable-numbers.cpp
65 lines (60 loc) · 1.23 KB
/
amicable-numbers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
// https://www.hackerrank.com/contests/projecteuler/challenges/euler021/problem
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll factorsum(ll n)
{
ll sum = 1;
for(int i = 2; i *i <= n ; i++)
{
if(n % i == 0)
{
sum += i;
if( n/ i != i)
sum += n / i;
}
}
return sum;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
ll sum[100001];
sum[0] = 0;
for(ll i = 1; i <= 100000; i++)
{
sum[i] = factorsum(i);
}
ll check[100001] = {0};
for(ll i = 1 ; i <= 100000 ; i ++)
{
ll x = sum[i];
if(x <= 100000 && x!=i)
{
if(sum[x] == i)
{
//cout << i << " " << x << endl;
check[i] = i;
check[x] = x;
}
}
else if( x > 100000)
{
ll z = factorsum(x);
if(z == i)
check[i] = x;
}
}
for(int i = 1; i <= 100000; i++)
{
check[i] += check[i - 1];
}
ll t;
cin >> t;
while( t-- )
{
ll n;
cin >> n;
cout << check[n - 1] << endl;
}
return 0;
}