Skip to content

Commit 8f9d8ea

Browse files
committed
Fixes: #3163 - Add new solution for problem 234
1 parent ff9be86 commit 8f9d8ea

File tree

1 file changed

+90
-32
lines changed

1 file changed

+90
-32
lines changed

Diff for: project_euler/problem_234/sol1.py

+90-32
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,98 @@
1717
What is the sum of all semidivisible numbers not exceeding 999966663333 ?
1818
"""
1919

20+
import math
2021

21-
def fib(a, b, n):
22-
23-
if n == 1:
24-
return a
25-
elif n == 2:
26-
return b
27-
elif n == 3:
28-
return str(a) + str(b)
29-
30-
temp = 0
31-
for x in range(2, n):
32-
c = str(a) + str(b)
33-
temp = b
34-
b = c
35-
a = temp
36-
return c
37-
38-
39-
def solution(n):
40-
"""Returns the sum of all semidivisible numbers not exceeding n."""
41-
semidivisible = []
42-
for x in range(n):
43-
l = [i for i in input().split()] # noqa: E741
44-
c2 = 1
45-
while 1:
46-
if len(fib(l[0], l[1], c2)) < int(l[2]):
47-
c2 += 1
48-
else:
22+
23+
def sieve(n: int) -> list:
24+
"""
25+
Sieve of Erotosthenes
26+
Function to return all the prime numbers up to a certain number
27+
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
28+
>>> sieve(3)
29+
[2]
30+
>>> sieve(50)
31+
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
32+
"""
33+
is_prime = [True] * n
34+
is_prime[0] = False
35+
is_prime[1] = False
36+
is_prime[2] = True
37+
38+
for i in range(3, int(n ** 0.5 + 1), 2):
39+
index = i * 2
40+
while index < n:
41+
is_prime[index] = False
42+
index = index + i
43+
44+
primes = [2]
45+
46+
for i in range(3, n, 2):
47+
if is_prime[i]:
48+
primes.append(i)
49+
50+
return primes
51+
52+
53+
def solution() -> int:
54+
"""
55+
Computes the solution to the problem
56+
>>> solution()
57+
1259187438574927161
58+
"""
59+
limit = 999966663333
60+
primes_upper_bound = math.floor(math.sqrt(limit)) + 100
61+
primes = sieve(primes_upper_bound)
62+
63+
matches_sum = 0
64+
prime_index = 0
65+
last_prime = primes[prime_index]
66+
67+
while (last_prime * last_prime) <= limit:
68+
next_prime = primes[prime_index + 1]
69+
70+
lower_bound = last_prime * last_prime
71+
upper_bound = next_prime * next_prime
72+
73+
# Get numbers divisible by lps(current)
74+
current = lower_bound + last_prime
75+
while current < upper_bound and current <= limit:
76+
matches_sum += current
77+
current += last_prime
78+
79+
# Reset the upper_bound
80+
while (upper_bound - next_prime) > limit:
81+
upper_bound -= next_prime
82+
83+
# Add the numbers divisible by ups(current)
84+
current = upper_bound - next_prime
85+
while current > lower_bound:
86+
matches_sum += current
87+
current -= next_prime
88+
89+
# Remove the numbers divisible by both ups and lps
90+
current = lower_bound - lower_bound % (last_prime * next_prime)
91+
while current < upper_bound and current <= limit:
92+
if current <= lower_bound:
93+
# Increment the current number
94+
current += last_prime * next_prime
95+
continue
96+
97+
if current > limit:
4998
break
50-
semidivisible.append(fib(l[0], l[1], c2 + 1)[int(l[2]) - 1])
51-
return semidivisible
99+
100+
# Remove twice since it was added by both ups and lps
101+
matches_sum -= current * 2
102+
103+
# Increment the current number
104+
current += last_prime * next_prime
105+
106+
# Setup for next pair
107+
last_prime = next_prime
108+
prime_index += 1
109+
110+
return matches_sum
52111

53112

54113
if __name__ == "__main__":
55-
for i in solution(int(str(input()).strip())):
56-
print(i)
114+
print(solution())

0 commit comments

Comments
 (0)