In the wake of the many (two?) FizzBuzz-related challenges posted recently on PPCG, I've been tempted to come up with my own. Behold...

Fizz Buzz Lightyear

Write a program or function that takes an integer n and prints out FizzBuzz for any number divisible by 15, Fizz for any number divisible by 3, and Buzz for any number divisible by 5, up to (and including) n. Output for all i must be followed by a newline. But there's a twist!

For every third time you print Buzz, Buzz Lightyear finally heeds your call and crash lands in your program. He then introduces himself - but since he crash landed, some of what he said gets mixed up with your program's output:

Buzz Lightyear, Space Ranger, Universe Protection Unit.
FizzBuzz Lightyear, Space Ranger, Universe Protection Unit.

(that is, only append  Lightyear, Space Ranger, Universe Protection Unit. to Buzz or FizzBuzz - whatever it is you'd have displayed otherwise. Note the leading space)

However, Buzz Lightyear, being the Space Ranger he is, has very acute hearing, and so printing FizzBuzz will count towards your Buzz count.

Then, Buzz hangs around to defend your computer from all of that evil output, until you hit another number that's divisible by 5 (or 15, since those are divisible by 5, too). What that means is until you have to print Buzz (or FizzBuzz) again, you don't print anything at all.

When you finally reach that condition, Buzz departs:

To infinity and beyond!

Example Output

This is the expected output for n = 25: (notice how it skips 16 through 19)

FizzBuzz Lightyear, Space Ranger, Universe Protection Unit.
To infinity and beyond!


Optional trailing newline is acceptable.

This is code golf; as such, shortest code, in bytes, wins.

Assume given n is valid and greater than or equal to 15 (which is when the challenge first deviates from standard fizzbuzz)

Buzz greets you when the "Buzz counter" (which counts both Buzz and FizzBuzz) hits 3; he departs when the next Buzz (including, again, both Buzz and FizzBuzz) is printed.

The number in which he departs does not count towards the next "Buzz counter"; you should instead start counting the Buzzes again from 0. For instance, a program running with n = 25 (example output) should end with a "Buzz counter" of 1, since that's how many times Buzz was printed since the last time he departed.

In case n falls between one of Buzz's arrivals and one of his departures (i.e., he's still there - you're not printing anything), graceful termination is expected. Therefore, the last line of output would be his introduction

Standard loopholes are forbidden.

  • 1
    \$\begingroup\$ Can you clarify the expected behavior when n falls in-between Buzz Lightyear's arrival and his departure? \$\endgroup\$
    – nderscore
    Feb 22, 2017 at 20:49
  • \$\begingroup\$ Simply quit the program. I'll edit the rules in a second \$\endgroup\$
    – osuka_
    Feb 22, 2017 at 20:50

3 Answers 3


Javascript (ES6), 182 175 bytes

  • -7 bytes: Moved Buzz Lightyear logic into Buzz ternary expression.

f=(n,s=i=b=_='')=>i++<n?f(n,s+`${(i%3?_:'Fizz')+(i%5?_:`Buzz${++b%3?_:` Lightyear, Space Ranger, Universe Protection Unit.${(i+=5)>n?_:`
To infinity and beyond!`}`}`)||i}
<!-- snippet demo: -->
<input oninput=o.innerHTML=f(this.value)>
<pre id=o>


Python 2, 185 178 172 bytes

for i in range(input()):
 if-~i%20<16:print i%20/19*"To infinity and beyond!"or i%3/2*"Fizz"+i%5/4*"Buzz"+i%20/14*" Lightyear, Space Ranger, Universe Protection Unit."or-~i

Try it online!


Observe: Buzz Lightyear arrives on the third "buzz number" and departs on the fourth. "Buzz numbers" are the multiples of five. Thus, Buzz's movements happen on a cycle of length 20.

We loop over each i from 0 through input-1. (This means that i is always one less than the actual number we're considering.)

Using -~i as a shortcut for i+1, if-~i%20<16: checks if i+1, mod 20, is 15 or under. (If it's 16 to 19, Buzz Lightyear is present and we don't want to output anything.)

Inside the if statement, we want to print To infinity and beyond! on every multiple of 20--that is, every time i%20 is 19. (Remember that i is one less than the actual number.) Since i%20 will never be greater than 19, i%20/19 will be 1 in the desired case, <1 otherwise. Python 2, conveniently, truncates floats when multiplying by strings, so i%20/19*"..." gives the full string if i%20 is 19, otherwise "".

If the above case applies, we don't print anything else. But if the first expression is "" (which is falsy), we use or to keep going. The expressions for Fizz, Buzz, and the introduction are computed similarly to the above and added together.

Finally, if none of these cases applies, we print the number itself with -~i.


05AB1E, 97 93 90 bytes

>GN"FizzBuzz"2äN35SÖÏJ)˜1(è“To infinity€ƒ—°!“)N20%©_è®15Q” Lightyear,‡²ìÓ,ªÜŠí‰¿.”×J®16‹i,

Try it online!

Explanation to come after further golfing.

Alternative 97 byte version

>G"FizzBuzz"2ä” Lightyear,‡²ìÓ,ªÜŠí‰¿.”)˜N•9¨•3äR%15%_ÏJ“To infinity€ƒ—°!“)N20ÖèN)˜é®èN20%15›i\},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.