Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the nth lucky number generated by a sieve in Python

Tags:

python

I'm trying to make a program in Python which will generate the nth lucky number according to the lucky number sieve. I'm fairly new to Python so I don't know how to do all that much yet. So far I've figured out how to make a function which determines all lucky numbers below a specified number:

def lucky(number):
    l = range(1, number + 1, 2)
    i = 1
    while i < len(l):
        del l[l[i] - 1::l[i]]
        i += 1
    return l

Is there a way to modify this so that I can instead find the nth lucky number? I thought about increasing the specified number gradually until a list of the appropriate length to find the required lucky number was created, but that seems like a really inefficient way of doing it.

Edit: I came up with this, but is there a better way?

def lucky(number):
    f = 2
    n = number * f
    while True:
        l = range(1, n + 1, 2)
        i = 1
        while i < len(l):
            del l[l[i] - 1::l[i]]
            i += 1
        if len(l) >= number:
            return l[number - 1]
        f += 1
        n = number * f
like image 320
user3397105 Avatar asked Mar 08 '14 22:03

user3397105


People also ask

How do I find my lucky number in Python?

Begin with a list of integers starting with 1 : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, . . . . Now eliminate every second number : 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ...

How can I calculate my lucky number?

To find your life path number in numerology, which is the most significant of your lucky numbers, start by breaking down your birth month, day, and year into single digits. Then, add the single digits for each part of your birthday together. Next, add all 3 of those numbers together.

What is lucky number logic?

The sequence of natural numbers or subset of integers that we get after removing second, third, fourth, fifth, and so on number respectively from the sequence. By applying the process there still remains some numbers indefinitely in the sequence such numbers are known as lucky numbers.

Is 9 a lucky number?

The number nine is often associated with a divine connotation in the mystical thought and religions across the globe from ancient times. There is both, negativity and positivity in this mysterious nine! Not to talk of the numeral 9, which is often cited as a 'lucky number' for many these days!


1 Answers

I came up with this, but is there a better way?

Truth is, there will always be a better way, the remaining question being: is it good enough for your need?

One possible improvement would be to turn all this into a generator function. That way, you would only compute new values as they are consumed. I came up with this version, which I only validated up to about 60 terms:

import itertools


def _idx_after_removal(removed_indices, value):
    for removed in removed_indices:
        value -= value / removed
    return value


def _should_be_excluded(removed_indices, value):
    for j in range(len(removed_indices) - 1):
        value_idx = _idx_after_removal(removed_indices[:j + 1], value)
        if value_idx % removed_indices[j + 1] == 0:
            return True
    return False


def lucky():
    yield 1
    removed_indices = [2]
    for i in itertools.count(3, 2):
        if not _should_be_excluded(removed_indices, i):
            yield i
            removed_indices.append(i)
            removed_indices = list(set(removed_indices))
            removed_indices.sort()

If you want to extract for example the 100th term from this generator, you can use itertools nth recipe:

def nth(iterable, n, default=None):
    "Returns the nth item or a default value"
    return next(itertools.islice(iterable, n, None), default)

print nth(lucky(), 100)

I hope this works, and there's without any doubt more room for code improvement (but as stated previously, there's always room for improvement!).

like image 97
icecrime Avatar answered Oct 25 '22 20:10

icecrime