-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
40 lines (34 loc) · 944 Bytes
/
Copy pathsolution.py
File metadata and controls
40 lines (34 loc) · 944 Bytes
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
import json
import time
import math
def get_prime_factors(n: int):
prime_factors = []
i, j = 2, math.ceil(n**0.5)
while i <= j:
if n % i == 0:
while n % i == 0:
n //= i
prime_factors.append(i)
i += 1
j = math.ceil(n**0.5)
if n > 1:
prime_factors.append(n)
return prime_factors
def phi(n: int) -> int:
"""Calculate totient number"""
tot = n
prime_fac = get_prime_factors(n)
for p in prime_fac:
tot *= 1 - 1/p
return int(tot)
def solution() -> int:
d = 10**6
total_elements = 0
for i in range(2, d+1):
total_elements += phi(i)
return total_elements
cpu_s, wall_s = time.process_time(), time.time()
result = solution()
cpu_e, wall_e = time.process_time(), time.time()
cpu_time, wall_time = cpu_e - cpu_s, wall_e - wall_s
print(json.dumps({"solution": result, "cpu": cpu_time, "wall": wall_time}))