Skip to content

Instantly share code, notes, and snippets.

@tstone
Created October 29, 2009 07:25
Show Gist options
  • Save tstone/221242 to your computer and use it in GitHub Desktop.
Save tstone/221242 to your computer and use it in GitHub Desktop.
Recursive Python FizzBuzz solution
def fizzbuzz (start, end):
if start % 15 == 0:
print 'fizzbuzz'
elif start % 3 == 0:
print 'fizz'
elif start % 5 == 0:
print 'buzz'
else:
print start
if start < end:
fizzbuzz(start+1, end)
fizzbuzz(1, 100)
@impredicative
Copy link

impredicative commented Oct 6, 2017

Your implementation can be rewritten with the signature def fizbuzz(end, start=1) and called accordingly. Externally this updated call can then still be called the normal way, i.e. as fizzbuzz(100).

Anyhow, here is my recursive Python 3 implementation which doesn't require start:

def fizzbuzz_recursive(n):
    if n == 0:
        return
    fizzbuzz_recursive(n - 1)
    if n % 3 == 0 and n % 5 == 0:
        print('FizzBuzz')
    elif n % 3 == 0:
        print('Fizz')
    elif n % 5 == 0:
        print('Buzz')
    else:
        print(n)

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