Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.0 kB
9
Indexable
Never
import numpy as np

class Didnt_Findit(Exception):
    pass
def lucky(n):
    # initial seed
    lucky_numbers = [1]
    # how many numbers do you need to get to n?
    candidates = np.arange(1, n*100, 2, dtype='object')
    # use numpy array boolean indexing
    next_lucky = candidates[candidates > lucky_numbers[-1]][0]
    # accumulate lucky numbers till you have n of them
    while next_lucky < candidates[-1]:
        with open('lnums.txt', 'a') as the_file:
          the_file.write(str(next_lucky)+'\n')
        lucky_numbers.append(next_lucky)
        #print lucky_numbers
        if len(lucky_numbers) == n:
            return lucky_numbers
        mask_start = next_lucky - 1
        mask_step = next_lucky
        mask = np.array([True] * len(candidates))
        mask[mask_start::mask_step] = False
        #print mask
        candidates = candidates[mask]
        next_lucky = candidates[ candidates > lucky_numbers[-1]][0]
    raise Didnt_Findit('n = ', n)
lucky(18446744073709551)