Skip to content

Instantly share code, notes, and snippets.

@honno
Last active March 19, 2020 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honno/fb3c1a9b27fd8a3ea7577d102669d246 to your computer and use it in GitHub Desktop.
Save honno/fb3c1a9b27fd8a3ea7577d102669d246 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
from collections import defaultdict
ID_LEN = 5
def gen_primes():
"""Using the Sieve of Erastothenes solution"""
composites = defaultdict(list)
checker = 2
while True:
if checker not in composites:
yield checker
composites[checker*checker].append(checker)
else:
for prime in composites[checker]:
composites[prime+checker].append(prime)
del composites[checker]
checker += 1
def solution(i):
pointer = 0
id_ = ''
primes = gen_primes()
while True:
prime_str = str(next(primes))
threshold = pointer + len(prime_str) - i
if threshold > 0:
prime_str = prime_str[i-pointer:]
while len(id_) < ID_LEN:
for num in prime_str[:ID_LEN-len(id_)]:
id_ += num
prime_str = str(next(primes))
return id_
pointer += len(prime_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment