Commit 133451c5 authored by Nayuki Minase's avatar Nayuki Minase

P69, P70, P71, P72, P76, P77, P80, P81, P85: Added Python solutions.

P58: Added Mathematica solution.
parent 9f816464
......@@ -71,6 +71,15 @@ ANSWERS = {
63: "49",
65: "272",
67: "7273",
69: "510510",
70: "8319823",
71: "428570",
72: "303963552391",
76: "190569291",
77: "71",
80: "40886",
81: "427337",
85: "2772",
}
......
(*
* Solution to Project Euler problem 58
* by Project Nayuki
*
* http://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*)
(* Use this in Wolfram Mathematica *)
(*
* From the diagram, let's observe the four corners of an n * n square (where n is odd).
* It's not hard to convince yourself that:
* - The bottom right corner always has the value n^2.
* Working clockwise (backwards):
* - The bottom left corner has the value n^2 - (n - 1).
* - The top left corner has the value n^2 - 2(n - 1).
* - The top right has the value n^2 - 3(n - 1).
*
* Furthermore, the number of elements on the diagonal is 2n - 1.
*)
p = 0;
For[n = 1, True, n += 2,
p += Sum[Boole[PrimeQ[n^2 - k * (n - 1)]], {k, 0, 3}];
If[n > 1 && p / (n * 2 - 1) < 1/10,
Break[]]]
n
#
# Solution to Project Euler problem 69
# by Project Nayuki
#
# http://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
def compute():
totients = list(range(1000001))
for i in range(2, len(totients)):
if totients[i] == i: # i is prime
for j in range(i, len(totients), i):
totients[j] = totients[j] // i * (i - 1)
maxnumer = 0
maxdenom = 1
for (i, tot) in enumerate(totients[2 : ], 2):
if i * maxdenom > maxnumer * tot:
maxnumer = i
maxdenom = totients[i]
return str(maxnumer)
if __name__ == "__main__":
print(compute())
#
# Solution to Project Euler problem 70
# by Project Nayuki
#
# http://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
def compute():
totients = list(range(10000000))
for i in range(2, len(totients)):
if totients[i] == i: # i is prime
for j in range(i, len(totients), i):
totients[j] = totients[j] // i * (i - 1)
minnumer = 1
mindenom = 0
for (i, tot) in enumerate(totients[2 : ], 2):
if sorted(str(i)) == sorted(str(tot)) and i * mindenom < minnumer * tot:
minnumer = i
mindenom = totients[i]
return str(minnumer)
if __name__ == "__main__":
print(compute())
#
# Solution to Project Euler problem 71
# by Project Nayuki
#
# http://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
def compute():
maxnumer = 0
maxdenom = 1
for d in range(2, 1000001):
n = d * 3 // 7
if d % 7 == 0:
n -= 1
if n * maxdenom > d * maxnumer:
maxnumer = n
maxdenom = d
return str(maxnumer)
if __name__ == "__main__":
print(compute())
#
# Solution to Project Euler problem 72
# by Project Nayuki
#
# http://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import itertools
def compute():
totients = list(range(1000001))
for i in range(2, len(totients)):
if totients[i] == i: # i is prime
for j in range(i, len(totients), i):
totients[j] = totients[j] // i * (i - 1)
ans = sum(itertools.islice(totients, 2, len(totients)))
return str(ans)
if __name__ == "__main__":
print(compute())
#
# Solution to Project Euler problem 76
# by Project Nayuki
#
# http://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
def compute():
LIMIT = 100
partitions = []
for i in range(LIMIT + 1):
partitions.append([None] * (LIMIT + 1))
for j in reversed(range(0, LIMIT + 1)):
if j == i:
partitions[i][j] = 1
elif j > i:
partitions[i][j] = 0
elif j == 0:
partitions[i][j] = partitions[i][j + 1]
else:
partitions[i][j] = partitions[i][j + 1] + partitions[i - j][j]
ans = partitions[LIMIT][1] - 1
return str(ans)
if __name__ == "__main__":
print(compute())
#
# Solution to Project Euler problem 77
# by Project Nayuki
#
# http://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import eulerlib, itertools
def compute():
for n in itertools.count(2):
if num_prime_sum_ways(n) > 5000:
return str(n)
primes = [2]
def num_prime_sum_ways(n):
for i in range(primes[-1] + 1, n + 1):
if eulerlib.is_prime(i):
primes.append(i)
ways = [0] * (n + 1)
ways[0] = 1
for p in primes:
for i in range(n + 1 - p):
ways[i + p] += ways[i]
return ways[n]
if __name__ == "__main__":
print(compute())
#
# Solution to Project Euler problem 80
# by Project Nayuki
#
# http://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import eulerlib
def compute():
ans = 0
for i in range(100):
if eulerlib.sqrt(i)**2 != i:
temp = eulerlib.sqrt(i * 10**200)
ans += sum(int(c) for c in str(temp)[ : 100])
return str(ans)
if __name__ == "__main__":
print(compute())
This diff is collapsed.
#
# Solution to Project Euler problem 85
# by Project Nayuki
#
# http://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import eulerlib
def compute():
TARGET = 2000000
bestdiff = None
end = eulerlib.sqrt(TARGET) + 1
for w in range(1, end):
for h in range(1, end):
diff = abs(num_rectangles(w, h) - TARGET)
if bestdiff is None or diff < bestdiff:
ans = w * h
bestdiff = diff
return str(ans)
def num_rectangles(m, n):
return (m + 1) * m * (n + 1) * n // 4 # A bit more than m^2 n^2 / 4
if __name__ == "__main__":
print(compute())
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