From ef8cfc2d4aff680f7830d05a8f60839a5cc5a909 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Sun, 8 Nov 2020 01:57:52 +0100 Subject: [PATCH] Finally finished 26, think I did it good enough --- 26/main.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 27/main.py | 7 +++++ 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 27/main.py diff --git a/26/main.py b/26/main.py index e933628..3e83244 100644 --- a/26/main.py +++ b/26/main.py @@ -1,8 +1,84 @@ # Find the number d < 1000 for which 1 / d has the longest recurring cycle # in its decimal fraction part -# Observation: I first thought we only need to check prime numbers, but -# that is not the case. For example, 1/21 has a cycle of length 7, longer -# than both 3 and 7, but exactly the same as the cycles of 3 and 7 combined. -# But for 1/49 it has a cycle of length 42, so I don't think there is a -# clever way to deduce the length of the cycles without calculating them \ No newline at end of file +# It seems like the cycle is never longer than the longest of the cycle length of any of its prime divisors +# So say we want to know the length for d and d = a * b, then the cycle for d has the same length as either the cycle for a or b, whichever is longer. + +# So it seems we then only have to check the primes, so first let's get a prime finder in here + +import numpy as np +import math + +def maxIndex(ns): + result = 0 + m = ns[0] + + for i in range(len(ns)): + if ns[i] > m: + result = i + m = ns[i] + + return result + +def getPrimes(n): + primes = [1 for p in range(n)] + primes[0] = 0 + primes[1] = 0 + + x = 2 + while x < np.sqrt(n): + if primes[x] == 1: + + for i in range(x+x, n, x): + primes[i] = 0 + + x += 1 + + result = [] + for i in range(n): + if primes[i]: + result.append(i) + + return result + +# Now we need to find out the recurring cycle length for any number +# As a benefit for only checking prime number, we know that the cycles will always immediately start after the .'s + +def cycleLength(n): + l = int(math.log10(n)) + 1 + y = 1 + power = pow(10,l) + + result = 1 + + while True: + + x = (y * power) // n + rest = y * power - x * n + + if rest == 0: + return 0 + + elif rest == 1: + break + + else: + y = rest + + result += 1 + + return result * l + +def main(): + print("Hello, this is Patrick") + + ps = getPrimes(1000) + cyclelengths = [cycleLength(p) for p in ps] + + for i in range(len(ps)): + print(f"{ps[i]} has length: {cyclelengths[i]}") + + print(f"Biggest cycle is for {ps[maxIndex(cyclelengths)]} with length {max(cyclelengths)}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/27/main.py b/27/main.py new file mode 100644 index 0000000..3e95a24 --- /dev/null +++ b/27/main.py @@ -0,0 +1,7 @@ +# Quadratic primes + +# Consider quadratics of the form: +# n^2 + an + b, where abs(a) < 1000 and abs(b) <= 1000 + +# Find the product of the coefficients, a * b, for the quadratic expression that +# produces the maximum number of primes for consecutive values of n, starting from n = 0 \ No newline at end of file