Created
November 23, 2024 22:45
-
-
Save james-world/e568245a324510bdc92a3eda5a3d6099 to your computer and use it in GitHub Desktop.
Calculate lucky numbers with no repeating digits in python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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