Skip to content

Instantly share code, notes, and snippets.

@Double-A-92
Last active June 23, 2020 13:06
Show Gist options
  • Save Double-A-92/1f44c1e7b63fbf94ac8141b0f7b4f5de to your computer and use it in GitHub Desktop.
Save Double-A-92/1f44c1e7b63fbf94ac8141b0f7b4f5de to your computer and use it in GitHub Desktop.
Write a program that prints the next 20 leap years.
# Input
CURRENT_YEAR = 2017
NOF_LEAP_YEARS = 20
# Functions
def is_leap_year(year):
if year % 4 > 0:
return False
elif year % 100 > 0:
return True
elif year % 400 > 0:
return False
else:
return True
def print_next_leap_years(current_year, nof_leap_years):
nof_leap_years_found = 0
while nof_leap_years_found < nof_leap_years:
if is_leap_year(current_year):
nof_leap_years_found += 1
print(current_year)
current_year += 1
def print_next_leap_years_with_recursion(current_year, nof_leap_years, nof_leap_years_found = 0):
if is_leap_year(current_year):
nof_leap_years_found += 1
print(current_year)
if nof_leap_years_found < nof_leap_years:
print_next_leap_years_with_recursion(current_year + 1, nof_leap_years, nof_leap_years_found)
# Main
print_next_leap_years(CURRENT_YEAR, NOF_LEAP_YEARS)
print_next_leap_years_with_recursion(CURRENT_YEAR, NOF_LEAP_YEARS)
@JasonG15295
Copy link

Cool! Try Lua for a challenge... ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment