mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 02:13:15 +08:00
[pre-commit.ci] pre-commit autoupdate (#11322)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
![66853113+pre-commit-ci[bot]@users.noreply.github.com](/assets/img/avatar_default.png)
committed by
GitHub

parent
5f95d6f805
commit
bc8df6de31
@ -14,6 +14,7 @@ four million, find the sum of the even-valued terms.
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Fibonacci_number
|
||||
"""
|
||||
|
||||
import math
|
||||
from decimal import Decimal, getcontext
|
||||
|
||||
|
@ -10,6 +10,7 @@ What is the largest prime factor of the number 600851475143?
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Prime_number#Unique_factorization
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
|
||||
|
@ -15,6 +15,7 @@ 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.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
|
||||
|
@ -11,6 +11,7 @@ What is the 10001st prime number?
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Prime_number
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
|
||||
|
@ -11,6 +11,7 @@ What is the 10001st prime number?
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Prime_number
|
||||
"""
|
||||
|
||||
import itertools
|
||||
import math
|
||||
|
||||
|
@ -30,6 +30,7 @@ product are 9 × 9 × 8 × 9 = 5832.
|
||||
Find the thirteen adjacent digits in the 1000-digit number that have the
|
||||
greatest product. What is the value of this product?
|
||||
"""
|
||||
|
||||
from functools import reduce
|
||||
|
||||
N = (
|
||||
|
@ -30,6 +30,7 @@ product are 9 × 9 × 8 × 9 = 5832.
|
||||
Find the thirteen adjacent digits in the 1000-digit number that have the
|
||||
greatest product. What is the value of this product?
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
N = (
|
||||
|
@ -10,6 +10,7 @@ Find the sum of all the primes below two million.
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Prime_number
|
||||
"""
|
||||
|
||||
import math
|
||||
from collections.abc import Iterator
|
||||
from itertools import takewhile
|
||||
|
@ -5,6 +5,7 @@ Problem Statement:
|
||||
Work out the first ten digits of the sum of the following one-hundred 50-digit
|
||||
numbers.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
|
@ -25,6 +25,7 @@ that all starting numbers finish at 1.
|
||||
|
||||
Which starting number, under one million, produces the longest chain?
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
COLLATZ_SEQUENCE_LENGTHS = {1: 1}
|
||||
|
@ -5,6 +5,7 @@ Starting in the top left corner of a 2×2 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 20×20 grid?
|
||||
"""
|
||||
|
||||
from math import factorial
|
||||
|
||||
|
||||
|
@ -27,6 +27,7 @@ Find the maximum total from top to bottom of the triangle below:
|
||||
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
||||
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
|
@ -8,6 +8,7 @@ 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!
|
||||
"""
|
||||
|
||||
from math import factorial
|
||||
|
||||
|
||||
|
@ -8,6 +8,7 @@ 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!
|
||||
"""
|
||||
|
||||
from math import factorial
|
||||
|
||||
|
||||
|
@ -13,6 +13,7 @@ and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and
|
||||
|
||||
Evaluate the sum of all the amicable numbers under 10000.
|
||||
"""
|
||||
|
||||
from math import sqrt
|
||||
|
||||
|
||||
|
@ -14,6 +14,7 @@ obtain a score of 938 × 53 = 49714.
|
||||
|
||||
What is the total of all the name scores in the file?
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
|
@ -14,6 +14,7 @@ obtain a score of 938 × 53 = 49714.
|
||||
|
||||
What is the total of all the name scores in the file?
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
|
@ -9,6 +9,7 @@ lexicographic permutations of 0, 1 and 2 are:
|
||||
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5,
|
||||
6, 7, 8 and 9?
|
||||
"""
|
||||
|
||||
from itertools import permutations
|
||||
|
||||
|
||||
|
@ -23,6 +23,7 @@ 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?
|
||||
"""
|
||||
|
||||
from collections.abc import Generator
|
||||
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
""" Problem Statement (Digit Fifth Powers): https://projecteuler.net/problem=30
|
||||
"""Problem Statement (Digit Fifth Powers): https://projecteuler.net/problem=30
|
||||
|
||||
Surprisingly there are only three numbers that can be written as the sum of fourth
|
||||
powers of their digits:
|
||||
@ -21,7 +21,6 @@ So, number > 999
|
||||
and hence a number between 1000 and 1000000
|
||||
"""
|
||||
|
||||
|
||||
DIGITS_FIFTH_POWER = {str(digit): digit**5 for digit in range(10)}
|
||||
|
||||
|
||||
|
@ -12,6 +12,7 @@ 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.
|
||||
"""
|
||||
|
||||
import itertools
|
||||
|
||||
|
||||
|
@ -14,6 +14,7 @@ and denominator.
|
||||
If the product of these four fractions is given in its lowest common
|
||||
terms, find the value of the denominator.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fractions import Fraction
|
||||
|
@ -15,6 +15,7 @@ below 1 million using the Sieve of Eratosthenes. Then, out of all these primes,
|
||||
we will rule out the numbers which contain an even digit. After this we will
|
||||
generate each circular combination of the number and check if all are prime.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
sieve = [True] * 1000001
|
||||
|
@ -14,6 +14,7 @@ base 10 and base 2.
|
||||
(Please note that the palindromic number, in either base, may not include
|
||||
leading zeros.)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
|
@ -37,6 +37,7 @@ a has 3 digits, etc...
|
||||
=> 100 <= a < 334, candidate = a * 10^6 + 2a * 10^3 + 3a
|
||||
= 1002003 * a
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
|
@ -10,6 +10,7 @@ All pandigital numbers except for 1, 4 ,7 pandigital numbers are divisible by 3.
|
||||
So we will check only 7 digit pandigital numbers to obtain the largest possible
|
||||
pandigital prime.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
@ -13,6 +13,7 @@ Using words.txt (right click and 'Save Link/Target As...'), a 16K text file
|
||||
containing nearly two-thousand common English words, how many are triangle
|
||||
words?
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
# Precomputes a list of the 100 first triangular numbers
|
||||
|
@ -18,7 +18,6 @@ d8d9d10=289 is divisible by 17
|
||||
Find the sum of all 0 to 9 pandigital numbers with this property.
|
||||
"""
|
||||
|
||||
|
||||
from itertools import permutations
|
||||
|
||||
|
||||
|
@ -15,6 +15,7 @@ contains 21 terms, and is equal to 953.
|
||||
Which prime, below one-million, can be written as the sum of the most
|
||||
consecutive primes?
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
|
@ -15,6 +15,7 @@ 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.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import Counter
|
||||
|
@ -16,6 +16,7 @@ It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.
|
||||
How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater
|
||||
than one-million?
|
||||
"""
|
||||
|
||||
from math import factorial
|
||||
|
||||
|
||||
|
@ -40,6 +40,7 @@ Similar problem on codewars:
|
||||
https://www.codewars.com/kata/ranking-poker-hands
|
||||
https://www.codewars.com/kata/sortable-poker-hands
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
@ -33,6 +33,7 @@ So we check individually each one of these before incrementing our
|
||||
count of current primes.
|
||||
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
|
||||
|
@ -25,6 +25,7 @@ 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.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import string
|
||||
|
@ -11,6 +11,7 @@ Find the maximum total from top to bottom in triangle.txt (right click and
|
||||
'Save Link/Target As...'), a 15K text file containing a triangle with
|
||||
one-hundred rows.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
|
@ -11,6 +11,7 @@ Find the maximum total from top to bottom in triangle.txt (right click and
|
||||
'Save Link/Target As...'), a 15K text file containing a triangle with
|
||||
one-hundred rows.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
|
@ -28,6 +28,7 @@ References:
|
||||
Finding totients
|
||||
https://en.wikipedia.org/wiki/Euler's_totient_function#Euler's_product_formula
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
|
@ -27,7 +27,6 @@ How many chains, with a starting number below one million, contain exactly sixty
|
||||
non-repeating terms?
|
||||
"""
|
||||
|
||||
|
||||
DIGIT_FACTORIALS = {
|
||||
"0": 1,
|
||||
"1": 1,
|
||||
|
@ -33,6 +33,7 @@ The generation of the chain stops before a repeating item or if the size of the
|
||||
is greater then the desired one.
|
||||
After generating each chain, the length is checked and the counter increases.
|
||||
"""
|
||||
|
||||
from math import factorial
|
||||
|
||||
DIGIT_FACTORIAL: dict[str, int] = {str(digit): factorial(digit) for digit in range(10)}
|
||||
|
@ -12,6 +12,7 @@ It is possible to write ten as the sum of primes in exactly five different ways:
|
||||
What is the first value which can be written as the sum of primes in over
|
||||
five thousand different ways?
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
|
@ -13,6 +13,7 @@ The text file, keylog.txt, contains fifty successful login attempts.
|
||||
Given that the three characters are always asked for in order, analyse the file
|
||||
so as to determine the shortest possible secret passcode of unknown length.
|
||||
"""
|
||||
|
||||
import itertools
|
||||
from pathlib import Path
|
||||
|
||||
|
@ -6,6 +6,7 @@ the digital sums of the first one hundred decimal digits for all the irrational
|
||||
square roots.
|
||||
Time: 5 October 2020, 18:30
|
||||
"""
|
||||
|
||||
import decimal
|
||||
|
||||
|
||||
|
@ -13,6 +13,7 @@ Find the minimal path sum from the top left to the bottom right by only moving r
|
||||
and down in matrix.txt (https://projecteuler.net/project/resources/p081_matrix.txt),
|
||||
a 31K text file containing an 80 by 80 matrix.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
|
@ -44,6 +44,7 @@ Solution:
|
||||
Reference: https://en.wikipedia.org/wiki/Triangular_number
|
||||
https://en.wikipedia.org/wiki/Quadratic_formula
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from math import ceil, floor, sqrt
|
||||
|
@ -66,7 +66,6 @@ Solution:
|
||||
|
||||
"""
|
||||
|
||||
|
||||
from math import sqrt
|
||||
|
||||
|
||||
|
@ -11,7 +11,6 @@ when each coordinate lies between 0 and 2 inclusive; that is,
|
||||
Given that 0 ≤ x1, y1, x2, y2 ≤ 50, how many right triangles can be formed?
|
||||
"""
|
||||
|
||||
|
||||
from itertools import combinations, product
|
||||
|
||||
|
||||
|
@ -41,6 +41,7 @@ Consider the following tenth degree polynomial generating function:
|
||||
|
||||
Find the sum of FITs for the BOPs.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
|
@ -18,6 +18,7 @@ the number of triangles for which the interior contains the origin.
|
||||
NOTE: The first two examples in the file represent the triangles in the
|
||||
example given above.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
@ -27,6 +27,7 @@ Solution:
|
||||
We use Prim's algorithm to find a Minimum Spanning Tree.
|
||||
Reference: https://en.wikipedia.org/wiki/Prim%27s_algorithm
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
@ -37,6 +37,7 @@ So it could be simplified as,
|
||||
r = 2pn when n is odd
|
||||
r = 2 when n is even.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
|
@ -29,7 +29,6 @@ The animation on the right shows the first 10 reflections of the beam.
|
||||
How many times does the beam hit the internal surface of the white cell before exiting?
|
||||
"""
|
||||
|
||||
|
||||
from math import isclose, sqrt
|
||||
|
||||
|
||||
|
@ -13,6 +13,7 @@ There are 120 reversible numbers below one-thousand.
|
||||
|
||||
How many reversible numbers are there below one-billion (10^9)?
|
||||
"""
|
||||
|
||||
EVEN_DIGITS = [0, 2, 4, 6, 8]
|
||||
ODD_DIGITS = [1, 3, 5, 7, 9]
|
||||
|
||||
|
@ -11,7 +11,6 @@ possible to form forty-one different square laminae.
|
||||
Using up to one million tiles how many different square laminae can be formed?
|
||||
"""
|
||||
|
||||
|
||||
from math import ceil, sqrt
|
||||
|
||||
|
||||
|
@ -44,6 +44,7 @@ we get the right numerator and denominator.
|
||||
Reference:
|
||||
https://en.wikipedia.org/wiki/Fermat%27s_Last_Theorem
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fractions import Fraction
|
||||
|
@ -25,7 +25,6 @@ References:
|
||||
https://projecteuler.net/problem=191
|
||||
"""
|
||||
|
||||
|
||||
cache: dict[tuple[int, int, int], int] = {}
|
||||
|
||||
|
||||
|
@ -27,6 +27,7 @@ Pascal's triangle.
|
||||
References:
|
||||
- https://en.wikipedia.org/wiki/Pascal%27s_triangle
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
|
@ -12,7 +12,6 @@ You are given a(10^6) = 31054319.
|
||||
Find a(10^15)
|
||||
"""
|
||||
|
||||
|
||||
ks = range(2, 20 + 1)
|
||||
base = [10**k for k in range(ks[-1] + 1)]
|
||||
memo: dict[int, dict[int, list[list[int]]]] = {}
|
||||
|
Reference in New Issue
Block a user