mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Enable ruff RUF002 rule (#11377)
* Enable ruff RUF002 rule * Fix --------- Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
79dc7c97ac
commit
4700297b3e
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Given a partially filled 9×9 2D array, the objective is to fill a 9×9
|
Given a partially filled 9x9 2D array, the objective is to fill a 9x9
|
||||||
square grid with digits numbered 1 to 9, so that every row, column, and
|
square grid with digits numbered 1 to 9, so that every row, column, and
|
||||||
and each of the nine 3×3 sub-grids contains all of the digits.
|
and each of the nine 3x3 sub-grids contains all of the digits.
|
||||||
|
|
||||||
This can be solved using Backtracking and is similar to n-queens.
|
This can be solved using Backtracking and is similar to n-queens.
|
||||||
We check to see if a cell is safe or not and recursively call the
|
We check to see if a cell is safe or not and recursively call the
|
||||||
|
|
|
@ -8,8 +8,8 @@ def set_bit(number: int, position: int) -> int:
|
||||||
Set the bit at position to 1.
|
Set the bit at position to 1.
|
||||||
|
|
||||||
Details: perform bitwise or for given number and X.
|
Details: perform bitwise or for given number and X.
|
||||||
Where X is a number with all the bits – zeroes and bit on given
|
Where X is a number with all the bits - zeroes and bit on given
|
||||||
position – one.
|
position - one.
|
||||||
|
|
||||||
>>> set_bit(0b1101, 1) # 0b1111
|
>>> set_bit(0b1101, 1) # 0b1111
|
||||||
15
|
15
|
||||||
|
@ -26,8 +26,8 @@ def clear_bit(number: int, position: int) -> int:
|
||||||
Set the bit at position to 0.
|
Set the bit at position to 0.
|
||||||
|
|
||||||
Details: perform bitwise and for given number and X.
|
Details: perform bitwise and for given number and X.
|
||||||
Where X is a number with all the bits – ones and bit on given
|
Where X is a number with all the bits - ones and bit on given
|
||||||
position – zero.
|
position - zero.
|
||||||
|
|
||||||
>>> clear_bit(0b10010, 1) # 0b10000
|
>>> clear_bit(0b10010, 1) # 0b10000
|
||||||
16
|
16
|
||||||
|
@ -42,8 +42,8 @@ def flip_bit(number: int, position: int) -> int:
|
||||||
Flip the bit at position.
|
Flip the bit at position.
|
||||||
|
|
||||||
Details: perform bitwise xor for given number and X.
|
Details: perform bitwise xor for given number and X.
|
||||||
Where X is a number with all the bits – zeroes and bit on given
|
Where X is a number with all the bits - zeroes and bit on given
|
||||||
position – one.
|
position - one.
|
||||||
|
|
||||||
>>> flip_bit(0b101, 1) # 0b111
|
>>> flip_bit(0b101, 1) # 0b111
|
||||||
7
|
7
|
||||||
|
@ -79,7 +79,7 @@ def get_bit(number: int, position: int) -> int:
|
||||||
Get the bit at the given position
|
Get the bit at the given position
|
||||||
|
|
||||||
Details: perform bitwise and for the given number and X,
|
Details: perform bitwise and for the given number and X,
|
||||||
Where X is a number with all the bits – zeroes and bit on given position – one.
|
Where X is a number with all the bits - zeroes and bit on given position - one.
|
||||||
If the result is not equal to 0, then the bit on the given position is 1, else 0.
|
If the result is not equal to 0, then the bit on the given position is 1, else 0.
|
||||||
|
|
||||||
>>> get_bit(0b1010, 0)
|
>>> get_bit(0b1010, 0)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform
|
https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform
|
||||||
|
|
||||||
The Burrows–Wheeler transform (BWT, also called block-sorting compression)
|
The Burrows-Wheeler transform (BWT, also called block-sorting compression)
|
||||||
rearranges a character string into runs of similar characters. This is useful
|
rearranges a character string into runs of similar characters. This is useful
|
||||||
for compression, since it tends to be easy to compress a string that has runs
|
for compression, since it tends to be easy to compress a string that has runs
|
||||||
of repeated characters by techniques such as move-to-front transform and
|
of repeated characters by techniques such as move-to-front transform and
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
One of the several implementations of Lempel–Ziv–Welch compression algorithm
|
One of the several implementations of Lempel-Ziv-Welch compression algorithm
|
||||||
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
|
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ def add_key_to_lexicon(
|
||||||
|
|
||||||
def compress_data(data_bits: str) -> str:
|
def compress_data(data_bits: str) -> str:
|
||||||
"""
|
"""
|
||||||
Compresses given data_bits using Lempel–Ziv–Welch compression algorithm
|
Compresses given data_bits using Lempel-Ziv-Welch compression algorithm
|
||||||
and returns the result as a string
|
and returns the result as a string
|
||||||
"""
|
"""
|
||||||
lexicon = {"0": "0", "1": "1"}
|
lexicon = {"0": "0", "1": "1"}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
One of the several implementations of Lempel–Ziv–Welch decompression algorithm
|
One of the several implementations of Lempel-Ziv-Welch decompression algorithm
|
||||||
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
|
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ def read_file_binary(file_path: str) -> str:
|
||||||
|
|
||||||
def decompress_data(data_bits: str) -> str:
|
def decompress_data(data_bits: str) -> str:
|
||||||
"""
|
"""
|
||||||
Decompresses given data_bits using Lempel–Ziv–Welch compression algorithm
|
Decompresses given data_bits using Lempel-Ziv-Welch compression algorithm
|
||||||
and returns the result as a string
|
and returns the result as a string
|
||||||
"""
|
"""
|
||||||
lexicon = {"0": "0", "1": "1"}
|
lexicon = {"0": "0", "1": "1"}
|
||||||
|
|
|
@ -17,7 +17,7 @@ class RedBlackTree:
|
||||||
and slower for reading in the average case, though, because they're
|
and slower for reading in the average case, though, because they're
|
||||||
both balanced binary search trees, both will get the same asymptotic
|
both balanced binary search trees, both will get the same asymptotic
|
||||||
performance.
|
performance.
|
||||||
To read more about them, https://en.wikipedia.org/wiki/Red–black_tree
|
To read more about them, https://en.wikipedia.org/wiki/Red-black_tree
|
||||||
Unless otherwise specified, all asymptotic runtimes are specified in
|
Unless otherwise specified, all asymptotic runtimes are specified in
|
||||||
terms of the size of the tree.
|
terms of the size of the tree.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -74,9 +74,9 @@ def detect_high_low_threshold(
|
||||||
image_shape, destination, threshold_low, threshold_high, weak, strong
|
image_shape, destination, threshold_low, threshold_high, weak, strong
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
High-Low threshold detection. If an edge pixel’s gradient value is higher
|
High-Low threshold detection. If an edge pixel's gradient value is higher
|
||||||
than the high threshold value, it is marked as a strong edge pixel. If an
|
than the high threshold value, it is marked as a strong edge pixel. If an
|
||||||
edge pixel’s gradient value is smaller than the high threshold value and
|
edge pixel's gradient value is smaller than the high threshold value and
|
||||||
larger than the low threshold value, it is marked as a weak edge pixel. If
|
larger than the low threshold value, it is marked as a weak edge pixel. If
|
||||||
an edge pixel's value is smaller than the low threshold value, it will be
|
an edge pixel's value is smaller than the low threshold value, it will be
|
||||||
suppressed.
|
suppressed.
|
||||||
|
|
|
@ -182,7 +182,7 @@ class IndexCalculation:
|
||||||
Atmospherically Resistant Vegetation Index 2
|
Atmospherically Resistant Vegetation Index 2
|
||||||
https://www.indexdatabase.de/db/i-single.php?id=396
|
https://www.indexdatabase.de/db/i-single.php?id=396
|
||||||
:return: index
|
:return: index
|
||||||
−0.18+1.17*(self.nir−self.red)/(self.nir+self.red)
|
-0.18+1.17*(self.nir-self.red)/(self.nir+self.red)
|
||||||
"""
|
"""
|
||||||
return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red)))
|
return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red)))
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ Approach:
|
||||||
The basic idea is to go over recursively to find the way such that the sum
|
The basic idea is to go over recursively to find the way such that the sum
|
||||||
of chosen elements is “tar”. For every element, we have two choices
|
of chosen elements is “tar”. For every element, we have two choices
|
||||||
1. Include the element in our set of chosen elements.
|
1. Include the element in our set of chosen elements.
|
||||||
2. Don’t include the element in our set of chosen elements.
|
2. Don't include the element in our set of chosen elements.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,8 @@ def couloumbs_law(
|
||||||
|
|
||||||
Reference
|
Reference
|
||||||
----------
|
----------
|
||||||
Coulomb (1785) "Premier mémoire sur l’électricité et le magnétisme,"
|
Coulomb (1785) "Premier mémoire sur l'électricité et le magnétisme,"
|
||||||
Histoire de l’Académie Royale des Sciences, pp. 569–577.
|
Histoire de l'Académie Royale des Sciences, pp. 569-577.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""
|
"""
|
||||||
The Fletcher checksum is an algorithm for computing a position-dependent
|
The Fletcher checksum is an algorithm for computing a position-dependent
|
||||||
checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs
|
checksum devised by John G. Fletcher (1934-2012) at Lawrence Livermore Labs
|
||||||
in the late 1970s.[1] The objective of the Fletcher checksum was to
|
in the late 1970s.[1] The objective of the Fletcher checksum was to
|
||||||
provide error-detection properties approaching those of a cyclic
|
provide error-detection properties approaching those of a cyclic
|
||||||
redundancy check but with the lower computational effort associated
|
redundancy check but with the lower computational effort associated
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Lower–upper (LU) decomposition factors a matrix as a product of a lower
|
Lower-upper (LU) decomposition factors a matrix as a product of a lower
|
||||||
triangular matrix and an upper triangular matrix. A square matrix has an LU
|
triangular matrix and an upper triangular matrix. A square matrix has an LU
|
||||||
decomposition under the following conditions:
|
decomposition under the following conditions:
|
||||||
- If the matrix is invertible, then it has an LU decomposition if and only
|
- If the matrix is invertible, then it has an LU decomposition if and only
|
||||||
|
|
|
@ -18,7 +18,7 @@ def schur_complement(
|
||||||
the pseudo_inv argument.
|
the pseudo_inv argument.
|
||||||
|
|
||||||
Link to Wiki: https://en.wikipedia.org/wiki/Schur_complement
|
Link to Wiki: https://en.wikipedia.org/wiki/Schur_complement
|
||||||
See also Convex Optimization – Boyd and Vandenberghe, A.5.5
|
See also Convex Optimization - Boyd and Vandenberghe, A.5.5
|
||||||
>>> import numpy as np
|
>>> import numpy as np
|
||||||
>>> a = np.array([[1, 2], [2, 1]])
|
>>> a = np.array([[1, 2], [2, 1]])
|
||||||
>>> b = np.array([[0, 3], [3, 0]])
|
>>> b = np.array([[0, 3], [3, 0]])
|
||||||
|
|
|
@ -11,7 +11,7 @@ for polynomial regression:
|
||||||
|
|
||||||
β = (XᵀX)⁻¹Xᵀy = X⁺y
|
β = (XᵀX)⁻¹Xᵀy = X⁺y
|
||||||
|
|
||||||
where X is the design matrix, y is the response vector, and X⁺ denotes the Moore–Penrose
|
where X is the design matrix, y is the response vector, and X⁺ denotes the Moore-Penrose
|
||||||
pseudoinverse of X. In the case of polynomial regression, the design matrix is
|
pseudoinverse of X. In the case of polynomial regression, the design matrix is
|
||||||
|
|
||||||
|1 x₁ x₁² ⋯ x₁ᵐ|
|
|1 x₁ x₁² ⋯ x₁ᵐ|
|
||||||
|
@ -106,7 +106,7 @@ class PolynomialRegression:
|
||||||
|
|
||||||
β = (XᵀX)⁻¹Xᵀy = X⁺y
|
β = (XᵀX)⁻¹Xᵀy = X⁺y
|
||||||
|
|
||||||
where X⁺ denotes the Moore–Penrose pseudoinverse of the design matrix X. This
|
where X⁺ denotes the Moore-Penrose pseudoinverse of the design matrix X. This
|
||||||
function computes X⁺ using singular value decomposition (SVD).
|
function computes X⁺ using singular value decomposition (SVD).
|
||||||
|
|
||||||
References:
|
References:
|
||||||
|
|
|
@ -5,7 +5,7 @@ from math import ceil, factorial
|
||||||
def pi(precision: int) -> str:
|
def pi(precision: int) -> str:
|
||||||
"""
|
"""
|
||||||
The Chudnovsky algorithm is a fast method for calculating the digits of PI,
|
The Chudnovsky algorithm is a fast method for calculating the digits of PI,
|
||||||
based on Ramanujan’s PI formulae.
|
based on Ramanujan's PI formulae.
|
||||||
|
|
||||||
https://en.wikipedia.org/wiki/Chudnovsky_algorithm
|
https://en.wikipedia.org/wiki/Chudnovsky_algorithm
|
||||||
|
|
||||||
|
|
|
@ -21,10 +21,10 @@ def calculate_prob(text: str) -> None:
|
||||||
:return: Prints
|
:return: Prints
|
||||||
1) Entropy of information based on 1 alphabet
|
1) Entropy of information based on 1 alphabet
|
||||||
2) Entropy of information based on couples of 2 alphabet
|
2) Entropy of information based on couples of 2 alphabet
|
||||||
3) print Entropy of H(X n∣Xn−1)
|
3) print Entropy of H(X n|Xn-1)
|
||||||
|
|
||||||
Text from random books. Also, random quotes.
|
Text from random books. Also, random quotes.
|
||||||
>>> text = ("Behind Winston’s back the voice "
|
>>> text = ("Behind Winston's back the voice "
|
||||||
... "from the telescreen was still "
|
... "from the telescreen was still "
|
||||||
... "babbling and the overfulfilment")
|
... "babbling and the overfulfilment")
|
||||||
>>> calculate_prob(text)
|
>>> calculate_prob(text)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
"""
|
"""
|
||||||
In mathematics, the Lucas–Lehmer test (LLT) is a primality test for Mersenne
|
In mathematics, the Lucas-Lehmer test (LLT) is a primality test for Mersenne
|
||||||
numbers. https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test
|
numbers. https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test
|
||||||
|
|
||||||
A Mersenne number is a number that is one less than a power of two.
|
A Mersenne number is a number that is one less than a power of two.
|
||||||
That is M_p = 2^p - 1
|
That is M_p = 2^p - 1
|
||||||
https://en.wikipedia.org/wiki/Mersenne_prime
|
https://en.wikipedia.org/wiki/Mersenne_prime
|
||||||
|
|
||||||
The Lucas–Lehmer test is the primality test used by the
|
The Lucas-Lehmer test is the primality test used by the
|
||||||
Great Internet Mersenne Prime Search (GIMPS) to locate large primes.
|
Great Internet Mersenne Prime Search (GIMPS) to locate large primes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ def modular_division(a: int, b: int, n: int) -> int:
|
||||||
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
|
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
|
||||||
|
|
||||||
Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should
|
Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should
|
||||||
return an integer x such that 0≤x≤n−1, and b/a=x(modn) (that is, b=ax(modn)).
|
return an integer x such that 0≤x≤n-1, and b/a=x(modn) (that is, b=ax(modn)).
|
||||||
|
|
||||||
Theorem:
|
Theorem:
|
||||||
a has a multiplicative inverse modulo n iff gcd(a,n) = 1
|
a has a multiplicative inverse modulo n iff gcd(a,n) = 1
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Given a function on floating number f(x) and two floating numbers ‘a’ and ‘b’ such that
|
Given a function on floating number f(x) and two floating numbers `a` and `b` such that
|
||||||
f(a) * f(b) < 0 and f(x) is continuous in [a, b].
|
f(a) * f(b) < 0 and f(x) is continuous in [a, b].
|
||||||
Here f(x) represents algebraic or transcendental equation.
|
Here f(x) represents algebraic or transcendental equation.
|
||||||
Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0)
|
Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Python program to show how to interpolate and evaluate a polynomial
|
Python program to show how to interpolate and evaluate a polynomial
|
||||||
using Neville's method.
|
using Neville's method.
|
||||||
Neville’s method evaluates a polynomial that passes through a
|
Neville's method evaluates a polynomial that passes through a
|
||||||
given set of x and y points for a particular x value (x0) using the
|
given set of x and y points for a particular x value (x0) using the
|
||||||
Newton polynomial form.
|
Newton polynomial form.
|
||||||
Reference:
|
Reference:
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
https://en.wikipedia.org/wiki/Augmented_matrix
|
https://en.wikipedia.org/wiki/Augmented_matrix
|
||||||
|
|
||||||
This algorithm solves simultaneous linear equations of the form
|
This algorithm solves simultaneous linear equations of the form
|
||||||
λa + λb + λc + λd + ... = γ as [λ, λ, λ, λ, ..., γ]
|
λa + λb + λc + λd + ... = y as [λ, λ, λ, λ, ..., y]
|
||||||
Where λ & γ are individual coefficients, the no. of equations = no. of coefficients - 1
|
Where λ & y are individual coefficients, the no. of equations = no. of coefficients - 1
|
||||||
|
|
||||||
Note in order to work there must exist 1 equation where all instances of λ and γ != 0
|
Note in order to work there must exist 1 equation where all instances of λ and y != 0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ Explanation: There is no 1 in the matrix.
|
||||||
|
|
||||||
Approach:
|
Approach:
|
||||||
We initialize another matrix (dp) with the same dimensions
|
We initialize another matrix (dp) with the same dimensions
|
||||||
as the original one initialized with all 0’s.
|
as the original one initialized with all 0's.
|
||||||
|
|
||||||
dp_array(i,j) represents the side length of the maximum square whose
|
dp_array(i,j) represents the side length of the maximum square whose
|
||||||
bottom right corner is the cell with index (i,j) in the original matrix.
|
bottom right corner is the cell with index (i,j) in the original matrix.
|
||||||
|
@ -39,7 +39,7 @@ bottom right corner is the cell with index (i,j) in the original matrix.
|
||||||
Starting from index (0,0), for every 1 found in the original matrix,
|
Starting from index (0,0), for every 1 found in the original matrix,
|
||||||
we update the value of the current element as
|
we update the value of the current element as
|
||||||
|
|
||||||
dp_array(i,j)=dp_array(dp(i−1,j),dp_array(i−1,j−1),dp_array(i,j−1)) + 1.
|
dp_array(i,j)=dp_array(dp(i-1,j),dp_array(i-1,j-1),dp_array(i,j-1)) + 1.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ def spiral_traversal(matrix: list[list]) -> list[int]:
|
||||||
Algorithm:
|
Algorithm:
|
||||||
Step 1. first pop the 0 index list. (which is [1,2,3,4] and concatenate the
|
Step 1. first pop the 0 index list. (which is [1,2,3,4] and concatenate the
|
||||||
output of [step 2])
|
output of [step 2])
|
||||||
Step 2. Now perform matrix’s Transpose operation (Change rows to column
|
Step 2. Now perform matrix's Transpose operation (Change rows to column
|
||||||
and vice versa) and reverse the resultant matrix.
|
and vice versa) and reverse the resultant matrix.
|
||||||
Step 3. Pass the output of [2nd step], to same recursive function till
|
Step 3. Pass the output of [2nd step], to same recursive function till
|
||||||
base case hits.
|
base case hits.
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
A Framework of Back Propagation Neural Network(BP) model
|
A Framework of Back Propagation Neural Network (BP) model
|
||||||
|
|
||||||
Easy to use:
|
Easy to use:
|
||||||
* add many layers as you want !!!
|
* add many layers as you want ! ! !
|
||||||
* clearly see how the loss decreasing
|
* clearly see how the loss decreasing
|
||||||
Easy to expand:
|
Easy to expand:
|
||||||
* more activation functions
|
* more activation functions
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Davis–Putnam–Logemann–Loveland (DPLL) algorithm is a complete, backtracking-based
|
Davis-Putnam-Logemann-Loveland (DPLL) algorithm is a complete, backtracking-based
|
||||||
search algorithm for deciding the satisfiability of propositional logic formulae in
|
search algorithm for deciding the satisfiability of propositional logic formulae in
|
||||||
conjunctive normal form, i.e, for solving the Conjunctive Normal Form SATisfiability
|
conjunctive normal form, i.e, for solving the Conjunctive Normal Form SATisfiability
|
||||||
(CNF-SAT) problem.
|
(CNF-SAT) problem.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
"""
|
"""
|
||||||
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a
|
The Fisher-Yates shuffle is an algorithm for generating a random permutation of a
|
||||||
finite sequence.
|
finite sequence.
|
||||||
For more details visit
|
For more details visit
|
||||||
wikipedia/Fischer-Yates-Shuffle.
|
wikipedia/Fischer-Yates-Shuffle.
|
||||||
|
|
|
@ -3,7 +3,7 @@ Calculate the buoyant force of any body completely or partially submerged in a s
|
||||||
fluid. This principle was discovered by the Greek mathematician Archimedes.
|
fluid. This principle was discovered by the Greek mathematician Archimedes.
|
||||||
|
|
||||||
Equation for calculating buoyant force:
|
Equation for calculating buoyant force:
|
||||||
Fb = ρ * V * g
|
Fb = p * V * g
|
||||||
|
|
||||||
https://en.wikipedia.org/wiki/Archimedes%27_principle
|
https://en.wikipedia.org/wiki/Archimedes%27_principle
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -16,8 +16,8 @@ assumed to be concentrated to visualize its motion. In other words, the center o
|
||||||
is the particle equivalent of a given object for the application of Newton's laws of
|
is the particle equivalent of a given object for the application of Newton's laws of
|
||||||
motion.
|
motion.
|
||||||
|
|
||||||
In the case of a system of particles P_i, i = 1, ..., n , each with mass m_i that are
|
In the case of a system of particles P_i, i = 1, ..., n , each with mass m_i that are
|
||||||
located in space with coordinates r_i, i = 1, ..., n , the coordinates R of the center
|
located in space with coordinates r_i, i = 1, ..., n , the coordinates R of the center
|
||||||
of mass corresponds to:
|
of mass corresponds to:
|
||||||
|
|
||||||
R = (Σ(mi * ri) / Σ(mi))
|
R = (Σ(mi * ri) / Σ(mi))
|
||||||
|
@ -36,8 +36,8 @@ def center_of_mass(particles: list[Particle]) -> Coord3D:
|
||||||
Input Parameters
|
Input Parameters
|
||||||
----------------
|
----------------
|
||||||
particles: list(Particle):
|
particles: list(Particle):
|
||||||
A list of particles where each particle is a tuple with it´s (x, y, z) position and
|
A list of particles where each particle is a tuple with it's (x, y, z) position and
|
||||||
it´s mass.
|
it's mass.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
|
|
|
@ -6,7 +6,7 @@ or centre of curvature.
|
||||||
The unit of centripetal force is newton.
|
The unit of centripetal force is newton.
|
||||||
|
|
||||||
The centripetal force is always directed perpendicular to the
|
The centripetal force is always directed perpendicular to the
|
||||||
direction of the object’s displacement. Using Newton’s second
|
direction of the object's displacement. Using Newton's second
|
||||||
law of motion, it is found that the centripetal force of an object
|
law of motion, it is found that the centripetal force of an object
|
||||||
moving in a circular path always acts towards the centre of the circle.
|
moving in a circular path always acts towards the centre of the circle.
|
||||||
The Centripetal Force Formula is given as the product of mass (in kg)
|
The Centripetal Force Formula is given as the product of mass (in kg)
|
||||||
|
|
|
@ -12,13 +12,13 @@ two inertial reference frames and X' moves in the x direction with velocity v
|
||||||
with respect to X, then the Lorentz transformation from X to X' is X' = BX,
|
with respect to X, then the Lorentz transformation from X to X' is X' = BX,
|
||||||
where
|
where
|
||||||
|
|
||||||
| γ -γβ 0 0|
|
| y -γβ 0 0|
|
||||||
B = |-γβ γ 0 0|
|
B = |-γβ y 0 0|
|
||||||
| 0 0 1 0|
|
| 0 0 1 0|
|
||||||
| 0 0 0 1|
|
| 0 0 0 1|
|
||||||
|
|
||||||
is the matrix describing the Lorentz boost between X and X',
|
is the matrix describing the Lorentz boost between X and X',
|
||||||
γ = 1 / √(1 - v²/c²) is the Lorentz factor, and β = v/c is the velocity as
|
y = 1 / √(1 - v²/c²) is the Lorentz factor, and β = v/c is the velocity as
|
||||||
a fraction of c.
|
a fraction of c.
|
||||||
|
|
||||||
Reference: https://en.wikipedia.org/wiki/Lorentz_transformation
|
Reference: https://en.wikipedia.org/wiki/Lorentz_transformation
|
||||||
|
@ -63,7 +63,7 @@ def beta(velocity: float) -> float:
|
||||||
|
|
||||||
def gamma(velocity: float) -> float:
|
def gamma(velocity: float) -> float:
|
||||||
"""
|
"""
|
||||||
Calculate the Lorentz factor γ = 1 / √(1 - v²/c²) for a given velocity
|
Calculate the Lorentz factor y = 1 / √(1 - v²/c²) for a given velocity
|
||||||
>>> gamma(4)
|
>>> gamma(4)
|
||||||
1.0000000000000002
|
1.0000000000000002
|
||||||
>>> gamma(1e5)
|
>>> gamma(1e5)
|
||||||
|
@ -90,12 +90,12 @@ def transformation_matrix(velocity: float) -> np.ndarray:
|
||||||
"""
|
"""
|
||||||
Calculate the Lorentz transformation matrix for movement in the x direction:
|
Calculate the Lorentz transformation matrix for movement in the x direction:
|
||||||
|
|
||||||
| γ -γβ 0 0|
|
| y -γβ 0 0|
|
||||||
|-γβ γ 0 0|
|
|-γβ y 0 0|
|
||||||
| 0 0 1 0|
|
| 0 0 1 0|
|
||||||
| 0 0 0 1|
|
| 0 0 0 1|
|
||||||
|
|
||||||
where γ is the Lorentz factor and β is the velocity as a fraction of c
|
where y is the Lorentz factor and β is the velocity as a fraction of c
|
||||||
>>> transformation_matrix(29979245)
|
>>> transformation_matrix(29979245)
|
||||||
array([[ 1.00503781, -0.10050378, 0. , 0. ],
|
array([[ 1.00503781, -0.10050378, 0. , 0. ],
|
||||||
[-0.10050378, 1.00503781, 0. , 0. ],
|
[-0.10050378, 1.00503781, 0. , 0. ],
|
||||||
|
|
|
@ -8,10 +8,10 @@ pipe. Reynolds number is defined by the ratio of inertial forces to that of
|
||||||
viscous forces.
|
viscous forces.
|
||||||
|
|
||||||
R = Inertial Forces / Viscous Forces
|
R = Inertial Forces / Viscous Forces
|
||||||
R = (ρ * V * D)/μ
|
R = (p * V * D)/μ
|
||||||
|
|
||||||
where :
|
where :
|
||||||
ρ = Density of fluid (in Kg/m^3)
|
p = Density of fluid (in Kg/m^3)
|
||||||
D = Diameter of pipe through which fluid flows (in m)
|
D = Diameter of pipe through which fluid flows (in m)
|
||||||
V = Velocity of flow of the fluid (in m/s)
|
V = Velocity of flow of the fluid (in m/s)
|
||||||
μ = Viscosity of the fluid (in Ns/m^2)
|
μ = Viscosity of the fluid (in Ns/m^2)
|
||||||
|
|
|
@ -8,13 +8,13 @@ and buoyancy is equal to the downward gravity force acting on the
|
||||||
object. The acceleration of the object is zero as the net force acting on
|
object. The acceleration of the object is zero as the net force acting on
|
||||||
the object is zero.
|
the object is zero.
|
||||||
|
|
||||||
Vt = ((2 * m * g)/(ρ * A * Cd))^0.5
|
Vt = ((2 * m * g)/(p * A * Cd))^0.5
|
||||||
|
|
||||||
where :
|
where :
|
||||||
Vt = Terminal velocity (in m/s)
|
Vt = Terminal velocity (in m/s)
|
||||||
m = Mass of the falling object (in Kg)
|
m = Mass of the falling object (in Kg)
|
||||||
g = Acceleration due to gravity (value taken : imported from scipy)
|
g = Acceleration due to gravity (value taken : imported from scipy)
|
||||||
ρ = Density of the fluid through which the object is falling (in Kg/m^3)
|
p = Density of the fluid through which the object is falling (in Kg/m^3)
|
||||||
A = Projected area of the object (in m^2)
|
A = Projected area of the object (in m^2)
|
||||||
Cd = Drag coefficient (dimensionless)
|
Cd = Drag coefficient (dimensionless)
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ Project Euler Problem 4: https://projecteuler.net/problem=4
|
||||||
Largest palindrome product
|
Largest palindrome product
|
||||||
|
|
||||||
A palindromic number reads the same both ways. The largest palindrome made
|
A palindromic number reads the same both ways. The largest palindrome made
|
||||||
from the product of two 2-digit numbers is 9009 = 91 × 99.
|
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.
|
Find the largest palindrome made from the product of two 3-digit numbers.
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ Project Euler Problem 4: https://projecteuler.net/problem=4
|
||||||
Largest palindrome product
|
Largest palindrome product
|
||||||
|
|
||||||
A palindromic number reads the same both ways. The largest palindrome made
|
A palindromic number reads the same both ways. The largest palindrome made
|
||||||
from the product of two 2-digit numbers is 9009 = 91 × 99.
|
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.
|
Find the largest palindrome made from the product of two 3-digit numbers.
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ Project Euler Problem 8: https://projecteuler.net/problem=8
|
||||||
Largest product in a series
|
Largest product in a series
|
||||||
|
|
||||||
The four adjacent digits in the 1000-digit number that have the greatest
|
The four adjacent digits in the 1000-digit number that have the greatest
|
||||||
product are 9 × 9 × 8 × 9 = 5832.
|
product are 9 x 9 x 8 x 9 = 5832.
|
||||||
|
|
||||||
73167176531330624919225119674426574742355349194934
|
73167176531330624919225119674426574742355349194934
|
||||||
96983520312774506326239578318016984801869478851843
|
96983520312774506326239578318016984801869478851843
|
||||||
|
|
|
@ -4,7 +4,7 @@ Project Euler Problem 8: https://projecteuler.net/problem=8
|
||||||
Largest product in a series
|
Largest product in a series
|
||||||
|
|
||||||
The four adjacent digits in the 1000-digit number that have the greatest
|
The four adjacent digits in the 1000-digit number that have the greatest
|
||||||
product are 9 × 9 × 8 × 9 = 5832.
|
product are 9 x 9 x 8 x 9 = 5832.
|
||||||
|
|
||||||
73167176531330624919225119674426574742355349194934
|
73167176531330624919225119674426574742355349194934
|
||||||
96983520312774506326239578318016984801869478851843
|
96983520312774506326239578318016984801869478851843
|
||||||
|
|
|
@ -4,7 +4,7 @@ Project Euler Problem 8: https://projecteuler.net/problem=8
|
||||||
Largest product in a series
|
Largest product in a series
|
||||||
|
|
||||||
The four adjacent digits in the 1000-digit number that have the greatest
|
The four adjacent digits in the 1000-digit number that have the greatest
|
||||||
product are 9 × 9 × 8 × 9 = 5832.
|
product are 9 x 9 x 8 x 9 = 5832.
|
||||||
|
|
||||||
73167176531330624919225119674426574742355349194934
|
73167176531330624919225119674426574742355349194934
|
||||||
96983520312774506326239578318016984801869478851843
|
96983520312774506326239578318016984801869478851843
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
"""
|
"""
|
||||||
Problem 15: https://projecteuler.net/problem=15
|
Problem 15: https://projecteuler.net/problem=15
|
||||||
|
|
||||||
Starting in the top left corner of a 2×2 grid, and only being able to move to
|
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.
|
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?
|
How many such routes are there through a 20x20 grid?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from math import factorial
|
from math import factorial
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
"""
|
"""
|
||||||
Problem 20: https://projecteuler.net/problem=20
|
Problem 20: https://projecteuler.net/problem=20
|
||||||
|
|
||||||
n! means n × (n − 1) × ... × 3 × 2 × 1
|
n! means n x (n - 1) x ... x 3 x 2 x 1
|
||||||
|
|
||||||
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
|
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.
|
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!
|
Find the sum of the digits in the number 100!
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
"""
|
"""
|
||||||
Problem 20: https://projecteuler.net/problem=20
|
Problem 20: https://projecteuler.net/problem=20
|
||||||
|
|
||||||
n! means n × (n − 1) × ... × 3 × 2 × 1
|
n! means n x (n - 1) x ... x 3 x 2 x 1
|
||||||
|
|
||||||
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
|
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.
|
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!
|
Find the sum of the digits in the number 100!
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
"""
|
"""
|
||||||
Problem 20: https://projecteuler.net/problem=20
|
Problem 20: https://projecteuler.net/problem=20
|
||||||
|
|
||||||
n! means n × (n − 1) × ... × 3 × 2 × 1
|
n! means n x (n - 1) x ... x 3 x 2 x 1
|
||||||
|
|
||||||
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
|
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.
|
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!
|
Find the sum of the digits in the number 100!
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
"""
|
"""
|
||||||
Problem 20: https://projecteuler.net/problem=20
|
Problem 20: https://projecteuler.net/problem=20
|
||||||
|
|
||||||
n! means n × (n − 1) × ... × 3 × 2 × 1
|
n! means n x (n - 1) x ... x 3 x 2 x 1
|
||||||
|
|
||||||
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
|
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.
|
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!
|
Find the sum of the digits in the number 100!
|
||||||
|
|
|
@ -10,7 +10,7 @@ score.
|
||||||
|
|
||||||
For example, when the list is sorted into alphabetical order, COLIN, which is
|
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
|
worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would
|
||||||
obtain a score of 938 × 53 = 49714.
|
obtain a score of 938 x 53 = 49714.
|
||||||
|
|
||||||
What is the total of all the name scores in the file?
|
What is the total of all the name scores in the file?
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -10,7 +10,7 @@ score.
|
||||||
|
|
||||||
For example, when the list is sorted into alphabetical order, COLIN, which is
|
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
|
worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would
|
||||||
obtain a score of 938 × 53 = 49714.
|
obtain a score of 938 x 53 = 49714.
|
||||||
|
|
||||||
What is the total of all the name scores in the file?
|
What is the total of all the name scores in the file?
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
The Fibonacci sequence is defined by the recurrence relation:
|
The Fibonacci sequence is defined by the recurrence relation:
|
||||||
|
|
||||||
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
|
Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.
|
||||||
|
|
||||||
Hence the first 12 terms will be:
|
Hence the first 12 terms will be:
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
The Fibonacci sequence is defined by the recurrence relation:
|
The Fibonacci sequence is defined by the recurrence relation:
|
||||||
|
|
||||||
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
|
Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.
|
||||||
|
|
||||||
Hence the first 12 terms will be:
|
Hence the first 12 terms will be:
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
The Fibonacci sequence is defined by the recurrence relation:
|
The Fibonacci sequence is defined by the recurrence relation:
|
||||||
|
|
||||||
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
|
Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.
|
||||||
|
|
||||||
Hence the first 12 terms will be:
|
Hence the first 12 terms will be:
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,12 @@ n2 + n + 41
|
||||||
It turns out that the formula will produce 40 primes for the consecutive values
|
It turns out that the formula will produce 40 primes for the consecutive values
|
||||||
n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible
|
n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible
|
||||||
by 41, and certainly when n = 41, 412 + 41 + 41 is clearly divisible by 41.
|
by 41, and certainly when n = 41, 412 + 41 + 41 is clearly divisible by 41.
|
||||||
The incredible formula n2 − 79n + 1601 was discovered, which produces 80 primes
|
The incredible formula n2 - 79n + 1601 was discovered, which produces 80 primes
|
||||||
for the consecutive values n = 0 to 79. The product of the coefficients, −79 and
|
for the consecutive values n = 0 to 79. The product of the coefficients, -79 and
|
||||||
1601, is −126479.
|
1601, is -126479.
|
||||||
Considering quadratics of the form:
|
Considering quadratics of the form:
|
||||||
n² + an + b, where |a| < 1000 and |b| < 1000
|
n² + an + b, where |a| < 1000 and |b| < 1000
|
||||||
where |n| is the modulus/absolute value of ne.g. |11| = 11 and |−4| = 4
|
where |n| is the modulus/absolute value of ne.g. |11| = 11 and |-4| = 4
|
||||||
Find the product of the coefficients, a and b, for the quadratic expression that
|
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
|
produces the maximum number of primes for consecutive values of n, starting with
|
||||||
n = 0.
|
n = 0.
|
||||||
|
|
|
@ -2,14 +2,14 @@
|
||||||
Coin sums
|
Coin sums
|
||||||
Problem 31: https://projecteuler.net/problem=31
|
Problem 31: https://projecteuler.net/problem=31
|
||||||
|
|
||||||
In England the currency is made up of pound, £, and pence, p, and there are
|
In England the currency is made up of pound, f, and pence, p, and there are
|
||||||
eight coins in general circulation:
|
eight coins in general circulation:
|
||||||
|
|
||||||
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
|
1p, 2p, 5p, 10p, 20p, 50p, f1 (100p) and f2 (200p).
|
||||||
It is possible to make £2 in the following way:
|
It is possible to make f2 in the following way:
|
||||||
|
|
||||||
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
|
1xf1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p
|
||||||
How many different ways can £2 be made using any number of coins?
|
How many different ways can f2 be made using any number of coins?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,17 +3,17 @@ Problem 31: https://projecteuler.net/problem=31
|
||||||
|
|
||||||
Coin sums
|
Coin sums
|
||||||
|
|
||||||
In England the currency is made up of pound, £, and pence, p, and there are
|
In England the currency is made up of pound, f, and pence, p, and there are
|
||||||
eight coins in general circulation:
|
eight coins in general circulation:
|
||||||
|
|
||||||
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
|
1p, 2p, 5p, 10p, 20p, 50p, f1 (100p) and f2 (200p).
|
||||||
It is possible to make £2 in the following way:
|
It is possible to make f2 in the following way:
|
||||||
|
|
||||||
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
|
1xf1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p
|
||||||
How many different ways can £2 be made using any number of coins?
|
How many different ways can f2 be made using any number of coins?
|
||||||
|
|
||||||
Hint:
|
Hint:
|
||||||
> There are 100 pence in a pound (£1 = 100p)
|
> There are 100 pence in a pound (f1 = 100p)
|
||||||
> There are coins(in pence) are available: 1, 2, 5, 10, 20, 50, 100 and 200.
|
> There are coins(in pence) are available: 1, 2, 5, 10, 20, 50, 100 and 200.
|
||||||
> how many different ways you can combine these values to create 200 pence.
|
> how many different ways you can combine these values to create 200 pence.
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ 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
|
digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through
|
||||||
5 pandigital.
|
5 pandigital.
|
||||||
|
|
||||||
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing
|
The product 7254 is unusual, as the identity, 39 x 186 = 7254, containing
|
||||||
multiplicand, multiplier, and product is 1 through 9 pandigital.
|
multiplicand, multiplier, and product is 1 through 9 pandigital.
|
||||||
|
|
||||||
Find the sum of all products whose multiplicand/multiplier/product identity can
|
Find the sum of all products whose multiplicand/multiplier/product identity can
|
||||||
|
|
|
@ -3,9 +3,9 @@ Project Euler Problem 38: https://projecteuler.net/problem=38
|
||||||
|
|
||||||
Take the number 192 and multiply it by each of 1, 2, and 3:
|
Take the number 192 and multiply it by each of 1, 2, and 3:
|
||||||
|
|
||||||
192 × 1 = 192
|
192 x 1 = 192
|
||||||
192 × 2 = 384
|
192 x 2 = 384
|
||||||
192 × 3 = 576
|
192 x 3 = 576
|
||||||
|
|
||||||
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call
|
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)
|
192384576 the concatenated product of 192 and (1,2,3)
|
||||||
|
|
|
@ -11,7 +11,7 @@ It can be seen that the 12th digit of the fractional part is 1.
|
||||||
If dn represents the nth digit of the fractional part, find the value of the
|
If dn represents the nth digit of the fractional part, find the value of the
|
||||||
following expression.
|
following expression.
|
||||||
|
|
||||||
d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000
|
d1 x d10 x d100 x d1000 x d10000 x d100000 x d1000000
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
"""
|
"""
|
||||||
Problem 44: https://projecteuler.net/problem=44
|
Problem 44: https://projecteuler.net/problem=44
|
||||||
|
|
||||||
Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten
|
Pentagonal numbers are generated by the formula, Pn=n(3n-1)/2. The first ten
|
||||||
pentagonal numbers are:
|
pentagonal numbers are:
|
||||||
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
|
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
|
||||||
It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference,
|
It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference,
|
||||||
70 − 22 = 48, is not pentagonal.
|
70 - 22 = 48, is not pentagonal.
|
||||||
|
|
||||||
Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference
|
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?
|
are pentagonal and D = |Pk - Pj| is minimised; what is the value of D?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ Problem 45: https://projecteuler.net/problem=45
|
||||||
|
|
||||||
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
|
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
|
||||||
Triangle T(n) = (n * (n + 1)) / 2 1, 3, 6, 10, 15, ...
|
Triangle T(n) = (n * (n + 1)) / 2 1, 3, 6, 10, 15, ...
|
||||||
Pentagonal P(n) = (n * (3 * n − 1)) / 2 1, 5, 12, 22, 35, ...
|
Pentagonal P(n) = (n * (3 * n - 1)) / 2 1, 5, 12, 22, 35, ...
|
||||||
Hexagonal H(n) = n * (2 * n − 1) 1, 6, 15, 28, 45, ...
|
Hexagonal H(n) = n * (2 * n - 1) 1, 6, 15, 28, 45, ...
|
||||||
It can be verified that T(285) = P(165) = H(143) = 40755.
|
It can be verified that T(285) = P(165) = H(143) = 40755.
|
||||||
|
|
||||||
Find the next triangle number that is also pentagonal and hexagonal.
|
Find the next triangle number that is also pentagonal and hexagonal.
|
||||||
|
|
|
@ -4,12 +4,12 @@ Problem 46: https://projecteuler.net/problem=46
|
||||||
It was proposed by Christian Goldbach that every odd composite number can be
|
It was proposed by Christian Goldbach that every odd composite number can be
|
||||||
written as the sum of a prime and twice a square.
|
written as the sum of a prime and twice a square.
|
||||||
|
|
||||||
9 = 7 + 2 × 12
|
9 = 7 + 2 x 12
|
||||||
15 = 7 + 2 × 22
|
15 = 7 + 2 x 22
|
||||||
21 = 3 + 2 × 32
|
21 = 3 + 2 x 32
|
||||||
25 = 7 + 2 × 32
|
25 = 7 + 2 x 32
|
||||||
27 = 19 + 2 × 22
|
27 = 19 + 2 x 22
|
||||||
33 = 31 + 2 × 12
|
33 = 31 + 2 x 12
|
||||||
|
|
||||||
It turns out that the conjecture was false.
|
It turns out that the conjecture was false.
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,14 @@ Problem 47
|
||||||
|
|
||||||
The first two consecutive numbers to have two distinct prime factors are:
|
The first two consecutive numbers to have two distinct prime factors are:
|
||||||
|
|
||||||
14 = 2 × 7
|
14 = 2 x 7
|
||||||
15 = 3 × 5
|
15 = 3 x 5
|
||||||
|
|
||||||
The first three consecutive numbers to have three distinct prime factors are:
|
The first three consecutive numbers to have three distinct prime factors are:
|
||||||
|
|
||||||
644 = 2² × 7 × 23
|
644 = 2² x 7 x 23
|
||||||
645 = 3 × 5 × 43
|
645 = 3 x 5 x 43
|
||||||
646 = 2 × 17 × 19.
|
646 = 2 x 17 x 19.
|
||||||
|
|
||||||
Find the first four consecutive integers to have four distinct prime factors each.
|
Find the first four consecutive integers to have four distinct prime factors each.
|
||||||
What is the first of these numbers?
|
What is the first of these numbers?
|
||||||
|
|
|
@ -10,7 +10,7 @@ In combinatorics, we use the notation, 5C3 = 10.
|
||||||
|
|
||||||
In general,
|
In general,
|
||||||
|
|
||||||
nCr = n!/(r!(n−r)!),where r ≤ n, n! = n×(n−1)×...×3×2×1, and 0! = 1.
|
nCr = n!/(r!(n-r)!),where r ≤ n, n! = nx(n-1)x...x3x2x1, and 0! = 1.
|
||||||
It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.
|
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
|
How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
The first known prime found to exceed one million digits was discovered in 1999,
|
The first known prime found to exceed one million digits was discovered in 1999,
|
||||||
and is a Mersenne prime of the form 2**6972593 − 1; it contains exactly 2,098,960
|
and is a Mersenne prime of the form 2**6972593 - 1; it contains exactly 2,098,960
|
||||||
digits. Subsequently other Mersenne primes, of the form 2**p − 1, have been found
|
digits. Subsequently other Mersenne primes, of the form 2**p - 1, have been found
|
||||||
which contain more digits.
|
which contain more digits.
|
||||||
However, in 2004 there was found a massive non-Mersenne prime which contains
|
However, in 2004 there was found a massive non-Mersenne prime which contains
|
||||||
2,357,207 digits: (28433 * (2 ** 7830457 + 1)).
|
2,357,207 digits: (28433 * (2 ** 7830457 + 1)).
|
||||||
|
|
|
@ -3,7 +3,7 @@ Project Euler Problem 104 : https://projecteuler.net/problem=104
|
||||||
|
|
||||||
The Fibonacci sequence is defined by the recurrence relation:
|
The Fibonacci sequence is defined by the recurrence relation:
|
||||||
|
|
||||||
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
|
Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.
|
||||||
It turns out that F541, which contains 113 digits, is the first Fibonacci number
|
It turns out that F541, which contains 113 digits, is the first Fibonacci number
|
||||||
for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9,
|
for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9,
|
||||||
but not necessarily in order). And F2749, which contains 575 digits, is the first
|
but not necessarily in order). And F2749, which contains 575 digits, is the first
|
||||||
|
|
|
@ -3,7 +3,7 @@ Problem 120 Square remainders: https://projecteuler.net/problem=120
|
||||||
|
|
||||||
Description:
|
Description:
|
||||||
|
|
||||||
Let r be the remainder when (a−1)^n + (a+1)^n is divided by a^2.
|
Let r be the remainder when (a-1)^n + (a+1)^n is divided by a^2.
|
||||||
For example, if a = 7 and n = 3, then r = 42: 6^3 + 8^3 = 728 ≡ 42 mod 49.
|
For example, if a = 7 and n = 3, then r = 42: 6^3 + 8^3 = 728 ≡ 42 mod 49.
|
||||||
And as n varies, so too will r, but for a = 7 it turns out that r_max = 42.
|
And as n varies, so too will r, but for a = 7 it turns out that r_max = 42.
|
||||||
For 3 ≤ a ≤ 1000, find ∑ r_max.
|
For 3 ≤ a ≤ 1000, find ∑ r_max.
|
||||||
|
|
|
@ -4,7 +4,7 @@ Problem 123: https://projecteuler.net/problem=123
|
||||||
Name: Prime square remainders
|
Name: Prime square remainders
|
||||||
|
|
||||||
Let pn be the nth prime: 2, 3, 5, 7, 11, ..., and
|
Let pn be the nth prime: 2, 3, 5, 7, 11, ..., and
|
||||||
let r be the remainder when (pn−1)^n + (pn+1)^n is divided by pn^2.
|
let r be the remainder when (pn-1)^n + (pn+1)^n is divided by pn^2.
|
||||||
|
|
||||||
For example, when n = 3, p3 = 5, and 43 + 63 = 280 ≡ 5 mod 25.
|
For example, when n = 3, p3 = 5, and 43 + 63 = 280 ≡ 5 mod 25.
|
||||||
The least value of n for which the remainder first exceeds 10^9 is 7037.
|
The least value of n for which the remainder first exceeds 10^9 is 7037.
|
||||||
|
|
|
@ -3,9 +3,9 @@ Project Euler Problem 135: https://projecteuler.net/problem=135
|
||||||
|
|
||||||
Given the positive integers, x, y, and z, are consecutive terms of an arithmetic
|
Given the positive integers, x, y, and z, are consecutive terms of an arithmetic
|
||||||
progression, the least value of the positive integer, n, for which the equation,
|
progression, the least value of the positive integer, n, for which the equation,
|
||||||
x2 − y2 − z2 = n, has exactly two solutions is n = 27:
|
x2 - y2 - z2 = n, has exactly two solutions is n = 27:
|
||||||
|
|
||||||
342 − 272 − 202 = 122 − 92 − 62 = 27
|
342 - 272 - 202 = 122 - 92 - 62 = 27
|
||||||
|
|
||||||
It turns out that n = 1155 is the least value which has exactly ten solutions.
|
It turns out that n = 1155 is the least value which has exactly ten solutions.
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ works its way back out.
|
||||||
The specific white cell we will be considering is an ellipse with the equation
|
The specific white cell we will be considering is an ellipse with the equation
|
||||||
4x^2 + y^2 = 100
|
4x^2 + y^2 = 100
|
||||||
|
|
||||||
The section corresponding to −0.01 ≤ x ≤ +0.01 at the top is missing, allowing the
|
The section corresponding to -0.01 ≤ x ≤ +0.01 at the top is missing, allowing the
|
||||||
light to enter and exit through the hole.
|
light to enter and exit through the hole.
|
||||||

|

|
||||||
The light beam in this problem starts at the point (0.0,10.1) just outside the white
|
The light beam in this problem starts at the point (0.0,10.1) just outside the white
|
||||||
|
@ -20,7 +20,7 @@ In the figure on the left, the red line shows the first two points of contact be
|
||||||
the laser beam and the wall of the white cell; the blue line shows the line tangent to
|
the laser beam and the wall of the white cell; the blue line shows the line tangent to
|
||||||
the ellipse at the point of incidence of the first bounce.
|
the ellipse at the point of incidence of the first bounce.
|
||||||
|
|
||||||
The slope m of the tangent line at any point (x,y) of the given ellipse is: m = −4x/y
|
The slope m of the tangent line at any point (x,y) of the given ellipse is: m = -4x/y
|
||||||
|
|
||||||
The normal line is perpendicular to this tangent line at the point of incidence.
|
The normal line is perpendicular to this tangent line at the point of incidence.
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ t = 32 is type L(2).
|
||||||
Let N(n) be the number of t ≤ 1000000 such that t is type L(n); for example,
|
Let N(n) be the number of t ≤ 1000000 such that t is type L(n); for example,
|
||||||
N(15) = 832.
|
N(15) = 832.
|
||||||
|
|
||||||
What is ∑ N(n) for 1 ≤ n ≤ 10?
|
What is sum N(n) for 1 ≤ n ≤ 10?
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
|
@ -10,6 +10,7 @@ lint.ignore = [ # `ruff rule S101` for a description of that rule
|
||||||
"PLW2901", # PLW2901: Redefined loop variable -- FIX ME
|
"PLW2901", # PLW2901: Redefined loop variable -- FIX ME
|
||||||
"PT011", # `pytest.raises(Exception)` is too broad, set the `match` parameter or use a more specific exception
|
"PT011", # `pytest.raises(Exception)` is too broad, set the `match` parameter or use a more specific exception
|
||||||
"PT018", # Assertion should be broken down into multiple parts
|
"PT018", # Assertion should be broken down into multiple parts
|
||||||
|
"RUF001", # String contains ambiguous {}. Did you mean {}?
|
||||||
"RUF002", # Docstring contains ambiguous {}. Did you mean {}?
|
"RUF002", # Docstring contains ambiguous {}. Did you mean {}?
|
||||||
"RUF003", # Comment contains ambiguous {}. Did you mean {}?
|
"RUF003", # Comment contains ambiguous {}. Did you mean {}?
|
||||||
"S101", # Use of `assert` detected -- DO NOT FIX
|
"S101", # Use of `assert` detected -- DO NOT FIX
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
def jaro_winkler(str1: str, str2: str) -> float:
|
def jaro_winkler(str1: str, str2: str) -> float:
|
||||||
"""
|
"""
|
||||||
Jaro–Winkler distance is a string metric measuring an edit distance between two
|
Jaro-Winkler distance is a string metric measuring an edit distance between two
|
||||||
sequences.
|
sequences.
|
||||||
Output value is between 0.0 and 1.0.
|
Output value is between 0.0 and 1.0.
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ def palindromic_string(input_string: str) -> str:
|
||||||
>>> palindromic_string('ababa')
|
>>> palindromic_string('ababa')
|
||||||
'ababa'
|
'ababa'
|
||||||
|
|
||||||
Manacher’s algorithm which finds Longest palindromic Substring in linear time.
|
Manacher's algorithm which finds Longest palindromic Substring in linear time.
|
||||||
|
|
||||||
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
|
1. first this convert input_string("xyx") into new_string("x|y|x") where odd
|
||||||
positions are actual input characters.
|
positions are actual input characters.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
https://cp-algorithms.com/string/prefix-function.html
|
https://cp-algorithms.com/string/prefix-function.html
|
||||||
|
|
||||||
Prefix function Knuth–Morris–Pratt algorithm
|
Prefix function Knuth-Morris-Pratt algorithm
|
||||||
|
|
||||||
Different algorithm than Knuth-Morris-Pratt pattern finding
|
Different algorithm than Knuth-Morris-Pratt pattern finding
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user