-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem21.cs
46 lines (37 loc) · 895 Bytes
/
Problem21.cs
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
using System;
class Problem21
{
const int N = 10000;
static void Main(string[] args)
{
int totalSum = 0;
int[] sums = new int[N];
for (int i = 4; i < N; i++)
{
if (sums[i] == 0)
sums[i] = SumOfDivisors(i);
if (sums[i] < N)
{
if (sums[sums[i]] == 0)
sums[sums[i]] = SumOfDivisors(sums[i]);
if (sums[sums[i]] == i && i != sums[i])
totalSum += i;
}
}
Console.WriteLine(totalSum);
}
static int SumOfDivisors(int n)
{
int sum = 1;
int sqrt = (int) Math.Sqrt(n);
for (int i = 2; i <= sqrt; i++)
{
if (n % i == 0)
{
sum += i;
sum += (n / i);
}
}
return sum;
}
}