Skip to content

Instantly share code, notes, and snippets.

@james-world
Created November 23, 2024 22:45
Show Gist options
  • Select an option

  • Save james-world/e568245a324510bdc92a3eda5a3d6099 to your computer and use it in GitHub Desktop.

Select an option

Save james-world/e568245a324510bdc92a3eda5a3d6099 to your computer and use it in GitHub Desktop.
Calculate lucky numbers with no repeating digits in python.
def generate_lucky_numbers(limit):
# Start with a list of odd numbers up to 'limit'
numbers = list(range(1, limit + 1, 2))
idx = 1 # Start with the second number in the list (index 1)
while idx < len(numbers):
step = numbers[idx]
if step >= len(numbers):
break
# Remove every 'step'-th number from the list, starting from the 'step'-1 index
del numbers[step - 1::step]
idx += 1
return numbers
# Generate lucky numbers up to 1000
lucky_numbers_up_to_1000 = generate_lucky_numbers(1000)
print(lucky_numbers_up_to_1000)
def remove_numbers_with_repeated_digits(numbers):
# Function to eliminate numbers with repeated digits
result = []
for num in numbers:
digits = str(num)
if len(set(digits)) == len(digits):
result.append(num)
return result
print(remove_numbers_with_repeated_digits(lucky_numbers_up_to_1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment