Quite a slow solution for 47, but it works
This commit is contained in:
68
47/main.py
Normal file
68
47/main.py
Normal file
@@ -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()
|
||||||
Reference in New Issue
Block a user