Solve problem 347 and some reformatting.

This commit is contained in:
2024-06-23 12:27:16 -04:00
parent 75b4398c90
commit b889c12ae7
6 changed files with 50 additions and 13 deletions

View File

@@ -52,4 +52,3 @@ if __name__ == "__main__":
solution = euler_203() solution = euler_203()
print("e203.py: " + str(solution)) print("e203.py: " + str(solution))
assert solution == 34029210557338 assert solution == 34029210557338

View File

@@ -35,4 +35,4 @@ def euler_204():
if __name__ == "__main__": if __name__ == "__main__":
solution = euler_204() solution = euler_204()
print("e204.py: " + str(solution)) print("e204.py: " + str(solution))
assert(solution == 2944730) assert solution == 2944730

View File

@@ -17,12 +17,12 @@ def euler_205():
all_combinations = len(scores_four) * len(scores_six) all_combinations = len(scores_four) * len(scores_six)
four_won_count = 0 four_won_count = 0
for four_value, four_count in get_item_counts(scores_four).items(): for four_value, four_count in get_item_counts(scores_four).items():
current_six_count = 0 current_six_count = 0
for six_value, six_count in get_item_counts(scores_six).items(): for six_value, six_count in get_item_counts(scores_six).items():
if four_value > six_value: if four_value > six_value:
current_six_count += six_count current_six_count += six_count
four_won_count += (four_count * current_six_count) four_won_count += four_count * current_six_count
p = four_won_count / all_combinations p = four_won_count / all_combinations
return f"{round(p, 7)}" return f"{round(p, 7)}"
@@ -32,4 +32,3 @@ if __name__ == "__main__":
solution = euler_205() solution = euler_205()
print("e205.py: " + str(solution)) print("e205.py: " + str(solution))
# assert(solution == 0) # assert(solution == 0)

View File

@@ -2,13 +2,14 @@ import math
def is_square(apositiveint): def is_square(apositiveint):
""" From: https://stackoverflow.com/a/2489519 """ """From: https://stackoverflow.com/a/2489519"""
x = apositiveint // 2 x = apositiveint // 2
seen = set([x]) seen = set([x])
while x * x != apositiveint: while x * x != apositiveint:
x = (x + (apositiveint // x)) // 2 x = (x + (apositiveint // x)) // 2
if x in seen: return False if x in seen:
seen.add(x) return False
seen.add(x)
return True return True
@@ -16,7 +17,7 @@ def canditates(current_value, depth):
if depth <= 0: if depth <= 0:
yield current_value yield current_value
else: else:
d = 10 ** depth d = 10**depth
for i in range(10): for i in range(10):
for new_value in canditates(current_value - i * d, depth - 2): for new_value in canditates(current_value - i * d, depth - 2):
yield new_value yield new_value
@@ -32,5 +33,4 @@ def euler_206():
if __name__ == "__main__": if __name__ == "__main__":
solution = euler_206() solution = euler_206()
print("e206.py: " + str(solution)) print("e206.py: " + str(solution))
assert(solution == 1389019170) assert solution == 1389019170

View File

@@ -37,4 +37,4 @@ def euler_243():
if __name__ == "__main__": if __name__ == "__main__":
solution = euler_243() solution = euler_243()
print("e243.py: " + str(solution)) print("e243.py: " + str(solution))
assert(solution == 892371480) assert solution == 892371480

39
python/e347.py Normal file
View File

@@ -0,0 +1,39 @@
from lib_prime import primes
from typing import Optional, Tuple
def s(n):
ps = primes(n)
sum = 0
for p1_index in range(len(ps)):
p1 = ps[p1_index]
for p2_index in range(p1_index + 1, len(ps)):
p2 = ps[p2_index]
if p1 * p2 > n:
break
largest: Optional[Tuple[int, int, int]] = None
p1e = 1
while p1**p1e * p2 <= n:
p2e = 1
m = p1**p1e * p2**p2e
while m <= n:
if largest is None or m > largest[2]:
largest = (p1, p2, m)
p2e += 1
m = p1**p1e * p2**p2e
p1e += 1
assert largest is not None
sum += largest[2]
return sum
def euler_347():
assert s(100) == 2262
return s(10_000_000)
if __name__ == "__main__":
solution = euler_347()
print("e347.py: " + str(solution))
assert solution == 11109800204052