Skip to Main Content
Blog

Print all Lucky Numbers in Python

Print all Lucky Numbers in Python

Enter limit: 50
1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49,
def main():
	"""
	Prints all the lucky numbers upto n.
	"""

	limit = int(input("Enter limit: "))
	lucky = get_lucky_numbers(limit)
	print(lucky)


def get_lucky_numbers(n: int):
	"""
	Returns all the lucky numbers upto n.
	"""

	count = n
	naturals = list(range(1, count + 1))

	step_index = 1
	step_value = naturals[step_index]
	while step_value <= count:
		lucky_end = step_value - 1
		for i in range(lucky_end + 1, count + 1):
			if i % step_value != 0:
				naturals[lucky_end] = naturals[i - 1]
				lucky_end += 1

		count = lucky_end
		step_value = naturals[step_index]
		step_index += 1

	return naturals[:count]


if __name__ == "__main__":
	main()