4 ms·
We can go a little shorter with C :) main(i){for(;i<101;puts(printf(i%3?"%d\r":"fizz",i)&&i++%5?"":"buzz"));}
by Zelizz 9y ago
We can go a little shorter with C :)
main(i){for(;i<101;puts(printf(i%3?"%d\r":"fizz",i)&&i++%5?"":"buzz"));}
- arthurcolle 9y agoThug life
- KirinDave 9y agoThe nightmare is real.
- feelin_googley 9y agoYou're hired.
- AlexCoventry 9y agoHow does the divisible-by-five case suppress the numeral output, when `i%3` is nonzero?
- vidarh 9y agoThe "\r" in the format string for the number is carriage return. It ensures that as long as you print to terminal rather than redirect to file etc., the number gets overwritten if anything else gets printed on the same line.
- AlexCoventry 9y agoAh, thank you.
- vidarh 9y agoIgnoring that this only works if "\r" works, here's a shorter Ruby version without even trying hard: (1..100).each{|i|e=[["fizz"][i%3],["buzz"][i%5]].join;puts(e[0]?e:i)} (array lookup with out-of-range offset returns nil, Array#join produces empty strings for nil elements, and accessing element 0 in a zero length string returns nil.