-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathsol1.py
298 lines (214 loc) · 7.7 KB
/
sol1.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Project Euler Problem 70: https://projecteuler.net/problem=70
Euler's Totient function, φ(n) [sometimes called the phi function], is used to
determine the number of positive numbers less than or equal to n which are
relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than
nine and relatively prime to nine, φ(9)=6.
The number 1 is considered to be relatively prime to every positive number, so
φ(1)=1.
Interestingly, φ(87109)=79180, and it can be seen that 87109 is a permutation
of 79180.
Find the value of n, 1 < n < 10^7, for which φ(n) is a permutation of n and
the ratio n/φ(n) produces a minimum.
-----
This is essentially brute force. Calculate all totients up to 10^7 and
find the minimum ratio of n/φ(n) that way. To minimize the ratio, we want
to minimize n and maximize φ(n) as much as possible, so we can store the
minimum fraction's numerator and denominator and calculate new fractions
with each totient to compare against. To avoid dividing by zero, I opt to
use cross multiplication.
References:
Finding totients
https://en.wikipedia.org/wiki/Euler%27s_totient_function#Euler%27s_product_formula
"""
from __future__ import annotations
from math import isqrt
import numpy as np
def calculate_prime_numbers(max_number: int) -> list[int]:
"""
Returns prime numbers below max_number.
See: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
>>> calculate_prime_numbers(10)
[2, 3, 5, 7]
>>> calculate_prime_numbers(2)
[]
"""
if max_number <= 2:
return []
# List containing a bool value for every odd number below max_number/2
is_prime = [True] * (max_number // 2)
for i in range(3, isqrt(max_number - 1) + 1, 2):
if is_prime[i // 2]:
# Mark all multiple of i as not prime using list slicing
is_prime[i**2 // 2 :: i] = [False] * (
# Same as: (max_number - (i**2)) // (2 * i) + 1
# but faster than len(is_prime[i**2 // 2 :: i])
len(range(i**2 // 2, max_number // 2, i))
)
return [2] + [2 * i + 1 for i in range(1, max_number // 2) if is_prime[i]]
def np_calculate_prime_numbers(max_number: int) -> list[int]:
"""
Returns prime numbers below max_number.
See: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
>>> np_calculate_prime_numbers(10)
[2, 3, 5, 7]
>>> np_calculate_prime_numbers(2)
[]
"""
if max_number <= 2:
return []
# List containing a bool value for every odd number below max_number/2
is_prime = np.ones(max_number // 2, dtype=bool)
for i in range(3, isqrt(max_number - 1) + 1, 2):
if is_prime[i // 2]:
# Mark all multiple of i as not prime using list slicing
is_prime[i**2 // 2 :: i] = False
primes = np.where(is_prime)[0] * 2 + 1
primes[0] = 2
return primes.tolist()
def slow_get_totients(max_one: int) -> list[int]:
"""
Calculates a list of totients from 0 to max_one exclusive, using the
definition of Euler's product formula.
>>> slow_get_totients(5)
[0, 1, 1, 2, 2]
>>> slow_get_totients(10)
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6]
"""
totients = np.arange(max_one)
for i in range(2, max_one):
if totients[i] == i:
x = np.arange(i, max_one, i) # array of indexes to select
totients[x] -= totients[x] // i
return totients.tolist()
def slicing_get_totients(max_one: int) -> list[int]:
"""
Calculates a list of totients from 0 to max_one exclusive, using the
definition of Euler's product formula.
>>> slicing_get_totients(5)
[0, 1, 1, 2, 2]
>>> slicing_get_totients(10)
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6]
"""
totients = np.arange(max_one)
for i in range(2, max_one):
if totients[i] == i:
totients[i::i] -= totients[i::i] // i
return totients.tolist()
def get_totients(limit) -> list[int]:
"""
Calculates a list of totients from 0 to max_one exclusive, using the
definition of Euler's product formula.
>>> get_totients(5)
[0, 1, 1, 2, 2]
>>> get_totients(10)
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6]
"""
totients = np.arange(limit)
primes = calculate_prime_numbers(limit)
for i in primes:
totients[i::i] -= totients[i::i] // i
return totients.tolist()
def np_get_totients(limit) -> list[int]:
"""
Calculates a list of totients from 0 to max_one exclusive, using the
definition of Euler's product formula.
>>> np_get_totients(5)
[0, 1, 1, 2, 2]
>>> np_get_totients(10)
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6]
"""
totients = np.arange(limit)
primes = np_calculate_prime_numbers(limit)
for i in primes:
totients[i::i] -= totients[i::i] // i
return totients.tolist()
def has_same_digits(num1: int, num2: int) -> bool:
"""
Return True if num1 and num2 have the same frequency of every digit, False
otherwise.
>>> has_same_digits(123456789, 987654321)
True
>>> has_same_digits(123, 23)
False
>>> has_same_digits(1234566, 123456)
False
"""
return sorted(str(num1)) == sorted(str(num2))
def slow_solution(max_n: int = 10000000) -> int:
"""
Finds the value of n from 1 to max such that n/φ(n) produces a minimum.
>>> slow_solution(100)
21
>>> slow_solution(10000)
4435
"""
totients = slow_get_totients(max_n + 1)
return common_solution(totients, max_n)
def slicing_solution(max_n: int = 10000000) -> int:
"""
Finds the value of n from 1 to max such that n/φ(n) produces a minimum.
>>> slicing_solution(100)
21
>>> slicing_solution(10000)
4435
"""
totients = slicing_get_totients(max_n + 1)
return common_solution(totients, max_n)
def py_solution(max_n: int = 10000000) -> int:
"""
Finds the value of n from 1 to max such that n/φ(n) produces a minimum.
>>> py_solution(100)
21
>>> py_solution(10000)
4435
"""
totients = get_totients(max_n + 1)
return common_solution(totients, max_n)
def solution(max_n: int = 10000000) -> int:
"""
Finds the value of n from 1 to max such that n/φ(n) produces a minimum.
>>> solution(100)
21
>>> solution(10000)
4435
"""
totients = np_get_totients(max_n + 1)
return common_solution(totients, max_n)
def common_solution(totients: list[int], max_n: int = 10000000) -> int:
"""
Finds the value of n from 1 to max such that n/φ(n) produces a minimum.
>>> common_solution(get_totients(101), 100)
21
>>> common_solution(get_totients(10001), 10000)
4435
"""
min_numerator = 1 # i
min_denominator = 0 # φ(i)
for i in range(2, max_n + 1):
t = totients[i]
if i * min_denominator < min_numerator * t and has_same_digits(i, t):
min_numerator = i
min_denominator = t
return min_numerator
def benchmark() -> None:
"""
Benchmark
"""
# Running performance benchmarks...
# Solution : 49.389978999999585
# Py Solution : 56.19136740000067
# Slicing Sol : 70.83823779999875
# Slow Sol : 118.29514729999937
from timeit import timeit
print("Running performance benchmarks...")
print(f"Solution : {timeit('solution()', globals=globals(), number=10)}")
print(f"Py Solution : {timeit('py_solution()', globals=globals(), number=10)}")
print(f"Slicing Sol : {timeit('slicing_solution()', globals=globals(), number=10)}")
print(f"Slow Sol : {timeit('slow_solution()', globals=globals(), number=10)}")
if __name__ == "__main__":
print(f"Solution : {solution()}")
print(f"Py Solution : {py_solution()}")
print(f"Slicing Sol : {slicing_solution()}")
print(f"Slow Sol : {slow_solution()}")
benchmark()