Unified numbers and output

This commit is contained in:
David Doblas Jiménez 2023-04-01 15:57:07 +02:00
parent e004055fb3
commit 220a742b6b
50 changed files with 188 additions and 179 deletions

View File

@ -5,14 +5,14 @@ Created on 14 Mar 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 1 of Project Euler Solution for problem 001 of Project Euler
https://projecteuler.net/problem=1 https://projecteuler.net/problem=1
""" """
from utils import timeit from utils import timeit
@timeit("Problem 1") @timeit("Problem 001")
def compute(): def compute():
""" """
If we list all the natural numbers below 10 that are multiples of 3 or 5, If we list all the natural numbers below 10 that are multiples of 3 or 5,
@ -27,4 +27,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for problem 1 is {compute()}") print(f"Result for problem 001: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 14 Mar 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 2 of Project Euler Solution for problem 002 of Project Euler
https://projecteuler.net/problem=2 https://projecteuler.net/problem=2
""" """
from utils import timeit from utils import timeit
@timeit("Problem 2") @timeit("Problem 002")
def compute(): def compute():
""" """
Each new term in the Fibonacci sequence is generated by adding the Each new term in the Fibonacci sequence is generated by adding the
@ -38,4 +38,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for problem 2 is {compute()}") print(f"Result for problem 002: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 18 Mar 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 3 of Project Euler Solution for problem 003 of Project Euler
https://projecteuler.net/problem=3 https://projecteuler.net/problem=3
""" """
from utils import timeit from utils import timeit
@timeit("Problem 3") @timeit("Problem 003")
def compute(): def compute():
""" """
The prime factors of 13195 are 5, 7, 13 and 29. The prime factors of 13195 are 5, 7, 13 and 29.
@ -31,4 +31,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for problem 3 is {compute()}") print(f"Result for problem 003: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 18 Mar 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 4 of Project Euler Solution for problem 004 of Project Euler
https://projecteuler.net/problem=4 https://projecteuler.net/problem=4
""" """
from utils import timeit from utils import timeit
@timeit("Problem 4") @timeit("Problem 004")
def compute(): def compute():
""" """
A palindromic number reads the same both ways. The largest palindrome made A palindromic number reads the same both ways. The largest palindrome made
@ -33,4 +33,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for problem 4 is {compute()}") print(f"Result for problem 004: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 23 Apr 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 5 of Project Euler Solution for problem 005 of Project Euler
https://projecteuler.net/problem=5 https://projecteuler.net/problem=5
""" """
@ -21,7 +21,7 @@ from utils import timeit
# computing the LCM of two numbers, i.e. LCM(a, b, c) = LCM(a, LCM(b, c)) # computing the LCM of two numbers, i.e. LCM(a, b, c) = LCM(a, LCM(b, c))
@timeit("Problem 5") @timeit("Problem 005")
def compute(): def compute():
""" """
2520 is the smallest number that can be divided by each of the numbers 2520 is the smallest number that can be divided by each of the numbers
@ -39,4 +39,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for problem 5 is {compute()}") print(f"Result for problem 005: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 17 Jun 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 6 of Project Euler Solution for problem 006 of Project Euler
https://projecteuler.net/problem=6 https://projecteuler.net/problem=6
""" """
from utils import timeit from utils import timeit
@timeit("Problem 6") @timeit("Problem 006")
def compute(): def compute():
""" """
The sum of the squares of the first ten natural numbers is, The sum of the squares of the first ten natural numbers is,
@ -36,4 +36,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 6 is {compute()}") print(f"Result for Problem 006: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 28 Jun 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 7 of Project Euler Solution for problem 007 of Project Euler
https://projecteuler.net/problem=7 https://projecteuler.net/problem=7
""" """
from utils import is_prime, timeit from utils import is_prime, timeit
@timeit("Problem 7") @timeit("Problem 007")
def compute(): def compute():
""" """
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see
@ -34,4 +34,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 7 is {compute()}") print(f"Result for Problem 007: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 28 Abr 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 8 of Project Euler Solution for problem 008 of Project Euler
https://projecteuler.net/problem=8 https://projecteuler.net/problem=8
""" """
@ -27,7 +27,7 @@ def sliding_window(iterable, n):
yield tuple(window) yield tuple(window)
@timeit("Problem 8") @timeit("Problem 008")
def compute(): def compute():
""" """
The four adjacent digits in the 1000-digit number that have the The four adjacent digits in the 1000-digit number that have the
@ -61,6 +61,7 @@ def compute():
05886116467109405077541002256983155200055935729725 05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450 71636269561882670428252483600823257530420752963450
""" """
num = NUM.replace("\n", "").replace(" ", "") num = NUM.replace("\n", "").replace(" ", "")
adjacent_digits = 13 adjacent_digits = 13
ans = 0 ans = 0
@ -75,4 +76,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 8 is {compute()}") print(f"Result for Problem 008: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 26 Aug 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 9 of Project Euler Solution for problem 009 of Project Euler
https://projecteuler.net/problem=9 https://projecteuler.net/problem=9
""" """
from utils import timeit from utils import timeit
@timeit("Problem 9") @timeit("Problem 009")
def compute(): def compute():
""" """
A Pythagorean triplet is a set of three natural numbers, a < b < c, A Pythagorean triplet is a set of three natural numbers, a < b < c,
@ -34,4 +34,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 9 is {compute()}") print(f"Result for Problem 009: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 26 Aug 2017
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 10 of Project Euler Solution for problem 010 of Project Euler
https://projecteuler.net/problem=10 https://projecteuler.net/problem=10
""" """
from utils import list_primes, timeit from utils import list_primes, timeit
@timeit("Problem 10") @timeit("Problem 010")
def compute(): def compute():
""" """
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
@ -26,4 +26,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 10 is {compute()}") print(f"Result for Problem 010: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 07 Jul 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 11 of Project Euler Solution for problem 011 of Project Euler
https://projecteuler.net/problem=11 https://projecteuler.net/problem=11
""" """
@ -42,7 +42,7 @@ def grid_product(x, y, dx, dy, n):
return result return result
@timeit("Problem 11") @timeit("Problem 011")
def compute(): def compute():
""" """
In the 20x20 grid above, four numbers along a diagonal line have been In the 20x20 grid above, four numbers along a diagonal line have been
@ -72,4 +72,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 11 is {compute()}") print(f"Result for Problem 011: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 01 Jan 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 12 of Project Euler Solution for problem 012 of Project Euler
https://projecteuler.net/problem=12 https://projecteuler.net/problem=12
""" """
@ -27,7 +27,7 @@ def num_divisors(n):
return 2 * len(divs) return 2 * len(divs)
@timeit("Problem 12") @timeit("Problem 012")
def compute(): def compute():
""" """
The sequence of triangle numbers is generated by adding the natural The sequence of triangle numbers is generated by adding the natural
@ -63,4 +63,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 12 is {compute()}") print(f"Result for Problem 012: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 1 Jan 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 13 of Project Euler Solution for problem 013 of Project Euler
https://projecteuler.net/problem=13 https://projecteuler.net/problem=13
""" """
@ -14,14 +14,15 @@ from pathlib import Path
from utils import timeit from utils import timeit
@timeit("Problem 13") @timeit("Problem 013")
def compute(): def compute():
""" """
Work out the first ten digits of the sum of the following one-hundred Work out the first ten digits of the sum of the following one-hundred
50-digit numbers. 50-digit numbers.
""" """
_file = Path("../files/Problem13.txt")
with open(_file, "r") as f: file = Path("files/Problem13.txt")
with open(file, "r") as f:
num = f.readlines() num = f.readlines()
ans = 0 ans = 0
for line in num: for line in num:
@ -31,4 +32,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 13 is {compute()}") print(f"Result for Problem 013: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 7 Jan 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 14 of Project Euler Solution for problem 014 of Project Euler
https://projecteuler.net/problem=14 https://projecteuler.net/problem=14
""" """
@ -26,7 +26,7 @@ def chain_length(n, terms):
return length return length
@timeit("Problem 14") @timeit("Problem 014")
def compute(): def compute():
""" """
The following iterative sequence is defined for the set of positive The following iterative sequence is defined for the set of positive
@ -63,4 +63,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 14 is {compute()}") print(f"Result for Problem 014: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 7 Jan 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 15 of Project Euler Solution for problem 015 of Project Euler
https://projecteuler.net/problem=15 https://projecteuler.net/problem=15
""" """
@ -14,7 +14,7 @@ from math import factorial
from utils import timeit from utils import timeit
@timeit("Problem 15") @timeit("Problem 015")
def compute(): def compute():
""" """
Starting in the top left corner of a 2x2 grid, and only being able to Starting in the top left corner of a 2x2 grid, and only being able to
@ -25,8 +25,10 @@ def compute():
""" """
n = 20 n = 20
return int(factorial(2 * n) / (factorial(n) * factorial(2 * n - n))) ans = int(factorial(2 * n) / (factorial(n) * factorial(2 * n - n)))
return ans
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 15 is {compute()}") print(f"Result for Problem 015: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 13 Jan 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 16 of Project Euler Solution for problem 016 of Project Euler
https://projecteuler.net/problem=16 https://projecteuler.net/problem=16
""" """
from utils import timeit from utils import timeit
@timeit("Problem 16") @timeit("Problem 016")
def compute(): def compute():
""" """
2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
@ -21,8 +21,10 @@ def compute():
""" """
n = 1000 n = 1000
return sum(int(digit) for digit in str(2**n)) ans = sum(int(digit) for digit in str(2**n))
return ans
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 16 is {compute()}") print(f"Result for Problem 016: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 13 Jan 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 17 of Project Euler Solution for problem 017 of Project Euler
https://projecteuler.net/problem=17 https://projecteuler.net/problem=17
""" """
@ -67,7 +67,7 @@ def num_to_letters(num):
return num_to_letters(thousands) + len(nums[1000]) return num_to_letters(thousands) + len(nums[1000])
@timeit("Problem 17") @timeit("Problem 017")
def compute(): def compute():
""" """
If the numbers 1 to 5 are written out in words: one, two, three, four, If the numbers 1 to 5 are written out in words: one, two, three, four,
@ -83,11 +83,12 @@ def compute():
""" """
n = 1000 n = 1000
letters = 0 ans = 0
for num in range(1, n + 1): for num in range(1, n + 1):
letters += num_to_letters(num) ans += num_to_letters(num)
return letters
return ans
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 17 is {compute()}") print(f"Result for Problem 017: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 15 Sep 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 18 of Project Euler Solution for problem 018 of Project Euler
https://projecteuler.net/problem=18 https://projecteuler.net/problem=18
""" """
@ -30,7 +30,7 @@ triangle = [
] ]
@timeit("Problem 18") @timeit("Problem 018")
def compute(): def compute():
""" """
By starting at the top of the triangle below and moving to adjacent By starting at the top of the triangle below and moving to adjacent
@ -54,4 +54,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 18 is {compute()}") print(f"Result for Problem 018: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 15 Sep 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 19 of Project Euler Solution for problem 019 of Project Euler
https://projecteuler.net/problem=19 https://projecteuler.net/problem=19
""" """
@ -14,7 +14,7 @@ from datetime import date
from utils import timeit from utils import timeit
@timeit("Problem 19") @timeit("Problem 019")
def compute(): def compute():
""" """
You are given the following information, but you may prefer to do some You are given the following information, but you may prefer to do some
@ -33,13 +33,14 @@ def compute():
century (1 Jan 1901 to 31 Dec 2000)? century (1 Jan 1901 to 31 Dec 2000)?
""" """
sundays = 0 ans = 0
for year in range(1901, 2001): for year in range(1901, 2001):
for month in range(1, 13): for month in range(1, 13):
if date(year, month, 1).weekday() == 6: if date(year, month, 1).weekday() == 6:
sundays += 1 ans += 1
return sundays
return ans
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 19 is {compute()}") print(f"Result for Problem 019: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 15 Sep 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 20 of Project Euler Solution for problem 020 of Project Euler
https://projecteuler.net/problem=20 https://projecteuler.net/problem=20
""" """
@ -14,7 +14,7 @@ from math import factorial
from utils import timeit from utils import timeit
@timeit("Problem 20") @timeit("Problem 020")
def compute(): def compute():
""" """
n! means n x (n - 1) x ... x 3 x 2 x 1 n! means n x (n - 1) x ... x 3 x 2 x 1
@ -33,4 +33,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 20 is {compute()}") print(f"Result for Problem 020: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 15 Sep 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 21 of Project Euler Solution for problem 021 of Project Euler
https://projecteuler.net/problem=21 https://projecteuler.net/problem=21
""" """
@ -16,7 +16,7 @@ def sum_divisors(n):
return sum(i for i in range(1, n // 2 + 1) if n % i == 0) return sum(i for i in range(1, n // 2 + 1) if n % i == 0)
@timeit("Problem 21") @timeit("Problem 021")
def compute(): def compute():
""" """
Let d(n) be defined as the sum of proper divisors of n (numbers Let d(n) be defined as the sum of proper divisors of n (numbers
@ -42,4 +42,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 21 is {compute()}") print(f"Result for Problem 021: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 31 Dec 2018
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 22 of Project Euler Solution for problem 022 of Project Euler
https://projecteuler.net/problem=22 https://projecteuler.net/problem=22
""" """
@ -14,7 +14,7 @@ from pathlib import Path
from utils import timeit from utils import timeit
@timeit("Problem 22") @timeit("Problem 022")
def compute(): def compute():
""" """
Using names.txt, a 46K text file containing over five-thousand first names, Using names.txt, a 46K text file containing over five-thousand first names,
@ -29,7 +29,7 @@ def compute():
What is the total of all the name scores in the file? What is the total of all the name scores in the file?
""" """
file = Path("../files/Problem22.txt") file = Path("files/Problem22.txt")
with open(file, "r") as f: with open(file, "r") as f:
names = sorted(f.read().replace('"', "").split(",")) names = sorted(f.read().replace('"', "").split(","))
@ -41,4 +41,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 22 is {compute()}") print(f"Result for Problem 022: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 05 Jan 2019
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 23 of Project Euler Solution for problem 023 of Project Euler
https://projecteuler.net/problem=23 https://projecteuler.net/problem=23
""" """
@ -13,7 +13,7 @@ https://projecteuler.net/problem=23
from utils import timeit from utils import timeit
@timeit("Problem 23") @timeit("Problem 023")
def compute(): def compute():
""" """
A perfect number is a number for which the sum of its proper divisors is A perfect number is a number for which the sum of its proper divisors is
@ -58,4 +58,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 23 is {compute()}") print(f"Result for Problem 023: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 11 Sep 2019
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 24 of Project Euler Solution for problem 024 of Project Euler
https://projecteuler.net/problem=24 https://projecteuler.net/problem=24
""" """
@ -14,7 +14,7 @@ from itertools import permutations
from utils import timeit from utils import timeit
@timeit("Problem 24") @timeit("Problem 024")
def compute(): def compute():
""" """
A permutation is an ordered arrangement of objects. For example, 3124 is A permutation is an ordered arrangement of objects. For example, 3124 is
@ -30,9 +30,10 @@ def compute():
digits = list(range(10)) digits = list(range(10))
_permutations = list(permutations(digits)) _permutations = list(permutations(digits))
ans = "".join(str(digit) for digit in _permutations[999_999])
return "".join(str(digit) for digit in _permutations[999_999]) return ans
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 24 is {compute()}") print(f"Result for Problem 024: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 11 Sep 2019
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 25 of Project Euler Solution for problem 025 of Project Euler
https://projecteuler.net/problem=25 https://projecteuler.net/problem=25
""" """
from utils import timeit from utils import timeit
@timeit("Problem 25") @timeit("Problem 025")
def compute(): def compute():
""" """
The Fibonacci sequence is defined by the recurrence relation: The Fibonacci sequence is defined by the recurrence relation:
@ -50,4 +50,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 25 is {compute()}") print(f"Result for Problem 025: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 11 Sep 2019
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 26 of Project Euler Solution for problem 026 of Project Euler
https://projecteuler.net/problem=26 https://projecteuler.net/problem=26
""" """
from utils import timeit from utils import timeit
@timeit("Problem 26") @timeit("Problem 026")
def compute(): def compute():
""" """
A unit fraction contains 1 in the numerator. The decimal representation A unit fraction contains 1 in the numerator. The decimal representation
@ -50,4 +50,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 26 is {compute()}") print(f"Result for Problem 026: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 15 Sep 2019
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 27 of Project Euler Solution for problem 027 of Project Euler
https://projecteuler.net/problem=27 https://projecteuler.net/problem=27
""" """
from utils import is_prime, timeit from utils import is_prime, timeit
@timeit("Problem 27") @timeit("Problem 027")
def compute(): def compute():
""" """
Euler discovered the remarkable quadratic formula: Euler discovered the remarkable quadratic formula:
@ -56,4 +56,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 27 is {compute()}") print(f"Result for Problem 027: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 3 Jan 2020
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 28 of Project Euler Solution for problem 028 of Project Euler
https://projecteuler.net/problem=28 https://projecteuler.net/problem=28
""" """
from utils import timeit from utils import timeit
@timeit("Problem 28") @timeit("Problem 028")
def compute(): def compute():
""" """
Starting with the number 1 and moving to the right in a clockwise Starting with the number 1 and moving to the right in a clockwise
@ -45,4 +45,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 28 is {compute()}") print(f"Result for Problem 028: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 3 Jan 2020
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 29 of Project Euler Solution for problem 029 of Project Euler
https://projecteuler.net/problem=29 https://projecteuler.net/problem=29
""" """
from itertools import product from itertools import product
@ -13,7 +13,7 @@ from itertools import product
from utils import timeit from utils import timeit
@timeit("Problem 29") @timeit("Problem 029")
def compute(): def compute():
""" """
Consider all integer combinations of ab for 2 a 5 and 2 b 5: Consider all integer combinations of ab for 2 a 5 and 2 b 5:
@ -38,4 +38,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 29 is {compute()}") print(f"Result for Problem 029: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 3 Jan 2020
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 30 of Project Euler Solution for problem 030 of Project Euler
https://projecteuler.net/problem=30 https://projecteuler.net/problem=30
""" """
@ -16,7 +16,7 @@ def power_digit_sum(pow, n):
return sum(int(c) ** pow for c in str(n)) return sum(int(c) ** pow for c in str(n))
@timeit("Problem 30") @timeit("Problem 030")
def compute(): def compute():
""" """
Surprisingly there are only three numbers that can be written as the sum Surprisingly there are only three numbers that can be written as the sum
@ -40,4 +40,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 30 is {compute()}") print(f"Result for Problem 030: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 24 Feb 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 31 of Project Euler Solution for problem 031 of Project Euler
https://projecteuler.net/problem=31 https://projecteuler.net/problem=31
""" """
@ -14,7 +14,7 @@ from itertools import product
from utils import timeit from utils import timeit
@timeit("Problem 31") @timeit("Problem 031")
def compute(): def compute():
""" """
In the United Kingdom the currency is made up of pound (£) and pence (p). In the United Kingdom the currency is made up of pound (£) and pence (p).
@ -29,6 +29,7 @@ def compute():
How many different ways can £2 be made using any number of coins? How many different ways can £2 be made using any number of coins?
""" """
ans = 0 ans = 0
coins = [2, 5, 10, 20, 50, 100] coins = [2, 5, 10, 20, 50, 100]
bunch_of_coins = product(*[range(0, 201, i) for i in coins]) bunch_of_coins = product(*[range(0, 201, i) for i in coins])
@ -42,4 +43,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 31 is {compute()}") print(f"Result for Problem 031: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 26 Feb 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 32 of Project Euler Solution for problem 032 of Project Euler
https://projecteuler.net/problem=32 https://projecteuler.net/problem=32
""" """
from utils import timeit from utils import timeit
@timeit("Problem 32") @timeit("Problem 032")
def compute(): def compute():
""" """
We shall say that an n-digit number is pandigital if it makes use of all We shall say that an n-digit number is pandigital if it makes use of all
@ -41,4 +41,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 32 is {compute()}") print(f"Result for Problem 032: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 04 Mar 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 33 of Project Euler Solution for problem 033 of Project Euler
https://projecteuler.net/problem=33 https://projecteuler.net/problem=33
""" """
from utils import timeit from utils import timeit
@timeit("Problem 33") @timeit("Problem 033")
def compute(): def compute():
""" """
The fraction 49/98 is a curious fraction, as an inexperienced mathematician The fraction 49/98 is a curious fraction, as an inexperienced mathematician
@ -39,9 +39,10 @@ def compute():
if int(str(x)[0]) / int(str(y)[1]) == x / y: if int(str(x)[0]) / int(str(y)[1]) == x / y:
numerator *= x numerator *= x
denominator *= y denominator *= y
ans = int(denominator / numerator)
return int(denominator / numerator) return ans
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 33 is {compute()}") print(f"Result for Problem 033: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 02 Apr 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 34 of Project Euler Solution for problem 034 of Project Euler
https://projecteuler.net/problem=34 https://projecteuler.net/problem=34
""" """
@ -14,7 +14,7 @@ from math import factorial
from utils import timeit from utils import timeit
@timeit("Problem 34") @timeit("Problem 034")
def compute(): def compute():
""" """
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
@ -26,7 +26,6 @@ def compute():
""" """
ans = 0 ans = 0
for num in range(10, 2_540_160): for num in range(10, 2_540_160):
sum_of_factorial = 0 sum_of_factorial = 0
for digit in str(num): for digit in str(num):
@ -38,4 +37,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 34 is {compute()}") print(f"Result for Problem 034: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 02 Apr 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 35 of Project Euler Solution for problem 035 of Project Euler
https://projecteuler.net/problem=35 https://projecteuler.net/problem=35
""" """
@ -17,11 +17,10 @@ def circular_number(number):
ans = [] ans = []
for i in range(len(num_str)): for i in range(len(num_str)):
ans.append(int(num_str[i:] + num_str[:i])) ans.append(int(num_str[i:] + num_str[:i]))
return ans return ans
@timeit("Problem 35") @timeit("Problem 035")
def compute(): def compute():
""" """
The number, 197, is called a circular prime because all rotations of the The number, 197, is called a circular prime because all rotations of the
@ -48,4 +47,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 35 is {compute()}") print(f"Result for Problem 035: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 08 Apr 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 36 of Project Euler Solution for problem 036 of Project Euler
https://projecteuler.net/problem=36 https://projecteuler.net/problem=36
""" """
@ -16,7 +16,7 @@ def is_palidrome(num):
return str(num) == str(num)[::-1] return str(num) == str(num)[::-1]
@timeit("Problem 36") @timeit("Problem 036")
def compute(): def compute():
""" """
The decimal number, 585 = 1001001001_2 (binary), is palindromic The decimal number, 585 = 1001001001_2 (binary), is palindromic
@ -35,4 +35,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 36 is {compute()}") print(f"Result for Problem 036: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 09 Apr 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 37 of Project Euler Solution for problem 037 of Project Euler
https://projecteuler.net/problem=37 https://projecteuler.net/problem=37
""" """
@ -17,11 +17,10 @@ def is_truncatable_prime(number):
for i in range(1, len(num_str)): for i in range(1, len(num_str)):
if not is_prime(int(num_str[i:])) or not is_prime(int(num_str[:-i])): if not is_prime(int(num_str[i:])) or not is_prime(int(num_str[:-i])):
return False return False
return True return True
@timeit("Problem 37") @timeit("Problem 037")
def compute(): def compute():
""" """
The number 3797 has an interesting property. Being prime itself, it is The number 3797 has an interesting property. Being prime itself, it is
@ -46,4 +45,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 37 is {compute()}") print(f"Result for Problem 037: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 03 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 38 of Project Euler Solution for problem 038 of Project Euler
https://projecteuler.net/problem=38 https://projecteuler.net/problem=38
""" """
from utils import timeit from utils import timeit
@timeit("Problem 38") @timeit("Problem 038")
def compute(): def compute():
""" """
Take the number 192 and multiply it by each of 1, 2, and 3: Take the number 192 and multiply it by each of 1, 2, and 3:
@ -44,11 +44,10 @@ def compute():
number += str(integer * i) number += str(integer * i)
if sorted(number) == list("123456789"): if sorted(number) == list("123456789"):
ans.append(number) ans.append(number)
integer += 1 integer += 1
return max(ans) return max(ans)
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 38 is {compute()}") print(f"Result for Problem 038: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 05 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 39 of Project Euler Solution for problem 039 of Project Euler
https://projecteuler.net/problem=39 https://projecteuler.net/problem=39
""" """
from utils import timeit from utils import timeit
@timeit("Problem 39") @timeit("Problem 039")
def compute(): def compute():
""" """
If p is the perimeter of a right angle triangle with integral length sides, If p is the perimeter of a right angle triangle with integral length sides,
@ -42,4 +42,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 39 is {compute()}") print(f"Result for Problem 039: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 23 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 40 of Project Euler Solution for problem 040 of Project Euler
https://projecteuler.net/problem=40 https://projecteuler.net/problem=40
""" """
from utils import timeit from utils import timeit
@timeit("Problem 40") @timeit("Problem 040")
def compute(): def compute():
""" """
An irrational decimal fraction is created by concatenating the positive An irrational decimal fraction is created by concatenating the positive
@ -41,4 +41,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 40 is {compute()}") print(f"Result for Problem 040: {compute()}")

View File

@ -5,14 +5,22 @@ Created on 29 Jun 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 41 of Project Euler Solution for problem 041 of Project Euler
https://projecteuler.net/problem=41 https://projecteuler.net/problem=41
""" """
from utils import is_prime, timeit from utils import is_prime, timeit
@timeit("Problem 41") def is_pandigital(number):
number = sorted(str(number))
check = [str(i) for i in range(1, len(number) + 1)]
if number == check:
return True
return False
@timeit("Problem 041")
def compute(): def compute():
""" """
We shall say that an n-digit number is pandigital if it makes We shall say that an n-digit number is pandigital if it makes
@ -22,14 +30,6 @@ def compute():
What is the largest n-digit pandigital prime that exists? What is the largest n-digit pandigital prime that exists?
""" """
def is_pandigital(number):
number = sorted(str(number))
check = [str(i) for i in range(1, len(number) + 1)]
if number == check:
return True
return False
for ans in range(7654321, 1, -1): for ans in range(7654321, 1, -1):
if is_pandigital(ans): if is_pandigital(ans):
if is_prime(ans): if is_prime(ans):
@ -37,4 +37,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 41 is {compute()}") print(f"Result for Problem 041: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 26 Jul 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 42 of Project Euler Solution for problem 042 of Project Euler
https://projecteuler.net/problem=42 https://projecteuler.net/problem=42
""" """
@ -22,7 +22,7 @@ def word_to_value(word):
return sum(ord(letter) - 64 for letter in word) return sum(ord(letter) - 64 for letter in word)
@timeit("Problem 42") @timeit("Problem 042")
def compute(): def compute():
""" """
The nth term of the sequence of triangle numbers is given by, The nth term of the sequence of triangle numbers is given by,
@ -42,7 +42,7 @@ def compute():
triangular_numbers = [triangle_number(n) for n in range(27)] triangular_numbers = [triangle_number(n) for n in range(27)]
ans = 0 ans = 0
file = Path("../files/Problem42.txt") file = Path("files/Problem42.txt")
with open(file, "r") as f: with open(file, "r") as f:
words = f.readline().strip('"').split('","') words = f.readline().strip('"').split('","')
for word in words: for word in words:
@ -53,4 +53,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 42 is {compute()}") print(f"Result for Problem 042: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 03 Aug 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 43 of Project Euler Solution for problem 043 of Project Euler
https://projecteuler.net/problem=43 https://projecteuler.net/problem=43
""" """
@ -14,7 +14,7 @@ from itertools import permutations
from utils import timeit from utils import timeit
@timeit("Problem 43") @timeit("Problem 043")
def compute(): def compute():
""" """
The number, 1406357289, is a 0 to 9 pandigital number because The number, 1406357289, is a 0 to 9 pandigital number because
@ -34,6 +34,7 @@ def compute():
Find the sum of all 0 to 9 pandigital numbers with this property. Find the sum of all 0 to 9 pandigital numbers with this property.
""" """
ans = [] ans = []
pandigital = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] pandigital = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
@ -53,4 +54,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 43 is {compute()}") print(f"Result for Problem 043: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 30 Aug 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 44 of Project Euler Solution for problem 044 of Project Euler
https://projecteuler.net/problem=44 https://projecteuler.net/problem=44
""" """
@ -19,7 +19,7 @@ def pentagonal(n):
return int(n * (3 * n - 1) / 2) return int(n * (3 * n - 1) / 2)
@timeit("Problem 44") @timeit("Problem 044")
def compute(): def compute():
""" """
Pentagonal numbers are generated by the formula, Pn=n(3n-1)/2. Pentagonal numbers are generated by the formula, Pn=n(3n-1)/2.
@ -49,4 +49,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 44 is {compute()}") print(f"Result for Problem 044: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 09 Sep 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 45 of Project Euler Solution for problem 045 of Project Euler
https://projecteuler.net/problem=45 https://projecteuler.net/problem=45
""" """
@ -20,7 +20,7 @@ def hexagonal(n):
return int(n * (2 * n - 1)) return int(n * (2 * n - 1))
@timeit("Problem 45") @timeit("Problem 045")
def compute(): def compute():
""" """
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
@ -43,4 +43,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 45 is {compute()}") print(f"Result for Problem 045: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 12 Sep 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 46 of Project Euler Solution for problem 046 of Project Euler
https://projecteuler.net/problem=46 https://projecteuler.net/problem=46
""" """
@ -16,11 +16,10 @@ def is_goldbach(number):
for i in range(number - 1, 1, -1): for i in range(number - 1, 1, -1):
if is_prime(i) and ((number - i) / 2) ** 0.5 % 1 == 0: if is_prime(i) and ((number - i) / 2) ** 0.5 % 1 == 0:
return True return True
return False return False
@timeit("Problem 46") @timeit("Problem 046")
def compute(): def compute():
""" """
It was proposed by Christian Goldbach that every odd composite number It was proposed by Christian Goldbach that every odd composite number
@ -47,4 +46,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 46 is {compute()}") print(f"Result for Problem 046: {compute()}")

View File

@ -5,7 +5,7 @@ Created on 12 Sep 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 47 of Project Euler Solution for problem 047 of Project Euler
https://projecteuler.net/problem=47 https://projecteuler.net/problem=47
""" """
@ -23,11 +23,10 @@ def factor(n):
d += 1 d += 1
if n > 1: if n > 1:
ans.append(n) ans.append(n)
return ans return ans
@timeit("Problem 47") @timeit("Problem 047")
def compute(): def compute():
""" """
The first two consecutive numbers to have two distinct prime factors are: The first two consecutive numbers to have two distinct prime factors are:
@ -61,4 +60,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 47 is {compute()}") print(f"Result for Problem 047: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 12 Sep 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 48 of Project Euler Solution for problem 048 of Project Euler
https://projecteuler.net/problem=48 https://projecteuler.net/problem=48
""" """
from utils import timeit from utils import timeit
@timeit("Problem 48") @timeit("Problem 048")
def compute(): def compute():
""" """
The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.
@ -21,8 +21,10 @@ def compute():
""" """
series = sum(i**i for i in range(1, 1001)) series = sum(i**i for i in range(1, 1001))
return str(series)[-10:] ans = str(series)[-10:]
return ans
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 48 is {compute()}") print(f"Result for Problem 048: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 18 Sep 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 49 of Project Euler Solution for problem 049 of Project Euler
https://projecteuler.net/problem=49 https://projecteuler.net/problem=49
""" """
from utils import list_primes, timeit from utils import list_primes, timeit
@timeit("Problem 49") @timeit("Problem 049")
def compute(): def compute():
""" """
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms The arithmetic sequence, 1487, 4817, 8147, in which each of the terms
@ -39,9 +39,10 @@ def compute():
): ):
if number + 3330 in primes_list and number + 6660 in primes_list: if number + 3330 in primes_list and number + 6660 in primes_list:
ans.append(str(number) + str(number + 3300) + str(number + 6660)) ans.append(str(number) + str(number + 3300) + str(number + 6660))
# return the second one # return the second one
return ans[1] return ans[1]
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 49 is {compute()}") print(f"Result for Problem 049: {compute()}")

View File

@ -5,14 +5,14 @@ Created on 18 Sep 2021
@author: David Doblas Jiménez @author: David Doblas Jiménez
@email: daviddoji@pm.me @email: daviddoji@pm.me
Solution for problem 50 of Project Euler Solution for problem 050 of Project Euler
https://projecteuler.net/problem=50 https://projecteuler.net/problem=50
""" """
from utils import is_prime, list_primes, timeit from utils import is_prime, list_primes, timeit
@timeit("Problem 50") @timeit("Problem 050")
def compute(): def compute():
""" """
The prime 41, can be written as the sum of six consecutive primes: The prime 41, can be written as the sum of six consecutive primes:
@ -49,4 +49,4 @@ def compute():
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 50 is {compute()}") print(f"Result for Problem 050: {compute()}")