Commit 55140150 authored by Nayuki Minase's avatar Nayuki Minase

P92, P102, P104, P128, P132, P133, P182, P205, P250, P381: Tweaked Python...

P92, P102, P104, P128, P132, P133, P182, P205, P250, P381: Tweaked Python solution code for clarity.
parent 58618e1e
......@@ -9,8 +9,9 @@
def compute():
ans = 0
terminals = (1, 89)
for i in range(1, 10000000):
while i != 1 and i != 89:
while i not in terminals:
i = square_digit_sum(i)
if i == 89:
ans += 1
......
......@@ -8,10 +8,7 @@
def compute():
ans = 0
for coords in TRIANGLES:
if is_origin_in_triangle(*coords):
ans += 1
ans = sum(1 for coords in TRIANGLES if is_origin_in_triangle(*coords))
return str(ans)
......
......@@ -6,21 +6,24 @@
# https://github.com/nayuki/Project-Euler-solutions
#
import itertools
def compute():
MOD = 10**9
a = 0
b = 1
i = 0
while True:
if "".join(sorted(str(a))) == "123456789" and "".join(sorted(str(fibonacci(i)[0])[ : 9])) == "123456789":
return str(i)
for i in itertools.count():
# Loop invariants: a == fib(i) % MOD, b == fib(i+1) % MOD
if "".join(sorted(str(a))) == "123456789": # If suffix is pandigital
f = fibonacci(i)[0]
if "".join(sorted(str(f)[ : 9])) == "123456789": # If prefix is pandigital
return str(i)
a, b = b, (a + b) % MOD
i += 1
return str(ans)
# Returns the tuple (F(n), F(n+1)).
# Returns the tuple (F(n), F(n+1)), computed by the fast doubling method.
def fibonacci(n):
if n == 0:
return (0, 1)
......
......@@ -171,11 +171,11 @@ def compute():
TARGET = 2000 # Must be at least 3
count = 2 # Because n = 1 and 2 satisfy PD(n) = 3
for ring in itertools.count(2):
if eulerlib.is_prime(ring * 6 - 1) and eulerlib.is_prime(ring * 6 + 1) and eulerlib.is_prime(ring * 12 + 5):
if all(map(eulerlib.is_prime, (ring * 6 - 1, ring * 6 + 1, ring * 12 + 5))):
count += 1
if count == TARGET:
return str(ring * (ring - 1) * 3 + 2)
if eulerlib.is_prime(ring * 6 - 1) and eulerlib.is_prime(ring * 6 + 5) and eulerlib.is_prime(ring * 12 - 7):
if all(map(eulerlib.is_prime, (ring * 6 - 1, ring * 6 + 5, ring * 12 - 7))):
count += 1
if count == TARGET:
return str(ring * (ring + 1) * 3 + 1)
......
......@@ -6,19 +6,18 @@
# https://github.com/nayuki/Project-Euler-solutions
#
import eulerlib
import eulerlib, itertools
def compute():
sum = 0
ans = 0
count = 0
i = 2
while count < 40:
for i in itertools.count(2):
if eulerlib.is_prime(i) and repunit_mod(10**9, i) == 0:
sum += i
ans += i
count += 1
i += 1
return str(sum)
if count == 40:
return str(ans)
def repunit_mod(k, m):
......
......@@ -41,11 +41,8 @@ import eulerlib
# (for the number 2^16 = 65536). (In general, the largest exponent is floor(log2(limit)); in this case limit = 10^5.)
# So we only need to test if 10^16 is a multiple of k, equivalent to testing if R(10^16) is a multiple of n.
def compute():
ans = 0
primes = eulerlib.list_primes(100000)
for p in primes:
if p == 2 or p == 5 or not has_divisible_repunit(p):
ans += p
ans = sum(p for p in primes if p == 2 or p == 5 or not has_divisible_repunit(p))
return str(ans)
......
......@@ -18,11 +18,9 @@ def compute():
minunconcealedp = min(numunconcealedp)
minunconcealedq = min(numunconcealedq)
ans = 0
for e in range(TOTIENT):
if numunconcealedp[e % (P - 1)] == minunconcealedp and \
numunconcealedq[e % (Q - 1)] == minunconcealedq:
ans += e
ans = sum(e for e in range(TOTIENT) if
numunconcealedp[e % (P - 1)] == minunconcealedp and
numunconcealedq[e % (Q - 1)] == minunconcealedq)
return str(ans)
......
......@@ -8,21 +8,19 @@
def compute():
PYRAMIDAL_DIE_PDF = [0, 1, 1, 1, 1]
CUBIC_DIE_PDF = [0, 1, 1, 1, 1, 1, 1]
nine_pyramidal_pdf = [1]
PYRAMIDAL_DIE_PDF = [0, 1, 1, 1, 1]
for i in range(9):
nine_pyramidal_pdf = convolve(nine_pyramidal_pdf, PYRAMIDAL_DIE_PDF)
six_cubic_pdf = [1]
CUBIC_DIE_PDF = [0, 1, 1, 1, 1, 1, 1]
for i in range(6):
six_cubic_pdf = convolve(six_cubic_pdf, CUBIC_DIE_PDF)
ans = 0
for i in range(len(nine_pyramidal_pdf)):
ans += nine_pyramidal_pdf[i] * sum(six_cubic_pdf[ : i])
ans = float(ans) / (4**9 * 6**6)
return "{:.7f}".format(ans)
......
......@@ -14,14 +14,13 @@ def compute():
subsets[0] = 1
for i in range(1, 250250 + 1):
temp = pow(i, i, 250)
newsubsets = list(subsets)
temp = pow(i, i, 250)
for j in range(len(subsets)):
k = (j + temp) % 250
newsubsets[k] = (subsets[j] + subsets[k]) % MOD
subsets = newsubsets
ans = (subsets[0] - 1) % MOD
return str(ans)
......
......@@ -27,10 +27,7 @@ def compute():
def s(p):
return (p - 3) * reciprocal_mod(8 % p, p) % p
ans = 0
for p in eulerlib.prime_generator(10**8):
if p >= 5:
ans += s(p)
ans = sum(s(p) for p in eulerlib.prime_generator(10**8) if p >= 5)
return str(ans)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment