From 89b75091efcc8ab7ec62564a9dbe319cb0552eba Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 10 Jan 2022 10:58:14 +0000 Subject: Solutions for challenge #147 --- challenge-147/roger-bell-west/python/ch-1.py | 53 ++++++++++++++++++++++++++++ challenge-147/roger-bell-west/python/ch-2.py | 41 +++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100755 challenge-147/roger-bell-west/python/ch-1.py create mode 100755 challenge-147/roger-bell-west/python/ch-2.py (limited to 'challenge-147/roger-bell-west/python') diff --git a/challenge-147/roger-bell-west/python/ch-1.py b/challenge-147/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..18f827e01c --- /dev/null +++ b/challenge-147/roger-bell-west/python/ch-1.py @@ -0,0 +1,53 @@ +#! /usr/bin/python3 + +import unittest + +from math import sqrt,floor +from collections import deque + +def genprimes(mx): + primesh=set(range(2,4)) + for i in range(6,mx+2,6): + for j in range(i-1,i+2,2): + if j <= mx: + primesh.add(j) + q=deque([2,3,5,7]) + p=q.popleft() + mr=floor(sqrt(mx)) + while p <= mr: + if p in primesh: + for i in range(p*p,mx+1,p): + primesh.discard(i) + if len(q) < 2: + q.append(q[-1]+4) + q.append(q[-1]+2) + p=q.popleft() + primes=list(primesh) + primes.sort() + return primes + +def ltruncprimes(count): + out=[] + lt=0 + p=[str(i) for i in genprimes(500)] + pp=set(p) + for pc in p: + l=len(pc) + c=True + for i in range(1,l): + if not pc[i:l] in pp: + c=False + break + if c: + out.append(int(pc)) + lt += 1 + if lt >= count: + break + return out + +class TestLtruncprimes(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(ltruncprimes(20),[2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197],'example 1') + +unittest.main() diff --git a/challenge-147/roger-bell-west/python/ch-2.py b/challenge-147/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..83f5c48bba --- /dev/null +++ b/challenge-147/roger-bell-west/python/ch-2.py @@ -0,0 +1,41 @@ +#! /usr/bin/python3 + +def pentagon(n): + return int(n*(3*n-1)/2) + +def pentpair(): + fpent=[0] + rpent=dict() + mx=0 + a=1 + while 1: + while mx < a: + mx += 1 + fpent.append(pentagon(mx)) + rpent[fpent[mx]]=mx + for b in range(1,a): + d=fpent[a]-fpent[b] + if d < fpent[b]: + break + if d in rpent: + s=fpent[a]+fpent[b] + while s > fpent[mx]: + mx += 1 + fpent.append(pentagon(mx)) + rpent[fpent[mx]]=mx + if s in rpent: + print("P({:d}) + P({:d}) = {:d} + {:d} = {:d} = P({:d})".format( + a, b, + fpent[a], fpent[b], + s, + rpent[s])) + print("P({:d}) - P({:d}) = {:d} - {:d} = {:d} = P({:d})".format( + a, b, + fpent[a], fpent[b], + d, + rpent[d])) + quit() + a += 1 + + +pentpair() -- cgit From 8580bd80aa9245695b20c075a18dfac2061f7579 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 10 Jan 2022 12:04:26 +0000 Subject: Fixes to ch2 (premature optimisation) --- challenge-147/roger-bell-west/python/ch-2.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'challenge-147/roger-bell-west/python') diff --git a/challenge-147/roger-bell-west/python/ch-2.py b/challenge-147/roger-bell-west/python/ch-2.py index 83f5c48bba..b524b7e5e5 100755 --- a/challenge-147/roger-bell-west/python/ch-2.py +++ b/challenge-147/roger-bell-west/python/ch-2.py @@ -15,8 +15,6 @@ def pentpair(): rpent[fpent[mx]]=mx for b in range(1,a): d=fpent[a]-fpent[b] - if d < fpent[b]: - break if d in rpent: s=fpent[a]+fpent[b] while s > fpent[mx]: -- cgit