From df64e1015dee43eefce9b591b7bf6a62d57e8822 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Sat, 6 Mar 2021 01:32:47 +0100 Subject: [PATCH] Quite a slow solution for 47, but it works --- 47/main.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 47/main.py diff --git a/47/main.py b/47/main.py new file mode 100644 index 0000000..9bb1800 --- /dev/null +++ b/47/main.py @@ -0,0 +1,68 @@ +''' +The first two consecutive numbers to have two distinct prime factors are: + +14 = 2 × 7 +15 = 3 × 5 + +The first three consecutive numbers to have three distinct prime factors are: + +644 = 2² × 7 × 23 +645 = 3 × 5 × 43 +646 = 2 × 17 × 19. + +Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers? +''' + +import math +import numpy as np + +def sieve(n): + assert n > 1 + + ns = [True] * n + + for i in range(2, math.ceil(np.sqrt(n))): + if ns[i]: + j = pow(i, 2) + + while j < n: + ns[j] = False + j = j + i + + return [i for i,val in enumerate(ns) if val][2:] + +def primeFactors(n, primes): + res = [] + + for p in primes: + if n % p == 0: + res.append(p) + while n % p == 0: + n /= p + + return res + +def main(): + print("Hello this is Patrick") + + end = 1000000 + primeList = sieve(end) + primeSet = set(primeList) + consecs = 0 + + for n in range(100000, end): + if n not in primeSet: + if len(primeFactors(n, primeList)) == 4: + consecs += 1 + else: + consecs = 0 + else: + consecs = 0 + + if consecs == 4: + for i in range(n-3, n+1): + print(i, primeFactors(i, primeList)) + break + +if __name__ == "__main__": + main() \ No newline at end of file