Compare commits

..

2 Commits

Author SHA1 Message Date
fc66fce8ff Solve problem 145 2021-04-21 16:30:54 -04:00
c281f9e9c0 Solve problem 91 2021-04-21 15:12:05 -04:00
2 changed files with 76 additions and 24 deletions

View File

@@ -1,32 +1,55 @@
from collections import namedtuple
Point = namedtuple("P", ["x", "y"])
Vector = namedtuple("V", ["x", "y"])
def generate_triangles(n):
all_points = [Point(x, y)
for x in range(0, n + 1)
for y in range(0, n + 1)
]
all_points = all_points[1:] # remove (0, 0)
all_pairs = []
for i in range(len(all_points)):
for j in range(i + 1, len(all_points)):
p = (all_points[i], all_points[j])
yield p
def vector(p1, p2):
return Vector(p2.x - p1.x, p2.y - p1.y)
def dot_product(v1, v2):
return v1.x * v2.x + v1.y * v2.y
def has_right_angle(p, q):
o = Point(0, 0)
oq = vector(o, q)
op = vector(o, p)
qp = vector(q, p)
if dot_product(oq, op) == 0:
return True
elif dot_product(oq, qp) == 0:
return True
elif dot_product(op, qp) == 0:
return True
return False
def euler_091():
n = 2
qs = [(x, y)
for x in range(n + 1)
for y in range(n + 1)]
def count_possible_right_triangles(q):
o = (0, 0)
if o == q:
return 0
ps = [(x, y)
for x in range(n + 1)
for y in range(q[0], n + 1)]
for p in ps:
if q == p:
continue
print(o, q, p)
return 0
s = sum(map(count_possible_right_triangles, qs))
return s
count = 0
ts = generate_triangles(50)
for t in ts:
if has_right_angle(*t):
count += 1
return count
if __name__ == "__main__":
solution = euler_091()
print("e091.py: " + str(solution))
assert(solution == 0)
assert(solution == 14234)

29
python/e145.py Normal file
View File

@@ -0,0 +1,29 @@
def reversible(n):
n_rev_str = str(n)[::-1]
if n_rev_str.startswith("0"):
return False
n_rev = int(n_rev_str)
ds = str(n + n_rev)
for d in ds:
if int(d) % 2 == 0:
return False
return True
def euler_145():
# pure bruteforce, took about four minutes wity pypy
count = 0
counted = set()
for i in range(10**9):
if not i in counted and reversible(i):
count += 1
counted.add(i)
return count
if __name__ == "__main__":
solution = euler_145()
print("e145.py: " + str(solution))
assert(solution == 608720)