From b0b9705303a03f6718779aad4bf976356a9a7b3e Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Tue, 6 Jun 2023 22:37:04 +0200 Subject: [PATCH] Fix bugs --- Python/p001.py | 2 +- Python/p002.py | 2 +- Python/projecteuler.py | 68 ++++++++++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/Python/p001.py b/Python/p001.py index 4630705..c195a2b 100644 --- a/Python/p001.py +++ b/Python/p001.py @@ -21,7 +21,7 @@ def main(): end = default_timer() print('Project Euler, Problem 1') - print(f'Answer: {sum}') + print(f'Answer: {sum_}') print(f'Elapsed time: {end - start:.9f} seconds') diff --git a/Python/p002.py b/Python/p002.py index 9d2f4c9..63ed494 100644 --- a/Python/p002.py +++ b/Python/p002.py @@ -33,7 +33,7 @@ def main(): end = default_timer() print('Project Euler, Problem 2') - print(f'Answer: {sum}') + print(f'Answer: {sum_}') print(f'Elapsed time: {end - start:.9f} seconds') diff --git a/Python/projecteuler.py b/Python/projecteuler.py index 1feb974..032a091 100644 --- a/Python/projecteuler.py +++ b/Python/projecteuler.py @@ -4,6 +4,7 @@ from math import sqrt, floor, ceil, gcd from numpy import zeros + def is_prime(num): if num < 4: # 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. return True + def is_palindrome(num, base): reverse = 0 @@ -48,10 +50,13 @@ def is_palindrome(num, base): return True return False + + # Least common multiple algorithm using the greatest common divisor. def lcm(a, b): return a * b // gcd(a, b) + # Recursive function to calculate the least common multiple of more than 2 numbers. def lcmm(values, n): # 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)). return lcm(value, lcmm(values[1:], n-1)) + # Function implementing the Sieve or Eratosthenes to generate # primes up to a certain number. def sieve(n): @@ -75,7 +81,7 @@ def sieve(n): # primes[3] = 1 # 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+1] = 1 @@ -96,6 +102,7 @@ def sieve(n): return primes + def count_divisors(n): count = 0 # For every divisor below the square root of n, there is a corresponding one @@ -113,6 +120,7 @@ def count_divisors(n): return count + def find_max_path(triang, n): # Start from the second to last row and go up. for i in range(n-2, -1, -1): @@ -127,6 +135,7 @@ def find_max_path(triang, n): return triang[0][0] + def sum_of_divisors(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 @@ -147,6 +156,7 @@ def sum_of_divisors(n): return sum_ + def is_pandigital(value, n): i = 0 digits = [0] * (n + 1) @@ -172,6 +182,7 @@ def is_pandigital(value, n): return True + def is_pentagonal(n): # A number n is pentagonal if p=(sqrt(24n+1)+1)/6 is an integer. # In this case, n is the pth pentagonal number. @@ -179,6 +190,7 @@ def is_pentagonal(n): return i.is_integer() + # Function implementing the iterative algorithm taken from Wikipedia # to find the continued fraction for sqrt(S). The algorithm is as # follows: @@ -205,7 +217,7 @@ def build_sqrt_cont_fraction(i, l): while True: mn1 = dn * an - mn - dn1 = (i - mn1 * mn1)/ dn + dn1 = (i - mn1 * mn1) / dn an1 = floor((a0+mn1)/dn1) mn = mn1 dn = dn1 @@ -221,12 +233,12 @@ def build_sqrt_cont_fraction(i, l): return fraction, count + # Function to solve the Diophantine equation in the form x^2-Dy^2=1 # (Pell equation) using continued fractions. - def pell_eq(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. n1 = 0 @@ -265,6 +277,7 @@ def pell_eq(d): if fraction[j] == -1: j = 1 + # Function to check if a number is semiprime. Parameters include # pointers to p and q to return the factors values and a list of # primes. @@ -278,25 +291,28 @@ def is_semiprime(n, primes): if primes[n//2] == 1: p = 2 q = n // 2 + 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. elif n % 3 == 0: if primes[n//3] == 1: p = 3 q = n // 3 + return True, p, q - else: - return False, -1, -1 + + return False, -1, -1 # Any number can have only one prime factor greater than its # square root, so we can stop checking at this point. limit = floor(sqrt(n)) + 1 # Every prime other than 2 and 3 is in the form 6k+1 or 6k-1. -# If I check all those value no prime factors of the number -# will be missed. For each of these possible primes, check if +# If I check all those value no prime factors of the number +# will be missed. For each of these possible primes, check if # they are prime, then if the number is semiprime with using # that factor. for i in range(5, limit, 6): @@ -304,26 +320,31 @@ def is_semiprime(n, primes): if primes[n//i] == 1: p = i q = n // i + return True, p, q - else: - return False, -1, -1 - elif primes[i+2] == 1 and n % (i + 2) == 0: + + return False, -1, -1 + + if primes[i+2] == 1 and n % (i + 2) == 0: if primes[n//(i+2)] == 1: p = i + 2 q = n // (i + 2) + return True, p, q - else: - 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 p!=q. If p=q (n is a square), phi(n)=n-p. def phi_semiprime(n, p, q): if p == q: return n - p - else: - return n - (p + q) + 1 + + return n - (p + q) + 1 + def phi(n, primes): # If n is primes, phi(n)=n-1. @@ -366,8 +387,8 @@ def phi(n, primes): limit = floor(sqrt(n)) + 1 # Every prime other than 2 and 3 is in the form 6k+1 or 6k-1. -# If I check all those value no prime factors of the number -# will be missed. For each of these possible primes, check if +# If I check all those value no prime factors of the number +# will be missed. For each of these possible primes, check if # they are prime, then check if the number divides n, in which # case update the current ph. for i in range(5, limit, 6): @@ -398,6 +419,7 @@ def phi(n, primes): return ph + # Function implementing the partition function. def partition_fn(n, partitions, mod=-1): # 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 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): visited = zeros((m, n), int)