Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- #gen_primes is modified from http://stackoverflow.com/a/568618/203705
- def gen_primes(start, end):
- # Maps composites to primes witnessing their compositeness.
- # This is memory efficient, as the sieve is not "run forward"
- # indefinitely, but only as long as required by the current
- # number being tested.
- #
- D = {}
- # The running integer that's checked for primeness
- q = 2
- primes = []
- while q != end:
- if q not in D:
- # q is a new prime.
- # Yield it and mark its first multiple that isn't
- # already marked in previous iterations
- #
- if q >= start:
- primes.append(q)
- D[q * q] = [q]
- else:
- # q is composite. D[q] is the list of primes that
- # divide it. Since we've reached q, we no longer
- # need it in the map, but we'll mark the next
- # multiples of its witnesses to prepare for larger
- # numbers
- #
- for p in D[q]:
- D.setdefault(p + q, []).append(p)
- del D[q]
- q += 1
- return primes
- def get_danish_nums(name):
- nums = {}
- with open(name, 'r') as f:
- lines = f.readlines()
- for line in lines:
- vals = line.split(',')
- if vals[2].strip() != 'Reserve / Reserve':
- if len(vals[0].strip()) == 8:
- start = int(re.sub(r'[a-z]','0',vals[0]))
- end = int(re.sub(r'[a-z]','9',vals[0]))
- if start == end:
- nums[start] = True
- else:
- for num in range(start, end+1):
- nums[num] = True
- return nums
- if __name__ == '__main__':
- print("Getting nums...")
- #CSV built from spreadsheet found here:
- #http://www.erhvervsstyrelsen.dk/numbering_lists
- nums = get_danish_nums('danish_numberlist.csv')
- start = min(nums)
- end = max(nums)
- print("Getting primes...")
- primes = gen_primes(start,end)
- i = 0
- print("Checking...")
- for prime in primes:
- if nums.get(prime, False):
- print("%s is valid prime phone number!" % prime)
- i+=1
- print("%s of %s valid danish phone numbers are prime." % (i, len(nums)))
Advertisement
Add Comment
Please, Sign In to add comment