Fix bugs
This commit is contained in:
parent
26b940a2ac
commit
b0b9705303
@ -21,7 +21,7 @@ def main():
|
|||||||
end = default_timer()
|
end = default_timer()
|
||||||
|
|
||||||
print('Project Euler, Problem 1')
|
print('Project Euler, Problem 1')
|
||||||
print(f'Answer: {sum}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ def main():
|
|||||||
end = default_timer()
|
end = default_timer()
|
||||||
|
|
||||||
print('Project Euler, Problem 2')
|
print('Project Euler, Problem 2')
|
||||||
print(f'Answer: {sum}')
|
print(f'Answer: {sum_}')
|
||||||
|
|
||||||
print(f'Elapsed time: {end - start:.9f} seconds')
|
print(f'Elapsed time: {end - start:.9f} seconds')
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ from math import sqrt, floor, ceil, gcd
|
|||||||
|
|
||||||
from numpy import zeros
|
from numpy import zeros
|
||||||
|
|
||||||
|
|
||||||
def is_prime(num):
|
def is_prime(num):
|
||||||
if num < 4:
|
if num < 4:
|
||||||
# If num is 2 or 3 then it's prime.
|
# If num is 2 or 3 then it's prime.
|
||||||
@ -28,6 +29,7 @@ def is_prime(num):
|
|||||||
# If no factor is found up to the square root of num, num is prime.
|
# If no factor is found up to the square root of num, num is prime.
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def is_palindrome(num, base):
|
def is_palindrome(num, base):
|
||||||
reverse = 0
|
reverse = 0
|
||||||
|
|
||||||
@ -48,10 +50,13 @@ def is_palindrome(num, base):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# Least common multiple algorithm using the greatest common divisor.
|
# Least common multiple algorithm using the greatest common divisor.
|
||||||
def lcm(a, b):
|
def lcm(a, b):
|
||||||
return a * b // gcd(a, b)
|
return a * b // gcd(a, b)
|
||||||
|
|
||||||
|
|
||||||
# Recursive function to calculate the least common multiple of more than 2 numbers.
|
# Recursive function to calculate the least common multiple of more than 2 numbers.
|
||||||
def lcmm(values, n):
|
def lcmm(values, n):
|
||||||
# If there are only two numbers, use the lcm function to calculate the lcm.
|
# If there are only two numbers, use the lcm function to calculate the lcm.
|
||||||
@ -63,6 +68,7 @@ def lcmm(values, n):
|
|||||||
# Recursively calculate lcm(a, b, c, ..., n) = lcm(a, lcm(b, c, ..., n)).
|
# Recursively calculate lcm(a, b, c, ..., n) = lcm(a, lcm(b, c, ..., n)).
|
||||||
return lcm(value, lcmm(values[1:], n-1))
|
return lcm(value, lcmm(values[1:], n-1))
|
||||||
|
|
||||||
|
|
||||||
# Function implementing the Sieve or Eratosthenes to generate
|
# Function implementing the Sieve or Eratosthenes to generate
|
||||||
# primes up to a certain number.
|
# primes up to a certain number.
|
||||||
def sieve(n):
|
def sieve(n):
|
||||||
@ -75,7 +81,7 @@ def sieve(n):
|
|||||||
# primes[3] = 1
|
# primes[3] = 1
|
||||||
|
|
||||||
# Cross out (set to 0) all even numbers and set the odd numbers to 1 (possible prime).
|
# Cross out (set to 0) all even numbers and set the odd numbers to 1 (possible prime).
|
||||||
for i in range(4, n -1, 2):
|
for i in range(4, n - 1, 2):
|
||||||
primes[i] = 0
|
primes[i] = 0
|
||||||
# primes[i+1] = 1
|
# primes[i+1] = 1
|
||||||
|
|
||||||
@ -96,6 +102,7 @@ def sieve(n):
|
|||||||
|
|
||||||
return primes
|
return primes
|
||||||
|
|
||||||
|
|
||||||
def count_divisors(n):
|
def count_divisors(n):
|
||||||
count = 0
|
count = 0
|
||||||
# For every divisor below the square root of n, there is a corresponding one
|
# For every divisor below the square root of n, there is a corresponding one
|
||||||
@ -113,6 +120,7 @@ def count_divisors(n):
|
|||||||
|
|
||||||
return count
|
return count
|
||||||
|
|
||||||
|
|
||||||
def find_max_path(triang, n):
|
def find_max_path(triang, n):
|
||||||
# Start from the second to last row and go up.
|
# Start from the second to last row and go up.
|
||||||
for i in range(n-2, -1, -1):
|
for i in range(n-2, -1, -1):
|
||||||
@ -127,6 +135,7 @@ def find_max_path(triang, n):
|
|||||||
|
|
||||||
return triang[0][0]
|
return triang[0][0]
|
||||||
|
|
||||||
|
|
||||||
def sum_of_divisors(n):
|
def sum_of_divisors(n):
|
||||||
# For each divisor of n smaller than the square root of n,
|
# For each divisor of n smaller than the square root of n,
|
||||||
# there is another one larger than the square root. If i is
|
# there is another one larger than the square root. If i is
|
||||||
@ -147,6 +156,7 @@ def sum_of_divisors(n):
|
|||||||
|
|
||||||
return sum_
|
return sum_
|
||||||
|
|
||||||
|
|
||||||
def is_pandigital(value, n):
|
def is_pandigital(value, n):
|
||||||
i = 0
|
i = 0
|
||||||
digits = [0] * (n + 1)
|
digits = [0] * (n + 1)
|
||||||
@ -172,6 +182,7 @@ def is_pandigital(value, n):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def is_pentagonal(n):
|
def is_pentagonal(n):
|
||||||
# A number n is pentagonal if p=(sqrt(24n+1)+1)/6 is an integer.
|
# A number n is pentagonal if p=(sqrt(24n+1)+1)/6 is an integer.
|
||||||
# In this case, n is the pth pentagonal number.
|
# In this case, n is the pth pentagonal number.
|
||||||
@ -179,6 +190,7 @@ def is_pentagonal(n):
|
|||||||
|
|
||||||
return i.is_integer()
|
return i.is_integer()
|
||||||
|
|
||||||
|
|
||||||
# Function implementing the iterative algorithm taken from Wikipedia
|
# Function implementing the iterative algorithm taken from Wikipedia
|
||||||
# to find the continued fraction for sqrt(S). The algorithm is as
|
# to find the continued fraction for sqrt(S). The algorithm is as
|
||||||
# follows:
|
# follows:
|
||||||
@ -205,7 +217,7 @@ def build_sqrt_cont_fraction(i, l):
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
mn1 = dn * an - mn
|
mn1 = dn * an - mn
|
||||||
dn1 = (i - mn1 * mn1)/ dn
|
dn1 = (i - mn1 * mn1) / dn
|
||||||
an1 = floor((a0+mn1)/dn1)
|
an1 = floor((a0+mn1)/dn1)
|
||||||
mn = mn1
|
mn = mn1
|
||||||
dn = dn1
|
dn = dn1
|
||||||
@ -221,12 +233,12 @@ def build_sqrt_cont_fraction(i, l):
|
|||||||
|
|
||||||
return fraction, count
|
return fraction, count
|
||||||
|
|
||||||
|
|
||||||
# Function to solve the Diophantine equation in the form x^2-Dy^2=1
|
# Function to solve the Diophantine equation in the form x^2-Dy^2=1
|
||||||
# (Pell equation) using continued fractions.
|
# (Pell equation) using continued fractions.
|
||||||
|
|
||||||
def pell_eq(d):
|
def pell_eq(d):
|
||||||
# Find the continued fraction for sqrt(d).
|
# Find the continued fraction for sqrt(d).
|
||||||
fraction, period = build_sqrt_cont_fraction(d, 100)
|
fraction, _ = build_sqrt_cont_fraction(d, 100)
|
||||||
|
|
||||||
# Calculate the first convergent of the continued fraction.
|
# Calculate the first convergent of the continued fraction.
|
||||||
n1 = 0
|
n1 = 0
|
||||||
@ -265,6 +277,7 @@ def pell_eq(d):
|
|||||||
if fraction[j] == -1:
|
if fraction[j] == -1:
|
||||||
j = 1
|
j = 1
|
||||||
|
|
||||||
|
|
||||||
# Function to check if a number is semiprime. Parameters include
|
# Function to check if a number is semiprime. Parameters include
|
||||||
# pointers to p and q to return the factors values and a list of
|
# pointers to p and q to return the factors values and a list of
|
||||||
# primes.
|
# primes.
|
||||||
@ -278,17 +291,20 @@ def is_semiprime(n, primes):
|
|||||||
if primes[n//2] == 1:
|
if primes[n//2] == 1:
|
||||||
p = 2
|
p = 2
|
||||||
q = n // 2
|
q = n // 2
|
||||||
|
|
||||||
return True, p, q
|
return True, p, q
|
||||||
else:
|
|
||||||
return False, -1, -1
|
return False, -1, -1
|
||||||
|
|
||||||
# Check if n is semiprime and one of the factors is 3.
|
# Check if n is semiprime and one of the factors is 3.
|
||||||
elif n % 3 == 0:
|
elif n % 3 == 0:
|
||||||
if primes[n//3] == 1:
|
if primes[n//3] == 1:
|
||||||
p = 3
|
p = 3
|
||||||
q = n // 3
|
q = n // 3
|
||||||
|
|
||||||
return True, p, q
|
return True, p, q
|
||||||
else:
|
|
||||||
return False, -1, -1
|
return False, -1, -1
|
||||||
|
|
||||||
# Any number can have only one prime factor greater than its
|
# Any number can have only one prime factor greater than its
|
||||||
# square root, so we can stop checking at this point.
|
# square root, so we can stop checking at this point.
|
||||||
@ -304,26 +320,31 @@ def is_semiprime(n, primes):
|
|||||||
if primes[n//i] == 1:
|
if primes[n//i] == 1:
|
||||||
p = i
|
p = i
|
||||||
q = n // i
|
q = n // i
|
||||||
|
|
||||||
return True, p, q
|
return True, p, q
|
||||||
else:
|
|
||||||
return False, -1, -1
|
return False, -1, -1
|
||||||
elif primes[i+2] == 1 and n % (i + 2) == 0:
|
|
||||||
|
if primes[i+2] == 1 and n % (i + 2) == 0:
|
||||||
if primes[n//(i+2)] == 1:
|
if primes[n//(i+2)] == 1:
|
||||||
p = i + 2
|
p = i + 2
|
||||||
q = n // (i + 2)
|
q = n // (i + 2)
|
||||||
|
|
||||||
return True, p, q
|
return True, p, q
|
||||||
else:
|
|
||||||
return False, -1, -1
|
return False, -1, -1
|
||||||
|
|
||||||
return False, -1, -1
|
return False, -1, -1
|
||||||
|
|
||||||
|
|
||||||
# If n=pq is semiprime, phi(n)=(p-1)(q-1)=pq-p-q+1=n-(p+4)+1
|
# If n=pq is semiprime, phi(n)=(p-1)(q-1)=pq-p-q+1=n-(p+4)+1
|
||||||
# if p!=q. If p=q (n is a square), phi(n)=n-p.
|
# if p!=q. If p=q (n is a square), phi(n)=n-p.
|
||||||
def phi_semiprime(n, p, q):
|
def phi_semiprime(n, p, q):
|
||||||
if p == q:
|
if p == q:
|
||||||
return n - p
|
return n - p
|
||||||
else:
|
|
||||||
return n - (p + q) + 1
|
return n - (p + q) + 1
|
||||||
|
|
||||||
|
|
||||||
def phi(n, primes):
|
def phi(n, primes):
|
||||||
# If n is primes, phi(n)=n-1.
|
# If n is primes, phi(n)=n-1.
|
||||||
@ -398,6 +419,7 @@ def phi(n, primes):
|
|||||||
|
|
||||||
return ph
|
return ph
|
||||||
|
|
||||||
|
|
||||||
# Function implementing the partition function.
|
# Function implementing the partition function.
|
||||||
def partition_fn(n, partitions, mod=-1):
|
def partition_fn(n, partitions, mod=-1):
|
||||||
# The partition function for negative numbers is 0 by definition.
|
# The partition function for negative numbers is 0 by definition.
|
||||||
@ -427,12 +449,12 @@ def partition_fn(n, partitions, mod=-1):
|
|||||||
partitions[n] = res % mod
|
partitions[n] = res % mod
|
||||||
|
|
||||||
return res % mod
|
return res % mod
|
||||||
else:
|
|
||||||
partitions[n] = int(res)
|
|
||||||
|
|
||||||
return int(res)
|
partitions[n] = int(res)
|
||||||
|
|
||||||
|
return int(res)
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
def dijkstra(matrix, distances, m, n, up=False, back=False, start=0):
|
def dijkstra(matrix, distances, m, n, up=False, back=False, start=0):
|
||||||
visited = zeros((m, n), int)
|
visited = zeros((m, n), int)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user