-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPS.cs
70 lines (68 loc) · 2.13 KB
/
APS.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AlgoSolution
{
class APS
{
static void Main()
{
int t = int.Parse(Console.ReadLine());
long[] sum = new long[10000001];
sum[0] = sum[1] = 0;
bool[] pList = PrimeList();
for (int i = 2; i < 10000001; i++)
{
if (pList[i] == false)
{
sum[i] = sum[i - 1] + i;
}
else
{
if (i % 2 == 0)
{
sum[i] = sum[i - 1] + 2;
}
else
{
for (int j = 3; j < 10000001; j += 2)
{
if (pList[j] == false)
{
if (i % j == 0)
{
sum[i] = sum[i - 1] + j;
break;
}
}
}
}
}
}
while (t > 0)
{
int n = int.Parse(Console.ReadLine());
Console.WriteLine(sum[n]);
t--;
}
}
static bool[] PrimeList()
{
bool[] primeList = new bool[10000001];
for (int i = 2; i < 10000001; i++)
{
if (primeList[i] == false)
{
int counter = 2;
while (i * counter < 10000001)
{
primeList[i * counter] = true;
counter++;
}
}
}
return primeList;
}
}
}