From 02c15114c44e467fe4f2f5ef4a6e2799b2ec4585 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sun, 5 Apr 2026 20:34:01 +0200 Subject: [PATCH] Cleaner folder structure --- src/Python/Problem051.py | 70 ---------- src/Python/Problem052.py | 38 ------ src/Python/Problem053.py | 45 ------- src/Python/Problem054.py | 163 ----------------------- src/Python/Problem055.py | 66 --------- src/Python/Problem056.py | 37 ----- src/Python/Problem057.py | 50 ------- src/Python/Problem058.py | 55 -------- src/Python/Problem059.py | 64 --------- src/Python/Problem060.py | 56 -------- src/Python/Problem061.py | 83 ------------ src/Python/Problem062.py | 38 ------ src/Python/Problem063.py | 35 ----- src/Python/Problem066.py | 61 --------- src/Python/Problems001-050/Problem001.py | 30 ----- src/Python/Problems001-050/Problem002.py | 41 ------ src/Python/Problems001-050/Problem003.py | 34 ----- src/Python/Problems001-050/Problem004.py | 36 ----- src/Python/Problems001-050/Problem005.py | 42 ------ src/Python/Problems001-050/Problem006.py | 39 ------ src/Python/Problems001-050/Problem007.py | 37 ----- src/Python/Problems001-050/Problem008.py | 79 ----------- src/Python/Problems001-050/Problem009.py | 37 ----- src/Python/Problems001-050/Problem010.py | 29 ---- src/Python/Problems001-050/Problem011.py | 75 ----------- src/Python/Problems001-050/Problem012.py | 66 --------- src/Python/Problems001-050/Problem013.py | 35 ----- src/Python/Problems001-050/Problem014.py | 66 --------- src/Python/Problems001-050/Problem015.py | 34 ----- src/Python/Problems001-050/Problem016.py | 30 ----- src/Python/Problems001-050/Problem017.py | 94 ------------- src/Python/Problems001-050/Problem018.py | 57 -------- src/Python/Problems001-050/Problem019.py | 46 ------- src/Python/Problems001-050/Problem020.py | 36 ----- src/Python/Problems001-050/Problem021.py | 45 ------- src/Python/Problems001-050/Problem022.py | 44 ------ src/Python/Problems001-050/Problem023.py | 61 --------- src/Python/Problems001-050/Problem024.py | 39 ------ src/Python/Problems001-050/Problem025.py | 53 -------- src/Python/Problems001-050/Problem026.py | 53 -------- src/Python/Problems001-050/Problem027.py | 59 -------- src/Python/Problems001-050/Problem028.py | 48 ------- src/Python/Problems001-050/Problem029.py | 41 ------ src/Python/Problems001-050/Problem030.py | 43 ------ src/Python/Problems001-050/Problem031.py | 46 ------- src/Python/Problems001-050/Problem032.py | 44 ------ src/Python/Problems001-050/Problem033.py | 48 ------- src/Python/Problems001-050/Problem034.py | 40 ------ src/Python/Problems001-050/Problem035.py | 50 ------- src/Python/Problems001-050/Problem036.py | 38 ------ src/Python/Problems001-050/Problem037.py | 48 ------- src/Python/Problems001-050/Problem038.py | 53 -------- src/Python/Problems001-050/Problem039.py | 45 ------- src/Python/Problems001-050/Problem040.py | 44 ------ src/Python/Problems001-050/Problem041.py | 40 ------ src/Python/Problems001-050/Problem042.py | 56 -------- src/Python/Problems001-050/Problem043.py | 57 -------- src/Python/Problems001-050/Problem044.py | 52 -------- src/Python/Problems001-050/Problem045.py | 46 ------- src/Python/Problems001-050/Problem046.py | 49 ------- src/Python/Problems001-050/Problem047.py | 63 --------- src/Python/Problems001-050/Problem048.py | 30 ----- src/Python/Problems001-050/Problem049.py | 48 ------- src/Python/Problems001-050/Problem050.py | 52 -------- src/Python/Problems001-050/utils.py | 1 - src/Python/create_template.py | 52 -------- src/Python/utils.py | 54 -------- 67 files changed, 3346 deletions(-) delete mode 100644 src/Python/Problem051.py delete mode 100644 src/Python/Problem052.py delete mode 100644 src/Python/Problem053.py delete mode 100644 src/Python/Problem054.py delete mode 100644 src/Python/Problem055.py delete mode 100644 src/Python/Problem056.py delete mode 100644 src/Python/Problem057.py delete mode 100644 src/Python/Problem058.py delete mode 100644 src/Python/Problem059.py delete mode 100644 src/Python/Problem060.py delete mode 100644 src/Python/Problem061.py delete mode 100644 src/Python/Problem062.py delete mode 100644 src/Python/Problem063.py delete mode 100644 src/Python/Problem066.py delete mode 100644 src/Python/Problems001-050/Problem001.py delete mode 100644 src/Python/Problems001-050/Problem002.py delete mode 100644 src/Python/Problems001-050/Problem003.py delete mode 100644 src/Python/Problems001-050/Problem004.py delete mode 100644 src/Python/Problems001-050/Problem005.py delete mode 100644 src/Python/Problems001-050/Problem006.py delete mode 100644 src/Python/Problems001-050/Problem007.py delete mode 100644 src/Python/Problems001-050/Problem008.py delete mode 100644 src/Python/Problems001-050/Problem009.py delete mode 100644 src/Python/Problems001-050/Problem010.py delete mode 100644 src/Python/Problems001-050/Problem011.py delete mode 100644 src/Python/Problems001-050/Problem012.py delete mode 100644 src/Python/Problems001-050/Problem013.py delete mode 100644 src/Python/Problems001-050/Problem014.py delete mode 100644 src/Python/Problems001-050/Problem015.py delete mode 100644 src/Python/Problems001-050/Problem016.py delete mode 100644 src/Python/Problems001-050/Problem017.py delete mode 100644 src/Python/Problems001-050/Problem018.py delete mode 100644 src/Python/Problems001-050/Problem019.py delete mode 100644 src/Python/Problems001-050/Problem020.py delete mode 100644 src/Python/Problems001-050/Problem021.py delete mode 100644 src/Python/Problems001-050/Problem022.py delete mode 100644 src/Python/Problems001-050/Problem023.py delete mode 100644 src/Python/Problems001-050/Problem024.py delete mode 100644 src/Python/Problems001-050/Problem025.py delete mode 100644 src/Python/Problems001-050/Problem026.py delete mode 100644 src/Python/Problems001-050/Problem027.py delete mode 100644 src/Python/Problems001-050/Problem028.py delete mode 100644 src/Python/Problems001-050/Problem029.py delete mode 100644 src/Python/Problems001-050/Problem030.py delete mode 100644 src/Python/Problems001-050/Problem031.py delete mode 100644 src/Python/Problems001-050/Problem032.py delete mode 100644 src/Python/Problems001-050/Problem033.py delete mode 100644 src/Python/Problems001-050/Problem034.py delete mode 100644 src/Python/Problems001-050/Problem035.py delete mode 100644 src/Python/Problems001-050/Problem036.py delete mode 100644 src/Python/Problems001-050/Problem037.py delete mode 100644 src/Python/Problems001-050/Problem038.py delete mode 100644 src/Python/Problems001-050/Problem039.py delete mode 100644 src/Python/Problems001-050/Problem040.py delete mode 100644 src/Python/Problems001-050/Problem041.py delete mode 100644 src/Python/Problems001-050/Problem042.py delete mode 100644 src/Python/Problems001-050/Problem043.py delete mode 100644 src/Python/Problems001-050/Problem044.py delete mode 100644 src/Python/Problems001-050/Problem045.py delete mode 100644 src/Python/Problems001-050/Problem046.py delete mode 100644 src/Python/Problems001-050/Problem047.py delete mode 100644 src/Python/Problems001-050/Problem048.py delete mode 100644 src/Python/Problems001-050/Problem049.py delete mode 100644 src/Python/Problems001-050/Problem050.py delete mode 120000 src/Python/Problems001-050/utils.py delete mode 100755 src/Python/create_template.py delete mode 100644 src/Python/utils.py diff --git a/src/Python/Problem051.py b/src/Python/Problem051.py deleted file mode 100644 index af73a38..0000000 --- a/src/Python/Problem051.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python -""" -Created on 24 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 051 of Project Euler -https://projecteuler.net/problem=51 -""" - -from utils import is_prime, list_primes, timeit - - -@timeit("Problem 051") -def compute(): - """ - By replacing the 1st digit of the 2-digit number *3, it turns out that - six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime. - - By replacing the 3rd and 4th digits of 56**3 with the same digit, this - 5-digit number is the first example having seven primes among the ten - generated numbers, yielding the family: - - 56003, 56113, 56333, 56443, 56663, 56773, and 56993. - - Consequently 56003, being the first member of this family, is the smallest - prime with this property. - - Find the smallest prime which, by replacing part of the number (not - necessarily adjacent digits) with the same digit, is part of an eight prime - value family. - """ - - primes = sorted(set(list_primes(1_000_000)) - set(list_primes(57_000))) - digits = { - "0": [], - "1": [], - "2": [], - "3": [], - "4": [], - "5": [], - "6": [], - "7": [], - "8": [], - "9": [], - } - for d in digits.keys(): - for p in primes: - p = str(p) - if p.count(d) == 3 and p[-1] != d: - digits[d].append(p) - - for d in {"0", "1", "2"}: - for p in digits[d]: - res = 0 - i = 10 - for D in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"} - {d}: - i -= 1 - q = int(p.replace(d, D)) - if is_prime(q) and q > 57_000: - res += 1 - if i + res < 7: - break - if res == 7: - return p - - -if __name__ == "__main__": - print(f"Result for Problem 051: {compute()}") diff --git a/src/Python/Problem052.py b/src/Python/Problem052.py deleted file mode 100644 index 2ad0ef7..0000000 --- a/src/Python/Problem052.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python -""" -Created on 26 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 052 of Project Euler -https://projecteuler.net/problem=52 -""" - -from utils import timeit - - -@timeit("Problem 052") -def compute(): - """ - It can be seen that the number, 125874, and its double, 251748, - contain exactly the same digits, but in a different order. - - Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, - and 6x, contain the same digits. - """ - - for number in range(123_456, 1_000_000): - if ( - sorted(str(number)) - == sorted(str(2 * number)) - == sorted(str(3 * number)) - == sorted(str(4 * number)) - == sorted(str(5 * number)) - == sorted(str(6 * number)) - ): - return number - - -if __name__ == "__main__": - print(f"Result for Problem 052: {compute()}") diff --git a/src/Python/Problem053.py b/src/Python/Problem053.py deleted file mode 100644 index 0c1a9fe..0000000 --- a/src/Python/Problem053.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -""" -Created on 26 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 053 of Project Euler -https://projecteuler.net/problem=53 -""" - -from math import comb - -from utils import timeit - - -@timeit("Problem 053") -def compute(): - """ - There are exactly ten ways of selecting three from five, 12345: - - 123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 - - In combinatorics, we use the notation, (5 over 3) = 10. - - In general, (n over r) = n!/r!*(n-r)!, where r<=n, n!=n*(n-1)*...*2*1, - and 0!=1. - - It is not until, that a value exceeds one-million: (23 over 10) = 1144066. - - How many, not necessarily distinct, values of (n over r) for 1<=n<=100, - are greater than one-million? - """ - - ans = 0 - for x in range(101): - for y in range(101): - if comb(x, y) > 1_000_000: - ans += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 053: {compute()}") diff --git a/src/Python/Problem054.py b/src/Python/Problem054.py deleted file mode 100644 index 2ce8aaf..0000000 --- a/src/Python/Problem054.py +++ /dev/null @@ -1,163 +0,0 @@ -#!/usr/bin/env python -""" -Created on 27 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 054 of Project Euler -https://projecteuler.net/problem=54 -""" - -from pathlib import Path - -from utils import timeit - - -# 3 help functions. -def replace_values_in_string(text: str, args_dict: dict) -> str: - for k, v in args_dict.items(): - text = text.replace(k, str(v)) - return text - - -def n_of_a_kind(hand: list, n: int) -> int: - return max([x for x in hand if hand.count(x) == n] or [0]) - - -def to_numerical(hand: list) -> list: - return sorted([int(x[:-1]) for x in hand], reverse=True) - - -# 10 Ranks functions. -def high_card(str_hand: list) -> list: - return to_numerical(str_hand) - - -def one_pair(hand: list) -> int: - return n_of_a_kind(hand, 2) - - -def two_pair(hand: list) -> int: - pairs = set([x for x in hand if hand.count(x) == 2]) - return 0 if len(pairs) < 2 else max(pairs) - - -def three_of_a_kind(hand: list) -> int: - return n_of_a_kind(hand, 3) - - -def straight(hand: list) -> int: - return 0 if not list(range(hand[0], hand[-1] - 1, -1)) == hand else max(hand) - - -def flush(str_hand: list) -> bool: - return len(set([x[-1] for x in str_hand])) == 1 - - -def full_house(hand: list) -> int: - return three_of_a_kind(hand) if one_pair(hand) and three_of_a_kind(hand) else 0 - - -def four_of_a_kind(hand: list) -> int: - return n_of_a_kind(hand, 4) - - -def straight_flush(str_hand: list) -> int: - straight_result = straight(to_numerical(str_hand)) - return straight_result if straight_result and flush(str_hand) else 0 - - -def royal_flush(str_hand: list) -> bool: - return flush(str_hand) and list(range(14, 9, -1)) == to_numerical(str_hand) - - -@timeit("Problem 054") -def compute(): - """ - In the card game poker, a hand consists of five cards and are ranked, - from lowest to highest, in the following way: - - High Card: Highest value card. - One Pair: Two cards of the same value. - Two Pairs: Two different pairs. - Three of a Kind: Three cards of the same value. - Straight: All cards are consecutive values. - Flush: All cards of the same suit. - Full House: Three of a kind and a pair. - Four of a Kind: Four cards of the same value. - Straight Flush: All cards are consecutive values of same suit. - Royal Flush: Ten, Jack, Queen, King, Ace, in same suit. - - The cards are valued in the order: - - 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace. - - If two players have the same ranked hands then the rank made up of the - highest value wins; for example, a pair of eights beats a pair of fives - (see example 1 below). But if two ranks tie, for example, both players - have a pair of queens, then highest cards in each hand are compared - (see example 4 below); if the highest cards tie then the next highest - cards are compared, and so on. - - Consider the following five hands dealt to two players: - - Hand Player 1 Player 2 Winner - 1 5H 5C 6S 7S KD 2C 3S 8S 8D TD Player 2 - Pair of Fives Pair of Eights - 2 5D 8C 9S JS AC 2C 5C 7D 8S QH Player 1 - Highest card Ace Highest card Queen - 3 2D 9C AS AH AC 3D 6D 7D TD QD Player 2 - Three Aces Flush with Diamonds - 4 4D 6S 9H QH QC 3D 6D 7H QD QS Player 1 - Pair of Queens Pair of Queens - Highest card Nine Highest card Seven - 5 2H 2D 4C 4D 4S 3C 3D 3S 9S 9D Player 1 - Full House Full House - With Three Fours with Three Threes - - The file, poker.txt, contains one-thousand random hands dealt to two - players. Each line of the file contains ten cards (separated by a single - space): the first five are Player 1's cards and the last five are Player - 2's cards. You can assume that all hands are valid (no invalid characters - or repeated cards), each player's hand is in no specific order, and in - each hand there is a clear winner. - - How many hands does Player 1 win? - """ - - replace_map = {"T": 10, "J": 11, "Q": 12, "K": 13, "A": 14} - score = [0, 0] - - file = Path("files/Problem54.txt") - for line in open(file, "r").read().splitlines(): - line = replace_values_in_string(line, replace_map).split() - hands = line[:5], line[5:] - for rank in ( - royal_flush, - straight_flush, - four_of_a_kind, - full_house, - flush, - straight, - three_of_a_kind, - two_pair, - one_pair, - high_card, - ): - should_convert_hand = ( - "str" not in rank.__code__.co_varnames[0] - ) # Checks parameter name. - result = [ - rank(to_numerical(hand) if should_convert_hand else hand) - for hand in hands - ] - if result[0] != result[1]: - score[0 if result[0] > result[1] else 1] += 1 - break - - return score[0] - - -if __name__ == "__main__": - print(f"Result for Problem 054: {compute()}") diff --git a/src/Python/Problem055.py b/src/Python/Problem055.py deleted file mode 100644 index 975ea7f..0000000 --- a/src/Python/Problem055.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python -""" -Created on 02 Oct 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 055 of Project Euler -https://projecteuler.net/problem=55 -""" - -from utils import is_palindrome, timeit - - -@timeit("Problem 055") -def compute(): - """ - If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. - - Not all numbers produce palindromes so quickly. For example, - - 349 + 943 = 1292, - 1292 + 2921 = 4213 - 4213 + 3124 = 7337 - - That is, 349 took three iterations to arrive at a palindrome. - - Although no one has proved it yet, it is thought that some numbers, like - 196, never produce a palindrome. A number that never forms a palindrome - through the reverse and add process is called a Lychrel number. Due to the - theoretical nature of these numbers, and for the purpose of this problem, - we shall assume that a number is Lychrel until proven otherwise. In - addition you are given that for every number below ten-thousand, it will - either: - (i) become a palindrome in less than fifty iterations, or, - (ii) no one, with all the computing power that exists, has managed so far - to map it to a palindrome. - - In fact, 10677 is the first number to be shown to require over fifty - iterations before producing a palindrome: - - 4668731596684224866951378664 (53 iterations, 28-digits). - - Surprisingly, there are palindromic numbers that are themselves Lychrel - numbers; the first example is 4994. - - How many Lychrel numbers are there below ten-thousand? - """ - - ans = 0 - for n in range(11, 10_000): - num = n - is_lychrel = True - for _ in range(50): - num += int(str(num)[::-1]) - if is_palindrome(num): - is_lychrel = False - break - if is_lychrel: - ans += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 055: {compute()}") diff --git a/src/Python/Problem056.py b/src/Python/Problem056.py deleted file mode 100644 index e81ced5..0000000 --- a/src/Python/Problem056.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python -""" -Created on 07 Oct 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 056 of Project Euler -https://projecteuler.net/problem=56 -""" - -from utils import timeit - - -@timeit("Problem 056") -def compute(): - """ - A googol (10^100) is a massive number: one followed by one-hundred zeros; - 100100 is almost unimaginably large: one followed by two-hundred zeros. - Despite their size, the sum of the digits in each number is only 1. - - Considering natural numbers of the form, a^b, where a, b < 100, what is the - maximum digital sum? - """ - - ans = 0 - for a in range(100): - for b in range(100): - num = sum([int(digit) for digit in str(a**b)]) - if num > ans: - ans = num - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 056: {compute()}") diff --git a/src/Python/Problem057.py b/src/Python/Problem057.py deleted file mode 100644 index 0dc8670..0000000 --- a/src/Python/Problem057.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python -""" -Created on 09 Oct 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 057 of Project Euler -https://projecteuler.net/problem=57 -""" - -from fractions import Fraction - -from utils import timeit - - -@timeit("Problem 057") -def compute(): - """ - It is possible to show that the square root of two can be expressed - as an infinite continued fraction. - - By expanding this for the first four iterations, we get: - - 1 + 1/2 = 3/2 = 1.5 - 1 + 1/2+1/2 = 7/5 = 1.4 - 1 + 1/2+1/2+1/2 = 17/12 = 1.41666... - 1 + 1/2+1/2+1/2+1/2 = 41/29 = 1.41379... - - The next three expansions are 99/70, 239/169, and 577/408, but the eighth - expansion, 1393/985, is the first example where the number of digits in - the numerator exceeds the number of digits in the denominator. - - In the first one-thousand expansions, how many fractions contain a numerator - with more digits than the denominator? - """ - - ans = 0 - f = Fraction(1, 2) - for _ in range(1000): - f = 1 / (2 + f) - result = 1 + f - if len(str(result.numerator)) > len(str(result.denominator)): - ans += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 057: {compute()}") diff --git a/src/Python/Problem058.py b/src/Python/Problem058.py deleted file mode 100644 index 45731f6..0000000 --- a/src/Python/Problem058.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python -""" -Created on 10 Oct 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 058 of Project Euler -https://projecteuler.net/problem=58 -""" - -from utils import is_prime, timeit - - -@timeit("Problem 058") -def compute(): - """ - Starting with 1 and spiralling anticlockwise in the following way, - a square spiral with side length 7 is formed. - - 37 36 35 34 33 32 31 - 38 17 16 15 14 13 30 - 39 18 5 4 3 12 29 - 40 19 6 1 2 11 28 - 41 20 7 8 9 10 27 - 42 21 22 23 24 25 26 - 43 44 45 46 47 48 49 - - It is interesting to note that the odd squares lie along the bottom right - diagonal, but what is more interesting is that 8 out of the 13 numbers - lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%. - - If one complete new layer is wrapped around the spiral above, a square - spiral with side length 9 will be formed. If this process is continued, - what is the side length of the square spiral for which the ratio of primes - along both diagonals first falls below 10%? - """ - - ratio = 1 - corners = [] - side = 0 - num = 1 - - while ratio > 0.1: - side += 2 - for _ in range(4): - num += side - corners.append(is_prime(num)) - ratio = sum(corners) / len(corners) - - return side + 1 - - -if __name__ == "__main__": - print(f"Result for Problem 058: {compute()}") diff --git a/src/Python/Problem059.py b/src/Python/Problem059.py deleted file mode 100644 index 289ab57..0000000 --- a/src/Python/Problem059.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python -""" -Created on 13 Oct 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 059 of Project Euler -https://projecteuler.net/problem=59 -""" - -from itertools import permutations -from string import ascii_lowercase - -from utils import timeit - - -@timeit("Problem 059") -def compute(): - """ - Each character on a computer is assigned a unique code and the preferred - standard is ASCII (American Standard Code for Information Interchange). - For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107. - - A modern encryption method is to take a text file, convert the bytes to - ASCII, then XOR each byte with a given value, taken from a secret key. - The advantage with the XOR function is that using the same encryption key - on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, - then 107 XOR 42 = 65. - - For unbreakable encryption, the key is the same length as the plain text - message, and the key is made up of random bytes. The user would keep the - encrypted message and the encryption key in different locations, and - without both "halves", it is impossible to decrypt the message. - - Unfortunately, this method is impractical for most users, so the modified - method is to use a password as a key. If the password is shorter than the - message, which is likely, the key is repeated cyclically throughout the - message. The balance for this method is using a sufficiently long password - key for security, but short enough to be memorable. - - Your task has been made easy, as the encryption key consists of three - lower case characters. Using p059_cipher.txt, a file containing the - encrypted ASCII codes, and the knowledge that the plain text must contain - common English words, decrypt the message and find the sum of the ASCII - values in the original text. - """ - - with open("files/Problem59.txt", "r") as f: - encrypted = [int(char) for char in f.read().split(",")] - - plain_text = len(encrypted) // 3 - for key in permutations(ascii_lowercase, 3): - decrypted = "" - for k, i in zip(list(key) * plain_text, encrypted): - decrypted += chr(ord(k) ^ i) - - # assuming Euler will be in the text - if "Euler" in decrypted: - return sum([ord(c) for c in decrypted]) - - -if __name__ == "__main__": - print(f"Result for Problem 059: {compute()}") diff --git a/src/Python/Problem060.py b/src/Python/Problem060.py deleted file mode 100644 index 1c2711e..0000000 --- a/src/Python/Problem060.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python -""" -Created on 04 Dic 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 060 of Project Euler -https://projecteuler.net/problem=60 -""" - -from utils import is_prime, list_primes, timeit - - -def solve_backtrack(max_chain_length, chain): - primes_list = list_primes(10_000) - - for p in primes_list[1:]: - if p not in chain and is_concatenable(chain, p): - chain.append(p) - if len(chain) == max_chain_length: - return chain - solve_backtrack(max_chain_length, chain) - chain.pop() - return chain - - -def is_concatenable(chain, candidate): - for n in chain: - if not is_prime(int(str(n) + str(candidate))): - return False - if not is_prime(int(str(candidate) + str(n))): - return False - return True - - -@timeit("Problem 060") -def compute(): - """ - The primes 3, 7, 109, and 673, are quite remarkable. By taking any two - primes and concatenating them in any order the result will always be prime. - For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of - these four primes, 792, represents the lowest sum for a set of four primes - with this property. - - Find the lowest sum for a set of five primes for which any two primes - concatenate to produce another prime. - """ - - ans = solve_backtrack(5, []) - - return sum(ans) - - -if __name__ == "__main__": - print(f"Result for Problem 060: {compute()}") diff --git a/src/Python/Problem061.py b/src/Python/Problem061.py deleted file mode 100644 index 48532bf..0000000 --- a/src/Python/Problem061.py +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env python -""" -Created on 16 Jul 2022 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 061 of Project Euler -https://projecteuler.net/problem=61 -""" - -from itertools import permutations - -from utils import timeit - - -def get_numbers(order: int, number: int) -> int: - return (number * (number - 1) * (order - 2) // 2) + number - - -poligonals: dict[int, set[str]] = { - o: set(str(get_numbers(o, n)) for n in range(150)) for o in range(3, 9) -} - -four_digits_numbers: set[str] = {str(n) for n in range(10**3, 10**4)} - -eligibles: dict[int, list[str]] = { - k: list(v & four_digits_numbers) for k, v in poligonals.items() -} - - -@timeit("Problem 061") -def compute(): - """ - Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers - are all figurate (polygonal) numbers and are generated by the following - formulae: - Triangle P3,n=n(n+1)/2 1, 3, 6, 10, 15, ... - Square P4,n=n2 1, 4, 9, 16, 25, ... - Pentagonal P5,n=n(3n-1)/2 1, 5, 12, 22, 35, ... - Hexagonal P6,n=n(2n-1) 1, 6, 15, 28, 45, ... - Heptagonal P7,n=n(5n-3)/2 1, 7, 18, 34, 55, ... - Octagonal P8,n=n(3n-2) 1, 8, 21, 40, 65, ... - - The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three - interesting properties. - - The set is cyclic, in that the last two digits of each number is the - first two digits of the next number (including the last number with the - first). - Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and - pentagonal (P5,44=2882), is represented by a different number in the - set. - This is the only set of 4-digit numbers with this property. - - Find the sum of the only ordered set of six cyclic 4-digit numbers for - which each polygonal type: triangle, square, pentagonal, hexagonal, - heptagonal, and octagonal, is represented by a different number in the set. - """ - - perms = permutations(eligibles.keys(), 6) - for perm in perms: - for a in eligibles.get(perm[0]): - ans = [] - a1, a2 = a[:2], a[2:] - for b in filter(lambda n: n[:2] == a2, eligibles.get(perm[1])): - b2 = b[2:] - for c in filter(lambda n: n[:2] == b2, eligibles.get(perm[2])): - c2 = c[2:] - for d in filter(lambda n: n[:2] == c2, eligibles.get(perm[3])): - d2 = d[2:] - for e in filter(lambda n: n[:2] == d2, eligibles.get(perm[4])): - e2 = e[2:] - for f in filter( - lambda n: n[:2] == e2 and n[2:] == a1, - eligibles.get(perm[5]), - ): - ans.append([a, b, c, d, e, f]) - return sum(map(int, ans[0])) - - -if __name__ == "__main__": - print(f"Result for Problem 061: {compute()}") diff --git a/src/Python/Problem062.py b/src/Python/Problem062.py deleted file mode 100644 index 48579c4..0000000 --- a/src/Python/Problem062.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python -""" -Created on 25 Jul 2022 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 062 of Project Euler -https://projecteuler.net/problem=62 -""" - -from collections import defaultdict - -from utils import timeit - - -@timeit("Problem 062") -def compute(): - """ - The cube, 41063625 (345^3), can be permuted to produce two other cubes: - 56623104 (384^3) and 66430125 (405^3). In fact, 41063625 is the smallest - cube which has exactly three permutations of its digits which are also - cube. - - Find the smallest cube for which exactly five permutations of its digits - are cube. - """ - - cubes = defaultdict(list) - for number in range(345, 10_000): - cube_str = "".join(sorted(list(str(number**3)))) - cubes[cube_str].append(number**3) - if len(cubes[cube_str]) == 5: - return min(cubes[cube_str]) - - -if __name__ == "__main__": - print(f"Result for Problem 062: {compute()}") diff --git a/src/Python/Problem063.py b/src/Python/Problem063.py deleted file mode 100644 index e7e1a84..0000000 --- a/src/Python/Problem063.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python -""" -Created on 05 Aug 2022 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 063 of Project Euler -https://projecteuler.net/problem=63 -""" - -from utils import timeit - - -@timeit("Problem 063") -def compute(): - """ - The 5-digit number, 16807=7^5, is also a fifth power. Similarly, the - 9-digit number, 134217728=8^9, is a ninth power. - - How many n-digit positive integers exist which are also an nth power? - """ - - ans = 0 - # no need to go higher than 10, because 10**2 = 100 - for number in range(1, 10): - for exp in range(1, 30): - if len(str(number**exp)) == exp: - ans += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 063: {compute()}") diff --git a/src/Python/Problem066.py b/src/Python/Problem066.py deleted file mode 100644 index c833c37..0000000 --- a/src/Python/Problem066.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python -""" -Created on 08 Sep 2023 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 066 from Project Euler -https://projecteuler.net/problem=66 -""" - -from sympy import simplify -from sympy.abc import t, x, y -from sympy.solvers.diophantine.diophantine import diop_quadratic -from utils import timeit - - -@timeit("Problem 066") -def compute(): - """ - Consider quadratic Diophantine equations of the form: - - x^2 - Dy^2 = 1 - - For example, when D = 13, the minimal solution in x is - 649^2 - 13 * 180^2 = 1 - - It can be assumed that there are no solutions in positive integers when D is - square. - - By finding minimal solutions in x for D = {2, 3, 5, 6, 7}, we obtain the - following: - - 3^2 - 2 * 2^2 = 1 - 2^2 - 3 * 1^2 = 1 - 9^2 - 5 * 4^2 = 1 - 5^2 - 6 * 2^2 = 1 - 8^2 - 7 * 3^2 = 1 - - Hence, by considering minimal solutions in x for D <= 7, the largest x is - obtained when D = 5. - - Find the value of D <= 1000 in minimal solutions of x for which the largest - value of x is obtained. - """ - - max_d, max_x = 0, 0 - for d in range(2, 1000): - solve = diop_quadratic(x**2 - d * y**2 - 1, t) - for i in solve: - sol = simplify(i.subs({t: 0})) - xx = sol[0] - if xx > max_x: - max_x = xx - max_d = d - - return max_d - - -if __name__ == "__main__": - print(f"Result for Problem 066 is {compute()}") diff --git a/src/Python/Problems001-050/Problem001.py b/src/Python/Problems001-050/Problem001.py deleted file mode 100644 index 5d53d9f..0000000 --- a/src/Python/Problems001-050/Problem001.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python -""" -Created on 14 Mar 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 001 of Project Euler -https://projecteuler.net/problem=1 -""" - -from utils import timeit - - -@timeit("Problem 001") -def compute(): - """ - If we list all the natural numbers below 10 that are multiples of 3 or 5, - we get 3, 5, 6 and 9. The sum of these multiples is 23. - - Find the sum of all the multiples of 3 or 5 below 1000. - """ - - ans = sum(x for x in range(1000) if (x % 3 == 0 or x % 5 == 0)) - - return ans - - -if __name__ == "__main__": - print(f"Result for problem 001: {compute()}") diff --git a/src/Python/Problems001-050/Problem002.py b/src/Python/Problems001-050/Problem002.py deleted file mode 100644 index ebd3c23..0000000 --- a/src/Python/Problems001-050/Problem002.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python -""" -Created on 14 Mar 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 002 of Project Euler -https://projecteuler.net/problem=2 -""" - -from utils import timeit - - -@timeit("Problem 002") -def compute(): - """ - Each new term in the Fibonacci sequence is generated by adding the - previous two terms. By starting with 1 and 2, the first 10 terms will be: - - 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... - - Find the sum of all the even-valued terms in the sequence which do not - exceed four million. - """ - - ans = 0 - limit = 4_000_000 - x, y = 1, 1 - z = x + y # Because every third Fibonacci number is even - while z <= limit: - ans += z - x = y + z - y = z + x - z = x + y - - return ans - - -if __name__ == "__main__": - print(f"Result for problem 002: {compute()}") diff --git a/src/Python/Problems001-050/Problem003.py b/src/Python/Problems001-050/Problem003.py deleted file mode 100644 index 71d53cc..0000000 --- a/src/Python/Problems001-050/Problem003.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python -""" -Created on 18 Mar 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 003 of Project Euler -https://projecteuler.net/problem=3 -""" - -from utils import timeit - - -@timeit("Problem 003") -def compute(): - """ - The prime factors of 13195 are 5, 7, 13 and 29. - - What is the largest prime factor of the number 600851475143? - """ - - ans = 600851475143 - factor = 2 - while factor * factor < ans: - while ans % factor == 0: - ans = ans // factor - factor += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for problem 003: {compute()}") diff --git a/src/Python/Problems001-050/Problem004.py b/src/Python/Problems001-050/Problem004.py deleted file mode 100644 index ac291a2..0000000 --- a/src/Python/Problems001-050/Problem004.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python -""" -Created on 18 Mar 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 004 of Project Euler -https://projecteuler.net/problem=4 -""" - -from utils import timeit - - -@timeit("Problem 004") -def compute(): - """ - A palindromic number reads the same both ways. The largest palindrome made - from the product of two 2-digit numbers is 9009 = 91 x 99. - - Find the largest palindrome made from the product of two 3-digit numbers. - """ - - ans = 0 - for i in range(100, 1_000): - for j in range(100, 1_000): - palindrome = i * j - s = str(palindrome) - if s == s[::-1] and palindrome > ans: - ans = palindrome - - return ans - - -if __name__ == "__main__": - print(f"Result for problem 004: {compute()}") diff --git a/src/Python/Problems001-050/Problem005.py b/src/Python/Problems001-050/Problem005.py deleted file mode 100644 index 4eba8d6..0000000 --- a/src/Python/Problems001-050/Problem005.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python -""" -Created on 23 Apr 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 005 of Project Euler -https://projecteuler.net/problem=5 -""" - -from math import gcd - -from utils import timeit - -# The LCM of two natural numbers x and y is given by: -# def lcm(x, y): -# return x * y // math.gcd(x, y) - -# It is possible to compute the LCM of more than two numbers by iteratively -# computing the LCM of two numbers, i.e. LCM(a, b, c) = LCM(a, LCM(b, c)) - - -@timeit("Problem 005") -def compute(): - """ - 2520 is the smallest number that can be divided by each of the numbers - from 1 to 10 without any remainder. - - What is the smallest positive number that is evenly divisible by all of - the numbers from 1 to 20? - """ - - ans = 1 - for i in range(1, 21): - ans *= i // gcd(i, ans) - - return ans - - -if __name__ == "__main__": - print(f"Result for problem 005: {compute()}") diff --git a/src/Python/Problems001-050/Problem006.py b/src/Python/Problems001-050/Problem006.py deleted file mode 100644 index 8c31ac7..0000000 --- a/src/Python/Problems001-050/Problem006.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python -""" -Created on 17 Jun 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 006 of Project Euler -https://projecteuler.net/problem=6 -""" - -from utils import timeit - - -@timeit("Problem 006") -def compute(): - """ - The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 - - The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 55^2 = 3025 - - Hence the difference between the sum of the squares of the first ten - natural numbers and the square of the sum is 3025 − 385 = 2640. - - Find the difference between the sum of the squares of the first one - hundred natural numbers and the square of the sum. - """ - - n = 100 - square_of_sum = sum(i for i in range(1, n + 1)) ** 2 - sum_squares = sum(i**2 for i in range(1, n + 1)) - diff = square_of_sum - sum_squares - return diff - - -if __name__ == "__main__": - print(f"Result for Problem 006: {compute()}") diff --git a/src/Python/Problems001-050/Problem007.py b/src/Python/Problems001-050/Problem007.py deleted file mode 100644 index 86174fe..0000000 --- a/src/Python/Problems001-050/Problem007.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python -""" -Created on 28 Jun 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 007 of Project Euler -https://projecteuler.net/problem=7 -""" - -from utils import is_prime, timeit - - -@timeit("Problem 007") -def compute(): - """ - By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see - that the 6th prime is 13. - - What is the 10001st prime number? - """ - - number = 2 - primes = [] - while len(primes) < 10_001: - if is_prime(number): - primes.append(number) - number += 1 - - ans = primes[len(primes) - 1] - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 007: {compute()}") diff --git a/src/Python/Problems001-050/Problem008.py b/src/Python/Problems001-050/Problem008.py deleted file mode 100644 index 7e1fc5c..0000000 --- a/src/Python/Problems001-050/Problem008.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env python -""" -Created on 28 Abr 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 008 of Project Euler -https://projecteuler.net/problem=8 -""" - -import collections -from itertools import islice - -from utils import timeit - - -# recipe from more-itertools -def sliding_window(iterable, n): - # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG - it = iter(iterable) - window = collections.deque(islice(it, n), maxlen=n) - if len(window) == n: - yield tuple(window) - for x in it: - window.append(x) - yield tuple(window) - - -@timeit("Problem 008") -def compute(): - """ - The four adjacent digits in the 1000-digit number that have the - greatest product are 9 x 9 x 8 x 9 = 5832. - - 731671...963450 - - Find the thirteen adjacent digits in the 1000-digit number that have - the greatest product. What is the value of this product? - """ - - NUM = """ - 73167176531330624919225119674426574742355349194934 - 96983520312774506326239578318016984801869478851843 - 85861560789112949495459501737958331952853208805511 - 12540698747158523863050715693290963295227443043557 - 66896648950445244523161731856403098711121722383113 - 62229893423380308135336276614282806444486645238749 - 30358907296290491560440772390713810515859307960866 - 70172427121883998797908792274921901699720888093776 - 65727333001053367881220235421809751254540594752243 - 52584907711670556013604839586446706324415722155397 - 53697817977846174064955149290862569321978468622482 - 83972241375657056057490261407972968652414535100474 - 82166370484403199890008895243450658541227588666881 - 16427171479924442928230863465674813919123162824586 - 17866458359124566529476545682848912883142607690042 - 24219022671055626321111109370544217506941658960408 - 07198403850962455444362981230987879927244284909188 - 84580156166097919133875499200524063689912560717606 - 05886116467109405077541002256983155200055935729725 - 71636269561882670428252483600823257530420752963450 - """ - - num = NUM.replace("\n", "").replace(" ", "") - adjacent_digits = 13 - ans = 0 - for nums in sliding_window(num, adjacent_digits): - prod = 1 - for num in nums: - prod *= int(num) - if prod > ans: - ans = prod - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 008: {compute()}") diff --git a/src/Python/Problems001-050/Problem009.py b/src/Python/Problems001-050/Problem009.py deleted file mode 100644 index 98a09fb..0000000 --- a/src/Python/Problems001-050/Problem009.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python -""" -Created on 26 Aug 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 009 of Project Euler -https://projecteuler.net/problem=9 -""" - -from utils import timeit - - -@timeit("Problem 009") -def compute(): - """ - A Pythagorean triplet is a set of three natural numbers, a < b < c, - for which a^2 + b^2 = c^2 - - For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. - - There exists exactly one Pythagorean triplet for which a + b + c = 1000. - Find the product abc. - """ - - upper_limit = 1000 - for a in range(1, upper_limit + 1): - for b in range(a + 1, upper_limit + 1): - c = upper_limit - a - b - if a * a + b * b == c * c: - # It is now implied that b < c, because we have a > 0 - return a * b * c - - -if __name__ == "__main__": - print(f"Result for Problem 009: {compute()}") diff --git a/src/Python/Problems001-050/Problem010.py b/src/Python/Problems001-050/Problem010.py deleted file mode 100644 index 4b0103d..0000000 --- a/src/Python/Problems001-050/Problem010.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python -""" -Created on 26 Aug 2017 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 010 of Project Euler -https://projecteuler.net/problem=10 -""" - -from utils import list_primes, timeit - - -@timeit("Problem 010") -def compute(): - """ - The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. - - Find the sum of all the primes below two million. - """ - - ans = sum(list_primes(1_999_999)) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 010: {compute()}") diff --git a/src/Python/Problems001-050/Problem011.py b/src/Python/Problems001-050/Problem011.py deleted file mode 100644 index bc69578..0000000 --- a/src/Python/Problems001-050/Problem011.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python -""" -Created on 07 Jul 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 011 of Project Euler -https://projecteuler.net/problem=11 -""" - -from utils import timeit - -GRID = [ - [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8], - [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0], - [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65], - [52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91], - [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80], - [24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50], - [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70], - [67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21], - [24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72], - [21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95], - [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92], - [16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57], - [86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58], - [19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40], - [4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66], - [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69], - [4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36], - [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16], - [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], - [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48], -] - - -def grid_product(x, y, dx, dy, n): - result = 1 - for i in range(n): - result *= GRID[y + i * dy][x + i * dx] - return result - - -@timeit("Problem 011") -def compute(): - """ - In the 20x20 grid above, four numbers along a diagonal line have been - marked in red. - - The product of these numbers is 26 x 63 x 78 x 14 = 1788696. - - What is the greatest product of four adjacent numbers in any direction - (up, down, left, right, or diagonally) in the 20x20 grid? - """ - - ans = 0 - width = height = len(GRID) - adjacent_nums = 4 - for y in range(height): - for x in range(width): - if x + adjacent_nums <= width: - ans = max(grid_product(x, y, 1, 0, adjacent_nums), ans) - if y + adjacent_nums <= height: - ans = max(grid_product(x, y, 0, 1, adjacent_nums), ans) - if x + adjacent_nums <= width and y + adjacent_nums <= height: - ans = max(grid_product(x, y, 1, 1, adjacent_nums), ans) - if x - adjacent_nums >= -1 and y + adjacent_nums <= height: - ans = max(grid_product(x, y, -1, 1, adjacent_nums), ans) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 011: {compute()}") diff --git a/src/Python/Problems001-050/Problem012.py b/src/Python/Problems001-050/Problem012.py deleted file mode 100644 index 9826ba9..0000000 --- a/src/Python/Problems001-050/Problem012.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python -""" -Created on 01 Jan 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 012 of Project Euler -https://projecteuler.net/problem=12 -""" - -from itertools import count -from math import floor, sqrt - -from utils import timeit - - -# Returns the number of integers in the range [1, n] that divide n. -def num_divisors(n): - end = floor(sqrt(n)) - divs = [] - for i in range(1, end + 1): - if n % i == 0: - divs.append(i) - if end**2 == n: - divs.pop() - return 2 * len(divs) - - -@timeit("Problem 012") -def compute(): - """ - The sequence of triangle numbers is generated by adding the natural - numbers. So the 7th triangle number would be: - 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. - - The first ten terms would be: - 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... - - Let us list the factors of the first seven triangle numbers: - - 1: 1 - 3: 1,3 - 6: 1,2,3,6 - 10: 1,2,5,10 - 15: 1,3,5,15 - 21: 1,3,7,21 - 28: 1,2,4,7,14,28 - - We can see that 28 is the first triangle number to have over five divisors. - - What is the value of the first triangle number to have over five hundred - divisors? - """ - - triangle = 0 - for i in count(1): - # This is the ith triangle number, i.e. num = 1 + 2 + ... + i = - # = i*(i+1)/2 - triangle += i - if num_divisors(triangle) > 500: - return str(triangle) - - -if __name__ == "__main__": - print(f"Result for Problem 012: {compute()}") diff --git a/src/Python/Problems001-050/Problem013.py b/src/Python/Problems001-050/Problem013.py deleted file mode 100644 index b2acbfd..0000000 --- a/src/Python/Problems001-050/Problem013.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python -""" -Created on 1 Jan 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 013 of Project Euler -https://projecteuler.net/problem=13 -""" - -from pathlib import Path - -from utils import timeit - - -@timeit("Problem 013") -def compute(): - """ - Work out the first ten digits of the sum of the following one-hundred - 50-digit numbers. - """ - - file = Path("files/Problem13.txt") - with open(file, "r") as f: - num = f.readlines() - ans = 0 - for line in num: - ans += int(line) - - return str(ans)[:10] - - -if __name__ == "__main__": - print(f"Result for Problem 013: {compute()}") diff --git a/src/Python/Problems001-050/Problem014.py b/src/Python/Problems001-050/Problem014.py deleted file mode 100644 index fee5a69..0000000 --- a/src/Python/Problems001-050/Problem014.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python -""" -Created on 7 Jan 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 014 of Project Euler -https://projecteuler.net/problem=14 -""" - -from utils import timeit - - -def chain_length(n, terms): - length = 0 - while n != 1: - if n in terms: - length += terms[n] - break - if n % 2 == 0: - n = n / 2 - else: - n = 3 * n + 1 - length += 1 - return length - - -@timeit("Problem 014") -def compute(): - """ - The following iterative sequence is defined for the set of positive - integers: - - n → n/2 (n is even) - n → 3n + 1 (n is odd) - - Using the rule above and starting with 13, we generate the following - sequence: - - 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 - - It can be seen that this sequence (starting at 13 and finishing at 1) - contains 10 terms. Although it has not been proved yet (Collatz Problem), - it is thought that all starting numbers finish at 1. - - Which starting number, under one million, produces the longest chain? - - NOTE: Once the chain starts the terms are allowed to go above one million. - """ - - ans = 0 - limit = 1_000_000 - score = 0 - terms = dict() - for i in range(1, limit): - terms[i] = chain_length(i, terms) - if terms[i] > score: - score = terms[i] - ans = i - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 014: {compute()}") diff --git a/src/Python/Problems001-050/Problem015.py b/src/Python/Problems001-050/Problem015.py deleted file mode 100644 index 858e1d4..0000000 --- a/src/Python/Problems001-050/Problem015.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python -""" -Created on 7 Jan 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 015 of Project Euler -https://projecteuler.net/problem=15 -""" - -from math import factorial - -from utils import timeit - - -@timeit("Problem 015") -def compute(): - """ - Starting in the top left corner of a 2x2 grid, and only being able to - move to the right and down, there are exactly 6 routes to the bottom - right corner. - - How many such routes are there through a 20x20 grid? - """ - - n = 20 - ans = int(factorial(2 * n) / (factorial(n) * factorial(2 * n - n))) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 015: {compute()}") diff --git a/src/Python/Problems001-050/Problem016.py b/src/Python/Problems001-050/Problem016.py deleted file mode 100644 index e2f00d9..0000000 --- a/src/Python/Problems001-050/Problem016.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python -""" -Created on 13 Jan 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 016 of Project Euler -https://projecteuler.net/problem=16 -""" - -from utils import timeit - - -@timeit("Problem 016") -def compute(): - """ - 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. - - What is the sum of the digits of the number 2^1000? - """ - - n = 1000 - ans = sum(int(digit) for digit in str(2**n)) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 016: {compute()}") diff --git a/src/Python/Problems001-050/Problem017.py b/src/Python/Problems001-050/Problem017.py deleted file mode 100644 index c1d37ee..0000000 --- a/src/Python/Problems001-050/Problem017.py +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env python -""" -Created on 13 Jan 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 017 of Project Euler -https://projecteuler.net/problem=17 -""" - -from utils import timeit - - -def num_to_letters(num): - nums = { - 0: "", - 1: "one", - 2: "two", - 3: "three", - 4: "four", - 5: "five", - 6: "six", - 7: "seven", - 8: "eight", - 9: "nine", - 10: "ten", - 11: "eleven", - 12: "twelve", - 13: "thirteen", - 14: "fourteen", - 15: "fifteen", - 16: "sixteen", - 17: "seventeen", - 18: "eighteen", - 19: "nineteen", - 20: "twenty", - 30: "thirty", - 40: "forty", - 50: "fifty", - 60: "sixty", - 70: "seventy", - 80: "eighty", - 90: "ninety", - 100: "hundred", - 1000: "thousand", - } - - if num <= 20: - return len(nums[num]) - elif num < 100: - tens, units = divmod(num, 10) - return len(nums[tens * 10]) + num_to_letters(units) - elif num < 1000: - hundreds, rest = divmod(num, 100) - if rest: - return ( - num_to_letters(hundreds) - + len(nums[100]) - + len("and") - + num_to_letters(rest) - ) - else: - return num_to_letters(hundreds) + len(nums[100]) - else: - thousands, _ = divmod(num, 1000) - return num_to_letters(thousands) + len(nums[1000]) - - -@timeit("Problem 017") -def compute(): - """ - If the numbers 1 to 5 are written out in words: one, two, three, four, - five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. - - If all the numbers from 1 to 1000 (one thousand) inclusive were written - out in words, how many letters would be used? - - NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and - forty-two) contains 23 letters and 115 (one hundred and fifteen) contains - 20 letters. The use of "and" when writing out numbers is in compliance - with British usage. - """ - - n = 1000 - ans = 0 - for num in range(1, n + 1): - ans += num_to_letters(num) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 017: {compute()}") diff --git a/src/Python/Problems001-050/Problem018.py b/src/Python/Problems001-050/Problem018.py deleted file mode 100644 index 3291229..0000000 --- a/src/Python/Problems001-050/Problem018.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env python -""" -Created on 15 Sep 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 018 of Project Euler -https://projecteuler.net/problem=18 -""" - -from utils import timeit - -triangle = [ - [75], - [95, 64], - [17, 47, 82], - [18, 35, 87, 10], - [20, 4, 82, 47, 65], - [19, 1, 23, 75, 3, 34], - [88, 2, 77, 73, 7, 63, 67], - [99, 65, 4, 28, 6, 16, 70, 92], - [41, 41, 26, 56, 83, 40, 80, 70, 33], - [41, 48, 72, 33, 47, 32, 37, 16, 94, 29], - [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14], - [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57], - [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48], - [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31], - [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23], -] - - -@timeit("Problem 018") -def compute(): - """ - By starting at the top of the triangle below and moving to adjacent - numbers on the row below, the maximum total from top to bottom is 23. - - 3 - 7 4 - 2 4 6 - 8 5 9 3 - - That is, 3 + 7 + 4 + 9 = 23. - - Find the maximum total from top to bottom of the triangle above - """ - - for i in reversed(range(len(triangle) - 1)): - for j in range(len(triangle[i])): - triangle[i][j] += max(triangle[i + 1][j], triangle[i + 1][j + 1]) - - return triangle[0][0] - - -if __name__ == "__main__": - print(f"Result for Problem 018: {compute()}") diff --git a/src/Python/Problems001-050/Problem019.py b/src/Python/Problems001-050/Problem019.py deleted file mode 100644 index 29ab31e..0000000 --- a/src/Python/Problems001-050/Problem019.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python -""" -Created on 15 Sep 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 019 of Project Euler -https://projecteuler.net/problem=19 -""" - -from datetime import date - -from utils import timeit - - -@timeit("Problem 019") -def compute(): - """ - You are given the following information, but you may prefer to do some - research for yourself. - - 1 Jan 1900 was a Monday. - Thirty days has September, April, June and November. - All the rest have thirty-one, - Saving February alone, - Which has twenty-eight, rain or shine. - And on leap years, twenty-nine. - A leap year occurs on any year evenly divisible by 4, but not on a century - unless it is divisible by 400. - - How many Sundays fell on the first of the month during the twentieth - century (1 Jan 1901 to 31 Dec 2000)? - """ - - ans = 0 - for year in range(1901, 2001): - for month in range(1, 13): - if date(year, month, 1).weekday() == 6: - ans += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 019: {compute()}") diff --git a/src/Python/Problems001-050/Problem020.py b/src/Python/Problems001-050/Problem020.py deleted file mode 100644 index 11af585..0000000 --- a/src/Python/Problems001-050/Problem020.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python -""" -Created on 15 Sep 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 020 of Project Euler -https://projecteuler.net/problem=20 -""" - -from math import factorial - -from utils import timeit - - -@timeit("Problem 020") -def compute(): - """ - n! means n x (n - 1) x ... x 3 x 2 x 1 - - For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800, - and the sum of the digits in the number 10! is: - 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. - - Find the sum of the digits in the number 100! - """ - - fact = factorial(100) - ans = sum(int(digit) for digit in str(fact)) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 020: {compute()}") diff --git a/src/Python/Problems001-050/Problem021.py b/src/Python/Problems001-050/Problem021.py deleted file mode 100644 index 81f2e51..0000000 --- a/src/Python/Problems001-050/Problem021.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -""" -Created on 15 Sep 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 021 of Project Euler -https://projecteuler.net/problem=21 -""" - -from utils import timeit - - -def sum_divisors(n): - return sum(i for i in range(1, n // 2 + 1) if n % i == 0) - - -@timeit("Problem 021") -def compute(): - """ - Let d(n) be defined as the sum of proper divisors of n (numbers - less than n which divide evenly into n). - If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable - pair and each of a and b are called amicable numbers. - - For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, - 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are - 1, 2, 4, 71 and 142; so d(284) = 220. - - Evaluate the sum of all the amicable numbers under 10000. - """ - - n = 10_000 - ans = 0 - for i in range(1, n): - value = sum_divisors(i) - if i != value and sum_divisors(value) == i: - ans += i - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 021: {compute()}") diff --git a/src/Python/Problems001-050/Problem022.py b/src/Python/Problems001-050/Problem022.py deleted file mode 100644 index a38fb7e..0000000 --- a/src/Python/Problems001-050/Problem022.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python -""" -Created on 31 Dec 2018 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 022 of Project Euler -https://projecteuler.net/problem=22 -""" - -from pathlib import Path - -from utils import timeit - - -@timeit("Problem 022") -def compute(): - """ - Using names.txt, a 46K text file containing over five-thousand first names, - begin by sorting it into alphabetical order. Then working out the - alphabetical value for each name, multiply this value by its alphabetical - position in the list to obtain a name score. - - For example, when the list is sorted into alphabetical order, COLIN, which - is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, - COLIN would obtain a score of 938 x 53 = 49714. - - What is the total of all the name scores in the file? - """ - - file = Path("files/Problem22.txt") - with open(file, "r") as f: - names = sorted(f.read().replace('"', "").split(",")) - - ans = 0 - for idx, name in enumerate(names, 1): - ans += sum(ord(char) - 64 for char in name) * idx - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 022: {compute()}") diff --git a/src/Python/Problems001-050/Problem023.py b/src/Python/Problems001-050/Problem023.py deleted file mode 100644 index 9c3283a..0000000 --- a/src/Python/Problems001-050/Problem023.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python -""" -Created on 05 Jan 2019 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 023 of Project Euler -https://projecteuler.net/problem=23 -""" - - -from utils import timeit - - -@timeit("Problem 023") -def compute(): - """ - A perfect number is a number for which the sum of its proper divisors is - exactly equal to the number. For example, the sum of the proper divisors - of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect - number. - - A number n is called deficient if the sum of its proper divisors is less - than n and it is called abundant if this sum exceeds n. - - As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest - number that can be written as the sum of two abundant numbers is 24. By - mathematical analysis, it can be shown that all integers greater than 28123 - can be written as the sum of two abundant numbers. However, this upper - limit cannot be reduced any further by analysis even though it is known - that the greatest number that cannot be expressed as the sum of two - abundant numbers is less than this limit. - - Find the sum of all the positive integers which cannot be written as the - sum of two abundant numbers. - """ - - limit = 28124 - divisor_sum = [0] * limit - for i in range(1, limit): - for j in range(i * 2, limit, i): - divisor_sum[j] += i - - abundant_nums = [i for (i, x) in enumerate(divisor_sum) if x > i] - - expressible = [False] * limit - for i in abundant_nums: - for j in abundant_nums: - if i + j < limit: - expressible[i + j] = True - else: - break - - ans = sum(i for (i, x) in enumerate(expressible) if not x) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 023: {compute()}") diff --git a/src/Python/Problems001-050/Problem024.py b/src/Python/Problems001-050/Problem024.py deleted file mode 100644 index 5dc2604..0000000 --- a/src/Python/Problems001-050/Problem024.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python -""" -Created on 11 Sep 2019 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 024 of Project Euler -https://projecteuler.net/problem=24 -""" - -from itertools import permutations - -from utils import timeit - - -@timeit("Problem 024") -def compute(): - """ - A permutation is an ordered arrangement of objects. For example, 3124 is - one possible permutation of the digits 1, 2, 3 and 4. If all of the - permutations are listed numerically or alphabetically, we call it - lexicographic order. The lexicographic permutations of 0, 1 and 2 are: - - 012 021 102 120 201 210 - - What is the millionth lexicographic permutation of the digits - 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? - """ - - digits = list(range(10)) - _permutations = list(permutations(digits)) - ans = "".join(str(digit) for digit in _permutations[999_999]) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 024: {compute()}") diff --git a/src/Python/Problems001-050/Problem025.py b/src/Python/Problems001-050/Problem025.py deleted file mode 100644 index c8ad510..0000000 --- a/src/Python/Problems001-050/Problem025.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python -""" -Created on 11 Sep 2019 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 025 of Project Euler -https://projecteuler.net/problem=25 -""" - -from utils import timeit - - -@timeit("Problem 025") -def compute(): - """ - The Fibonacci sequence is defined by the recurrence relation: - - Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1. - - Hence the first 12 terms will be: - - F1 = 1 - F2 = 1 - F3 = 2 - F4 = 3 - F5 = 5 - F6 = 8 - F7 = 13 - F8 = 21 - F9 = 34 - F10 = 55 - F11 = 89 - F12 = 144 - - The 12th term, F12, is the first term to contain three digits. - - What is the index of the first term in the Fibonacci sequence to - contain 1000 digits? - """ - - a, b = 1, 1 - ans = 2 - while len(str(b)) < 1000: - a, b = b, b + a - ans += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 025: {compute()}") diff --git a/src/Python/Problems001-050/Problem026.py b/src/Python/Problems001-050/Problem026.py deleted file mode 100644 index 0e4c08c..0000000 --- a/src/Python/Problems001-050/Problem026.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python -""" -Created on 11 Sep 2019 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 026 of Project Euler -https://projecteuler.net/problem=26 -""" - -from utils import timeit - - -@timeit("Problem 026") -def compute(): - """ - A unit fraction contains 1 in the numerator. The decimal representation - of the unit fractions with denominators 2 to 10 are given: - - 1/2 = 0.5 - 1/3 = 0.(3) - 1/4 = 0.25 - 1/5 = 0.2 - 1/6 = 0.1(6) - 1/7 = 0.(142857) - 1/8 = 0.125 - 1/9 = 0.(1) - 1/10 = 0.1 - - Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. - It can be seen that 1/7 has a 6-digit recurring cycle. - - Find the value of d < 1000 for which 1/d contains the longest recurring - cycle in its decimal fraction part. - """ - - cycle_length = 0 - ans = 0 - for number in range(3, 1000, 2): - if number % 5 == 0: - continue - p = 1 - while 10**p % number != 1: - p += 1 - if p > cycle_length: - cycle_length, ans = p, number - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 026: {compute()}") diff --git a/src/Python/Problems001-050/Problem027.py b/src/Python/Problems001-050/Problem027.py deleted file mode 100644 index 1f2a7f2..0000000 --- a/src/Python/Problems001-050/Problem027.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python -""" -Created on 15 Sep 2019 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 027 of Project Euler -https://projecteuler.net/problem=27 -""" - -from utils import is_prime, timeit - - -@timeit("Problem 027") -def compute(): - """ - Euler discovered the remarkable quadratic formula: - - n^2 + n + 41 - - It turns out that the formula will produce 40 primes for the consecutive - integer values 0≤n≤39. However, when n=40, 40^2+40+41=40(40+1)+41 is - divisible by 41, and certainly when n=41,41^2+41+41 is clearly divisible - by 41. - - The incredible formula n^2-79n+1601 was discovered, which produces 80 - primes for the consecutive values 0≤n≤79. The product of the coefficients, - -79 and 1601, is -126479. - - Considering quadratics of the form: - - n^2 + an + b - - where |a|<1000, |b|≤1000 and |n| is the modulus/absolute value of n - e.g. |11|=11 and |-4|=4 - - Find the product of the coefficients, a and b, for the quadratic expression - that produces the maximum number of primes for consecutive values of n, - starting with n=0. - """ - - limit = 1000 - consecutive_values = 0 - - for a in range(-999, limit): - for b in range(limit + 1): - n = 0 - while is_prime(abs((n**2) + (a * n) + b)): - n += 1 - if n > consecutive_values: - consecutive_values = n - ans = a * b - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 027: {compute()}") diff --git a/src/Python/Problems001-050/Problem028.py b/src/Python/Problems001-050/Problem028.py deleted file mode 100644 index 21d5ff8..0000000 --- a/src/Python/Problems001-050/Problem028.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python -""" -Created on 3 Jan 2020 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 028 of Project Euler -https://projecteuler.net/problem=28 -""" - -from utils import timeit - - -@timeit("Problem 028") -def compute(): - """ - Starting with the number 1 and moving to the right in a clockwise - direction a 5 by 5 spiral is formed as follows: - - 21 22 23 24 25 - 20 7 8 9 10 - 19 6 1 2 11 - 18 5 4 3 12 - 17 16 15 14 13 - - It can be verified that the sum of the numbers on the diagonals is 101. - - What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral - formed in the same way? - """ - - size = 1001 # Must be odd - ans = 1 # Special case for size 1 - step = 0 - i, current = 1, 1 - while step < size - 1: - step = i * 2 - for _ in range(1, 5): - current += step - ans += current - i += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 028: {compute()}") diff --git a/src/Python/Problems001-050/Problem029.py b/src/Python/Problems001-050/Problem029.py deleted file mode 100644 index 754b5cb..0000000 --- a/src/Python/Problems001-050/Problem029.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python -""" -Created on 3 Jan 2020 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 029 of Project Euler -https://projecteuler.net/problem=29 -""" -from itertools import product - -from utils import timeit - - -@timeit("Problem 029") -def compute(): - """ - Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: - - 2^2=4, 2^3=8, 2^4=16, 2^5=32 - 3^2=9, 3^3=27, 3^4=81, 3^5=243 - 4^2=16, 4^3=64, 4^4=256, 4^5=1024 - 5^2=25, 5^3=125, 5^4=625, 5^5=3125 - - If they are then placed in numerical order, with any repeats removed, we - get the following sequence of 15 distinct terms: - - 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 - - How many distinct terms are in the sequence generated by ab for 2≤a≤100 - and 2≤b≤100? - """ - - ans = len(set(a**b for a, b in product(range(2, 101), repeat=2))) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 029: {compute()}") diff --git a/src/Python/Problems001-050/Problem030.py b/src/Python/Problems001-050/Problem030.py deleted file mode 100644 index cb50ac7..0000000 --- a/src/Python/Problems001-050/Problem030.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env python -""" -Created on 3 Jan 2020 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 030 of Project Euler -https://projecteuler.net/problem=30 -""" - -from utils import timeit - - -def power_digit_sum(pow, n): - return sum(int(c) ** pow for c in str(n)) - - -@timeit("Problem 030") -def compute(): - """ - Surprisingly there are only three numbers that can be written as the sum - of fourth powers of their digits: - - 1634 = 1^4 + 6^4 + 3^4 + 4^4 - 8208 = 8^4 + 2^4 + 0^4 + 8^4 - 9474 = 9^4 + 4^4 + 7^4 + 4^4 - - As 1 = 14 is not a sum it is not included. - - The sum of these numbers is 1634 + 8208 + 9474 = 19316. - - Find the sum of all the numbers that can be written as the sum of fifth - powers of their digits. - """ - - ans = sum(i for i in range(2, 1_000_000) if i == power_digit_sum(5, i)) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 030: {compute()}") diff --git a/src/Python/Problems001-050/Problem031.py b/src/Python/Problems001-050/Problem031.py deleted file mode 100644 index c648a7b..0000000 --- a/src/Python/Problems001-050/Problem031.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python -""" -Created on 24 Feb 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 031 of Project Euler -https://projecteuler.net/problem=31 -""" - -from itertools import product - -from utils import timeit - - -@timeit("Problem 031") -def compute(): - """ - In the United Kingdom the currency is made up of pound (£) and pence (p). - There are eight coins in general circulation: - - 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p). - - It is possible to make £2 in the following way: - - 1x£1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p - - How many different ways can £2 be made using any number of coins? - - """ - - ans = 0 - coins = [2, 5, 10, 20, 50, 100] - bunch_of_coins = product(*[range(0, 201, i) for i in coins]) - - for money in bunch_of_coins: - if sum(money) <= 200: - ans += 1 - - # consider also the case for 200 coins of 1p - return ans + 1 - - -if __name__ == "__main__": - print(f"Result for Problem 031: {compute()}") diff --git a/src/Python/Problems001-050/Problem032.py b/src/Python/Problems001-050/Problem032.py deleted file mode 100644 index 7773775..0000000 --- a/src/Python/Problems001-050/Problem032.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python -""" -Created on 26 Feb 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 032 of Project Euler -https://projecteuler.net/problem=32 -""" - -from utils import timeit - - -@timeit("Problem 032") -def compute(): - """ - We shall say that an n-digit number is pandigital if it makes use of all - the digits 1 to n exactly once; for example, the 5-digit number, 15234, is - 1 through 5 pandigital. - - The product 7254 is unusual, as the identity, 39 x 186 = 7254, containing - multiplicand, multiplier, and product is 1 through 9 pandigital. - - Find the sum of all products whose multiplicand/multiplier/product identity - can be written as a 1 through 9 pandigital. - HINT: Some products can be obtained in more than one way so be sure to only - include it once in your sum. - """ - - ans = set() - pandigital = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] - - for x in range(1, 100): - for y in range(100, 10_000): - # product = x * y - if sorted(str(x) + str(y) + str(x * y)) == pandigital: - ans.add(x * y) - - return sum(ans) - - -if __name__ == "__main__": - print(f"Result for Problem 032: {compute()}") diff --git a/src/Python/Problems001-050/Problem033.py b/src/Python/Problems001-050/Problem033.py deleted file mode 100644 index dbe6d97..0000000 --- a/src/Python/Problems001-050/Problem033.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python -""" -Created on 04 Mar 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 033 of Project Euler -https://projecteuler.net/problem=33 -""" - -from utils import timeit - - -@timeit("Problem 033") -def compute(): - """ - The fraction 49/98 is a curious fraction, as an inexperienced mathematician - in attempting to simplify it may incorrectly believe that 49/98 = 4/8, - which is correct, is obtained by cancelling the 9s. - - We shall consider fractions like, 30/50 = 3/5, to be trivial examples. - - There are exactly four non-trivial examples of this type of fraction, less - than one in value, and containing two digits in the numerator and - denominator. - - If the product of these four fractions is given in its lowest common terms, - find the value of the denominator. - """ - - numerator = 1 - denominator = 1 - for x in range(10, 100): - for y in range(10, 100): - if x < y: - if str(x)[1] == str(y)[0]: - if int(str(y)[1]) != 0: - if int(str(x)[0]) / int(str(y)[1]) == x / y: - numerator *= x - denominator *= y - ans = int(denominator / numerator) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 033: {compute()}") diff --git a/src/Python/Problems001-050/Problem034.py b/src/Python/Problems001-050/Problem034.py deleted file mode 100644 index 07fd77a..0000000 --- a/src/Python/Problems001-050/Problem034.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python -""" -Created on 02 Apr 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 034 of Project Euler -https://projecteuler.net/problem=34 -""" - -from math import factorial - -from utils import timeit - - -@timeit("Problem 034") -def compute(): - """ - 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. - - Find the sum of all numbers which are equal to the sum of the factorial - of their digits. - - Note: As 1! = 1 and 2! = 2 are not sums they are not included. - """ - - ans = 0 - for num in range(10, 2_540_160): - sum_of_factorial = 0 - for digit in str(num): - sum_of_factorial += factorial(int(digit)) - if sum_of_factorial == num: - ans += sum_of_factorial - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 034: {compute()}") diff --git a/src/Python/Problems001-050/Problem035.py b/src/Python/Problems001-050/Problem035.py deleted file mode 100644 index c43e9e6..0000000 --- a/src/Python/Problems001-050/Problem035.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python -""" -Created on 02 Apr 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 035 of Project Euler -https://projecteuler.net/problem=35 -""" - -from utils import is_prime, timeit - - -def circular_number(number): - num_str = str(number) - ans = [] - for i in range(len(num_str)): - ans.append(int(num_str[i:] + num_str[:i])) - return ans - - -@timeit("Problem 035") -def compute(): - """ - The number, 197, is called a circular prime because all rotations of the - digits: 197, 971, and 719, are themselves prime. - - There are thirteen such primes below 100: - 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. - - How many circular primes are there below one million? - """ - - ans = [] - for i in range(2, 1_000_000): - if is_prime(i): - all_primes = True - for j in circular_number(i): - if not is_prime(j): - all_primes = False - break - if all_primes: - ans.append(i) - - return len(ans) - - -if __name__ == "__main__": - print(f"Result for Problem 035: {compute()}") diff --git a/src/Python/Problems001-050/Problem036.py b/src/Python/Problems001-050/Problem036.py deleted file mode 100644 index c9ea998..0000000 --- a/src/Python/Problems001-050/Problem036.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python -""" -Created on 08 Apr 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 036 of Project Euler -https://projecteuler.net/problem=36 -""" - -from utils import timeit - - -def is_palidrome(num): - return str(num) == str(num)[::-1] - - -@timeit("Problem 036") -def compute(): - """ - The decimal number, 585 = 1001001001_2 (binary), is palindromic - in both bases. - - Find the sum of all numbers, less than one million, which are palindromic - in base 10 and base 2. - """ - - ans = 0 - for i in range(1, 1_000_001, 2): - if is_palidrome(i) and is_palidrome(bin(i)[2:]): - ans += i - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 036: {compute()}") diff --git a/src/Python/Problems001-050/Problem037.py b/src/Python/Problems001-050/Problem037.py deleted file mode 100644 index 31bcf32..0000000 --- a/src/Python/Problems001-050/Problem037.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python -""" -Created on 09 Apr 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 037 of Project Euler -https://projecteuler.net/problem=37 -""" - -from utils import is_prime, list_primes, timeit - - -def is_truncatable_prime(number): - num_str = str(number) - for i in range(1, len(num_str)): - if not is_prime(int(num_str[i:])) or not is_prime(int(num_str[:-i])): - return False - return True - - -@timeit("Problem 037") -def compute(): - """ - The number 3797 has an interesting property. Being prime itself, it is - possible to continuously remove digits from left to right, and remain - prime at each stage: 3797, 797, 97, and 7. - Similarly we can work from right to left: 3797, 379, 37, and 3. - - Find the sum of the only eleven primes that are both truncatable from left - to right and right to left. - - NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. - """ - - ans = 0 - primes = list_primes(1_000_000) - # Statement of the problem says this - for number in primes[4:]: - if is_truncatable_prime(number): - ans += number - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 037: {compute()}") diff --git a/src/Python/Problems001-050/Problem038.py b/src/Python/Problems001-050/Problem038.py deleted file mode 100644 index 9cb3a5d..0000000 --- a/src/Python/Problems001-050/Problem038.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python -""" -Created on 03 Jun 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 038 of Project Euler -https://projecteuler.net/problem=38 -""" - -from utils import timeit - - -@timeit("Problem 038") -def compute(): - """ - Take the number 192 and multiply it by each of 1, 2, and 3: - - 192 x 1 = 192 - 192 x 2 = 384 - 192 x 3 = 576 - - By concatenating each product we get the 1 to 9 pandigital, - 192384576. We will call 192384576 the concatenated product of - 192 and (1,2,3) - - The same can be achieved by starting with 9 and multiplying by - 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is - the concatenated product of 9 and (1,2,3,4,5). - - What is the largest 1 to 9 pandigital 9-digit number that can - be formed as the concatenated product of an integer with - (1,2, ... , n) where n > 1? - """ - - ans = [] - # Number must 4 digits (exactly) to be pandigital - # if n > 1 - for i in range(1, 10_000): - integer = 1 - number = "" - while len(number) < 9: - number += str(integer * i) - if sorted(number) == list("123456789"): - ans.append(number) - integer += 1 - - return max(ans) - - -if __name__ == "__main__": - print(f"Result for Problem 038: {compute()}") diff --git a/src/Python/Problems001-050/Problem039.py b/src/Python/Problems001-050/Problem039.py deleted file mode 100644 index 478cb18..0000000 --- a/src/Python/Problems001-050/Problem039.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -""" -Created on 05 Jun 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 039 of Project Euler -https://projecteuler.net/problem=39 -""" - -from utils import timeit - - -@timeit("Problem 039") -def compute(): - """ - If p is the perimeter of a right angle triangle with integral length sides, - {a,b,c}, there are exactly three solutions for p = 120: - - {20,48,52}, {24,45,51}, {30,40,50} - - For which value of p ≤ 1000, is the number of solutions maximised? - """ - - ans, val = 0, 0 - for p in range(2, 1001, 2): - sol = 0 - for a in range(1, p): - for b in range(a + 1, p - 2 * a): - c = p - (a + b) - if a**2 + b**2 == c**2: - sol += 1 - elif a**2 + b**2 > c**2: - # As we continue our innermost loop, the left side - # gets bigger, right gets smaller, so we're done here - break - if sol > ans: - ans, val = sol, p - - return val - - -if __name__ == "__main__": - print(f"Result for Problem 039: {compute()}") diff --git a/src/Python/Problems001-050/Problem040.py b/src/Python/Problems001-050/Problem040.py deleted file mode 100644 index c0f3ce9..0000000 --- a/src/Python/Problems001-050/Problem040.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python -""" -Created on 23 Jun 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 040 of Project Euler -https://projecteuler.net/problem=40 -""" - -from utils import timeit - - -@timeit("Problem 040") -def compute(): - """ - An irrational decimal fraction is created by concatenating the positive - integers: - - 0.123456789101112131415161718192021... - - It can be seen that the 12th digit of the fractional part is 1. - - If d_n represents the n^th digit of the fractional part, find the value of - the following expression. - - d_1 x d_{10} x d_{100} x d_{1_000} x d_{10_000} x d_{100_000} - x d_{1_000_000} - """ - - fraction = "" - for i in range(1, 1_000_000): - fraction += str(i) - - ans = 1 - for i in range(7): - ans *= int(fraction[10**i - 1]) - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 040: {compute()}") diff --git a/src/Python/Problems001-050/Problem041.py b/src/Python/Problems001-050/Problem041.py deleted file mode 100644 index 924047e..0000000 --- a/src/Python/Problems001-050/Problem041.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python -""" -Created on 29 Jun 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 041 of Project Euler -https://projecteuler.net/problem=41 -""" - -from utils import is_prime, timeit - - -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(): - """ - We shall say that an n-digit number is pandigital if it makes - use of all the digits 1 to n exactly once. For example, 2143 is - a 4-digit pandigital and is also prime. - - What is the largest n-digit pandigital prime that exists? - """ - - for ans in range(7654321, 1, -1): - if is_pandigital(ans): - if is_prime(ans): - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 041: {compute()}") diff --git a/src/Python/Problems001-050/Problem042.py b/src/Python/Problems001-050/Problem042.py deleted file mode 100644 index 0b303e4..0000000 --- a/src/Python/Problems001-050/Problem042.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python -""" -Created on 26 Jul 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 042 of Project Euler -https://projecteuler.net/problem=42 -""" - -from pathlib import Path - -from utils import timeit - - -def triangle_number(num): - return int(0.5 * num * (num + 1)) - - -def word_to_value(word): - return sum(ord(letter) - 64 for letter in word) - - -@timeit("Problem 042") -def compute(): - """ - The nth term of the sequence of triangle numbers is given by, - tn = n(n+1)/2; so the first ten triangle numbers are: - - 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... - - By converting each letter in a word to a number corresponding to its - alphabetical position and adding these values we form a word value. For - example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word - value is a triangle number then we shall call the word a triangle word. - - Using words.txt, a 16K text file containing nearly two-thousand common - English words, how many are triangle words? - """ - - triangular_numbers = [triangle_number(n) for n in range(27)] - ans = 0 - - file = Path("files/Problem42.txt") - with open(file, "r") as f: - words = f.readline().strip('"').split('","') - for word in words: - if word_to_value(word) in triangular_numbers: - ans += 1 - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 042: {compute()}") diff --git a/src/Python/Problems001-050/Problem043.py b/src/Python/Problems001-050/Problem043.py deleted file mode 100644 index 93041e2..0000000 --- a/src/Python/Problems001-050/Problem043.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env python -""" -Created on 03 Aug 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 043 of Project Euler -https://projecteuler.net/problem=43 -""" - -from itertools import permutations - -from utils import timeit - - -@timeit("Problem 043") -def compute(): - """ - The number, 1406357289, is a 0 to 9 pandigital number because - it is made up of each of the digits 0 to 9 in some order, but - it also has a rather interesting sub-string divisibility property. - - Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this - way, we note the following: - - d2d3d4=406 is divisible by 2 - d3d4d5=063 is divisible by 3 - d4d5d6=635 is divisible by 5 - d5d6d7=357 is divisible by 7 - d6d7d8=572 is divisible by 11 - d7d8d9=728 is divisible by 13 - d8d9d10=289 is divisible by 17 - - Find the sum of all 0 to 9 pandigital numbers with this property. - """ - - ans = [] - pandigital = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] - - for n in permutations(pandigital): - n_ = "".join(n) - if n_[0] != "0" and sorted("".join(n_)) == pandigital: - if int(n_[7:]) % 17 == 0: - if int(n_[6:9]) % 13 == 0: - if int(n_[5:8]) % 11 == 0: - if int(n_[4:7]) % 7 == 0: - if int(n_[3:6]) % 5 == 0: - if int(n_[2:5]) % 3 == 0: - if int(n_[1:4]) % 2 == 0: - ans.append(int(n_)) - - return sum(ans) - - -if __name__ == "__main__": - print(f"Result for Problem 043: {compute()}") diff --git a/src/Python/Problems001-050/Problem044.py b/src/Python/Problems001-050/Problem044.py deleted file mode 100644 index 02208b5..0000000 --- a/src/Python/Problems001-050/Problem044.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python -""" -Created on 30 Aug 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 044 of Project Euler -https://projecteuler.net/problem=44 -""" - -from itertools import combinations -from operator import add, sub - -from utils import timeit - - -def pentagonal(n): - return int(n * (3 * n - 1) / 2) - - -@timeit("Problem 044") -def compute(): - """ - Pentagonal numbers are generated by the formula, Pn=n(3n-1)/2. - The first ten pentagonal numbers are: - - 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... - - It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their - difference, 70 - 22 = 48, is not pentagonal. - - Find the pair of pentagonal numbers, Pj and Pk, for which their - sum and difference are pentagonal and D = |Pk - Pj| is minimised. - - What is the value of D? - """ - - ans = 0 - pentagonal_list = set(pentagonal(n) for n in range(1, 2500)) - pairs = combinations(pentagonal_list, 2) - for p in pairs: - if add(*p) in pentagonal_list and abs(sub(*p)) in pentagonal_list: - ans = abs(sub(*p)) - # the first one found would be the smallest - break - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 044: {compute()}") diff --git a/src/Python/Problems001-050/Problem045.py b/src/Python/Problems001-050/Problem045.py deleted file mode 100644 index 7548009..0000000 --- a/src/Python/Problems001-050/Problem045.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python -""" -Created on 09 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 045 of Project Euler -https://projecteuler.net/problem=45 -""" - -from utils import timeit - - -def pentagonal(n): - return int(n * (3 * n - 1) / 2) - - -def hexagonal(n): - return int(n * (2 * n - 1)) - - -@timeit("Problem 045") -def compute(): - """ - Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: - Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ... - Pentagonal Pn=n(3n-1)/2 1, 5, 12, 22, 35, ... - Hexagonal Hn=n(2n-1) 1, 6, 15, 28, 45, ... - - It can be verified that T285 = P165 = H143 = 40755. - - Find the next triangle number that is also pentagonal and hexagonal. - """ - - pentagonal_list = set(pentagonal(n) for n in range(2, 100_000)) - # all hexagonal numbers are also triangle numbers! - hexagonal_list = set(hexagonal(n) for n in range(2, 100_000)) - - ans = sorted(hexagonal_list & pentagonal_list) - # First one is already known - return ans[1] - - -if __name__ == "__main__": - print(f"Result for Problem 045: {compute()}") diff --git a/src/Python/Problems001-050/Problem046.py b/src/Python/Problems001-050/Problem046.py deleted file mode 100644 index df8dd1f..0000000 --- a/src/Python/Problems001-050/Problem046.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env python -""" -Created on 12 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 046 of Project Euler -https://projecteuler.net/problem=46 -""" - -from utils import is_prime, timeit - - -def is_goldbach(number): - for i in range(number - 1, 1, -1): - if is_prime(i) and ((number - i) / 2) ** 0.5 % 1 == 0: - return True - return False - - -@timeit("Problem 046") -def compute(): - """ - It was proposed by Christian Goldbach that every odd composite number - can be written as the sum of a prime and twice a square. - - 9 = 7 + 2x1^2 - 15 = 7 + 2x2^2 - 21 = 3 + 2x3^2 - 25 = 7 + 2x3^2 - 27 = 19 + 2x2^2 - 33 = 31 + 2x1^2 - - It turns out that the conjecture was false. - - What is the smallest odd composite that cannot be written as the sum - of a prime and twice a square? - """ - - ans = 9 - while True: - ans += 2 - if not is_prime(ans) and not is_goldbach(ans): - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 046: {compute()}") diff --git a/src/Python/Problems001-050/Problem047.py b/src/Python/Problems001-050/Problem047.py deleted file mode 100644 index dda4b6e..0000000 --- a/src/Python/Problems001-050/Problem047.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python -""" -Created on 12 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 047 of Project Euler -https://projecteuler.net/problem=47 -""" - -from utils import timeit - - -def factor(n): - ans = [] - d = 2 - while d * d <= n: - if n % d == 0: - ans.append(d) - n //= d - else: - d += 1 - if n > 1: - ans.append(n) - return ans - - -@timeit("Problem 047") -def compute(): - """ - The first two consecutive numbers to have two distinct prime factors are: - - 14 = 2 x 7 - 15 = 3 x 5 - - The first three consecutive numbers to have three distinct prime factors - are: - - 644 = 2² x 7 x 23 - 645 = 3 x 5 x 43 - 646 = 2 x 17 x 19. - - Find the first four consecutive integers to have four distinct prime - factors each. - - What is the first of these numbers? - """ - - ans = [] - for number in range(1, 1_000_000): - if len(ans) == 4: - break - elif len(set(factor(number))) == 4: - ans.append(number) - else: - ans = [] - - return ans[0] - - -if __name__ == "__main__": - print(f"Result for Problem 047: {compute()}") diff --git a/src/Python/Problems001-050/Problem048.py b/src/Python/Problems001-050/Problem048.py deleted file mode 100644 index 9aadeee..0000000 --- a/src/Python/Problems001-050/Problem048.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python -""" -Created on 12 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 048 of Project Euler -https://projecteuler.net/problem=48 -""" - -from utils import timeit - - -@timeit("Problem 048") -def compute(): - """ - The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. - - Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. - """ - - series = sum(i**i for i in range(1, 1001)) - ans = str(series)[-10:] - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 048: {compute()}") diff --git a/src/Python/Problems001-050/Problem049.py b/src/Python/Problems001-050/Problem049.py deleted file mode 100644 index a61009f..0000000 --- a/src/Python/Problems001-050/Problem049.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python -""" -Created on 18 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 049 of Project Euler -https://projecteuler.net/problem=49 -""" - -from utils import list_primes, timeit - - -@timeit("Problem 049") -def compute(): - """ - The arithmetic sequence, 1487, 4817, 8147, in which each of the terms - increases by 3330, is unusual in two ways: - (i) each of the three terms are prime, and, - (ii) each of the 4-digit numbers are permutations of one another. - - There are no arithmetic sequences made up of three 1-, 2-, or 3-digit - primes, exhibiting this property, but there is one other 4-digit increasing - sequence. - - What 12-digit number do you form by concatenating the three terms in this - sequence? - """ - - ans = [] - primes_list = sorted(set(list_primes(10_000)) - set(list_primes(1_000))) - - for number in primes_list: - if ( - set(list(str(number))) - == set(list(str(number + 3330))) - == set(list(str(number + 6660))) - ): - if number + 3330 in primes_list and number + 6660 in primes_list: - ans.append(str(number) + str(number + 3300) + str(number + 6660)) - - # return the second one - return ans[1] - - -if __name__ == "__main__": - print(f"Result for Problem 049: {compute()}") diff --git a/src/Python/Problems001-050/Problem050.py b/src/Python/Problems001-050/Problem050.py deleted file mode 100644 index 2475b4f..0000000 --- a/src/Python/Problems001-050/Problem050.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python -""" -Created on 18 Sep 2021 - -@author: David Doblas Jiménez -@email: daviddoji@pm.me - -Solution for problem 050 of Project Euler -https://projecteuler.net/problem=50 -""" - -from utils import is_prime, list_primes, timeit - - -@timeit("Problem 050") -def compute(): - """ - The prime 41, can be written as the sum of six consecutive primes: - - 41 = 2 + 3 + 5 + 7 + 11 + 13 - - This is the longest sum of consecutive primes that adds to a prime below - one-hundred. - - The longest sum of consecutive primes below one-thousand that adds to a - prime, contains 21 terms, and is equal to 953. - - Which prime, below one-million, can be written as the sum of the most - consecutive primes? - """ - - ans = 0 - result = 0 - prime_list = list_primes(1_000_000) - - for i in range(len(prime_list)): - sum = 0 - count = 0 - for j in prime_list[i:]: - sum += j - count += 1 - if is_prime(sum) and count > result: - result = count - ans = sum - if sum > 1_000_000: - break - - return ans - - -if __name__ == "__main__": - print(f"Result for Problem 050: {compute()}") diff --git a/src/Python/Problems001-050/utils.py b/src/Python/Problems001-050/utils.py deleted file mode 120000 index 50fbc6d..0000000 --- a/src/Python/Problems001-050/utils.py +++ /dev/null @@ -1 +0,0 @@ -../utils.py \ No newline at end of file diff --git a/src/Python/create_template.py b/src/Python/create_template.py deleted file mode 100755 index 6c0aa72..0000000 --- a/src/Python/create_template.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python -""" -Creation of templates for the problems of Project Euler -""" - -import datetime -import inspect -from argparse import ArgumentParser - - -def create_problem(): - with open(Problem, "w+") as f: - template = inspect.cleandoc( - f'''#!/usr/bin/env python - """ - Created on {today} - - @author: David Doblas Jiménez - @email: daviddoji@pm.me - - Solution for problem {(args['problem']):0>3} from Project Euler - https://projecteuler.net/problem={args['problem']} - """ - - from utils import timeit - - - @timeit("Problem {(args['problem']):0>3}") - def compute(): - """ - # Statement - """ - - # Your code goes here - - - if __name__ == "__main__": - print(f"Result for Problem {(args['problem']):0>3} is {{compute()}}") - ''' # noqa: E501 - ) - - f.write(template) - - -if __name__ == "__main__": - today = datetime.datetime.now().strftime("%d %b %Y") - parser = ArgumentParser(description=__doc__) - # Add your arguments here - parser.add_argument("-p", "--problem", help="number of the problem to solve") - args = vars(parser.parse_args()) - Problem = f"Problem{(args['problem']):0>3}.py" - create_problem() diff --git a/src/Python/utils.py b/src/Python/utils.py deleted file mode 100644 index 5685717..0000000 --- a/src/Python/utils.py +++ /dev/null @@ -1,54 +0,0 @@ -import math -from functools import wraps - - -def timeit(name): - def profile(original): - import time - - @wraps(original) - def wrapper(*args, **kwargs): - t0 = time.perf_counter() - result = original(*args, **kwargs) - t1 = time.perf_counter() - if (t1 - t0) > 1: - print(f"Took: {(t1 - t0):.3f} s\n") - else: - print(f"Took: {(t1 - t0)*1000:.3f} ms\n") - return result - - return wrapper - - return profile - - -def is_prime(n): - if n < 2: - return False - for i in range(2, int(math.sqrt(n)) + 1): - if n % i == 0: - return False - return True - - -# Returns a list of True and False indicating whether each number is prime. -# For 0 <= i <= n, result[i] is True if i is a prime number, False otherwise. -def list_primality(n): - # Sieve of Eratosthenes - result = [True] * (n + 1) - result[0] = result[1] = False - for i in range(int(math.sqrt(n) + 1)): - if result[i]: - for j in range(i * i, len(result), i): - result[j] = False - return result - - -# Returns all the prime numbers less than or equal to n, in ascending order -# For example: list_primes(97) = [2, 3, 5, 7, 11, ..., 83, 89, 97]. -def list_primes(n): - return [i for (i, is_prime) in enumerate(list_primality(n)) if is_prime] - - -def is_palindrome(num): - return str(num) == str(num)[::-1]