Skip to content

Bug Fix: Integer overflow on windows in Project Euler 072 #10672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions project_euler/problem_072/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
function, phi(n). So, the answer is simply the sum of phi(n) for 2 <= n <= 1,000,000
Sum of phi(d), for all d|n = n. This result can be used to find phi(n) using a sieve.

Time: 1 sec
Time: 0.4 sec
"""

import numpy as np
Expand All @@ -36,12 +36,11 @@ def solution(limit: int = 1_000_000) -> int:
"""

# generating an array from -1 to limit
phi = np.arange(-1, limit)
phi = np.arange(-1, limit, dtype=np.int64)

for i in range(2, limit + 1):
if phi[i] == i - 1:
ind = np.arange(2 * i, limit + 1, i) # indexes for selection
phi[ind] -= phi[ind] // i
phi[2 * i :: i] -= phi[2 * i :: i] // i

return np.sum(phi[2 : limit + 1])

Expand Down
34 changes: 28 additions & 6 deletions project_euler/problem_072/sol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,43 @@
"""


def solution(limit: int = 1000000) -> int:
def set_generate_primes(limit: int) -> set:
"""
Return the number of reduced proper fractions with denominator less than limit.
>>> solution(8)
21
>>> solution(1000)
304191
Returns prime numbers below max_number.

>>> set_generate_primes(10)
{2, 3, 5, 7}
>>> set_generate_primes(2)
set()
"""

if limit <= 2:
return set()

primes = set(range(3, limit, 2))
primes.add(2)

for p in range(3, limit, 2):
if p not in primes:
continue
primes.difference_update(set(range(p * p, limit, p)))

return primes


def solution(limit: int = 1000000) -> int:
"""
Return the number of reduced proper fractions with denominator less than limit.

Time: 1 sec

>>> solution(8)
21
>>> solution(1000)
304191
"""

primes = set_generate_primes(limit)
phi = [float(n) for n in range(limit + 1)]

for p in primes:
Expand Down