Challenge
Input:
An integer \$b\$ between 2 and 62 (inclusive).
Output:
Count from \1ドル\$ to the equivalent of \5000ドル_{10}\$ in base \$b\$, using any reasonable representation for the digits.
However:
If the number is divisible by \$\lfloor b÷2+1\rfloor\$ (rounded down, e.g base 7 would be 7/2=3.5, 3.5+1=4.5, rounded to 4), then output 'Fizz' instead of the number.
If the number is divisible by \$\lceil b÷3+3\rceil\$ (rounded up, e.g 11/3=3.666, 3.666+3=6.666, rounded to 7), then output 'Buzz'.
As you can probably guess, if your number is divisible by both, output 'Fizzbuzz'.
Examples
Using [0-9], [A-Z] and [a-z] as the digits
(I've only included the first 10 values to keep the examples short - normally there'd by 4990 more items in each sequence)
Input: 10 (so 'Fizz' = 6 and 'Buzz' = 7)
Output: 1, 2, 3, 4, 5, Fizz, Buzz, 8, 9, 10
Input: 2 (so 'Fizz' = 2 and 'Buzz' = 4)
Output: 1, Fizz, 11, Fizzbuzz, 101, Fizz, 111, Fizzbuzz, 1001, Fizz
(I've included the first 50 values of the following to better show how they work)
Input: 55 (so 'Fizz' = \28ドル_{10}\$ = \$s_{55}\$ and 'Buzz' = \22ドル_{10}\$ = \$m_{55}\$)
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, g, h, i, j, k, l, Buzz, n, o, p, q, r, Fizz, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N
Rules
- Standard loopholes are forbidden
- This is Code Golf, so shortest answer in bytes wins
- Input and output can be through console, or function arguments/returns
- Leading/trailing white space is fine, as are empty lines
- Spaces between 'Fizz' and 'Buzz' are disallowed
- Any capitalization variant of 'Fizz'/'Buzz'/'Fizzbuzz' is fine.
- Outputs should be separated by newlines.
- If you return a array of base 10 'digits' instead of representing them with characters, then they have to be in the correct order!
12 Answers 12
Charcoal, 40 bytes
×ばつ5φ∨+⎇%ι⊕÷θ2ωFizz⎇%ι÷+11θ3ωBuzz⍘ιθ
Try it online! Link is to verbose version of code. Explanation:
Nθ Input `b` into variable `q`
1 Literal 1
...· Inclusive range to
φ Predefined variable 1000
×ばつ Multiplied by
5 Literal 5
E Map to
ι Current value
% Modulo
θ Input value
÷ Floor divide
2 Literal 2
⊕ Incremented
⎇ If nonzero
ω Then predefined empty string
Fizz Otherwise literal `Fizz`
+ Concatenated with
ι Current value
% Modulo
θ Input value
+ Plus
11 Literal 11
÷ Integer divided by
3 Literal 3
⎇ If nonzero
ω Then predefined empty string
Buzz Otherwise literal `Buzz`
∨ Logical Or
ι Current value
⍘ Converted to base
θ Input value
Implicitly print each result on its own line
Jelly, (削除) 42 38 34 33 29 (削除ここまで) 32 bytes
+3 to adhere to strict formatting rules
×ばつ"=%»ḟ0Fȯb@K\ð€Y
A full program which prints 5000 lines of text, each line containing a series of integers (the digits) or one of fizz, buzz, or fizzbuzz (works fine beyond base 62).
How?
Note that
\$\lfloor b÷2+1\rfloor\ = \lfloor b÷2\rfloor+1\$
...and
\$\lceil b÷3+3\rceil = \lceil b÷3+2\rceil+1 = \lceil (b+6)÷3\rceil+1 = \lfloor (b+8)÷3\rfloor+1\$
updating...
×ばつ"=%»ḟ0Fȯb@ð€ - Link: integer, b
5ȷ - 5*103 = 5000
ɓ ð€ - for €ach n in [1,2,...,5000] get this f(b,n):
8 - eight
; - concatenate -> [b,8]
Ä - cumulative sums -> [b,b+8]
2,3 - pair literal [2,3]
: - integer division -> [b//2, (b+8)//3]
‘ - increment -> [b//2+1, (b+8)//3+1]
ḍ - divides n? -> [n is fizzy?, n is buzzy?]
×ばつ"=%» - list of dictionary strings = ['fizz','buzz']
" - zip with:
ȧ - logical AND -> [0,0], ['fizz',0], [0,'buzz'],
- or ['fizz','buzz']
0 - zero
ḟ - filter discard -> [], ['fizz'], ['buzz'],
- or ['fizz','buzz']
F - flatten -> [], ['fizz'], ['buzz'],
- or ['fizzbuzz']
@ - using swapped arguments:
b - (n) to a list of digits in base (b) (say, [nb])
ȯ - logical OR -> [nb], ['fizz'], ['buzz'],
- or ['fizzbuzz']
-
\$\begingroup\$ The challenge states that output must be on seperate lines \$\endgroup\$Gymhgy– Gymhgy2019年02月23日 04:02:27 +00:00Commented Feb 23, 2019 at 4:02
-
\$\begingroup\$ True. While I'm fine with the representation (though it's borderline), outputting an array isn't the same as playing the game. However, if you output the array of digits on each line, I'll accept it. \$\endgroup\$Geza Kerecsenyi– Geza Kerecsenyi2019年02月23日 10:00:39 +00:00Commented Feb 23, 2019 at 10:00
-
3\$\begingroup\$ @GezaKerecsenyi Updated. However, I don't understand how the representation is "borderline" - in base b the digits are in \$[0,n-1]\$. Or how outputting an array isn't "playing the game" ("Input and output can be through console, or function arguments/returns"). Using some other arbitrary output is as simple as substitution and formatting is just boilerplate and orthogonal to the core of the challenge. \$\endgroup\$Jonathan Allan– Jonathan Allan2019年02月23日 16:11:30 +00:00Commented Feb 23, 2019 at 16:11
-
\$\begingroup\$ A terser implementation could just take a list of the desired digits, instead of the numerical base, like this. \$\endgroup\$Jonathan Allan– Jonathan Allan2019年02月23日 16:11:35 +00:00Commented Feb 23, 2019 at 16:11
R, (削除) 163 (削除ここまで) 131 bytes
b=scan();for(d in 1:5e3)cat(list(d%/%b^rev(0:log(d,b))%%b,'fizz','buzz','fizzbuzz')[[1+(!d%%((b+2)%/%2))+2*!d%%((b+11)%/%3)]],'\n')
Thanks to @digEmAll for saving 23 bytes. I then further golfed @digEmAll’s efforts to save a further 9.
-
-
\$\begingroup\$ @digEmAll thanks. I’ve further golfed your answer to get 131 bytes and given you credit; hope that’s ok. \$\endgroup\$Nick Kennedy– Nick Kennedy2019年02月23日 18:15:00 +00:00Commented Feb 23, 2019 at 18:15
-
\$\begingroup\$ absolutely ! ;) \$\endgroup\$digEmAll– digEmAll2019年02月23日 18:16:03 +00:00Commented Feb 23, 2019 at 18:16
-
-
\$\begingroup\$ Oops, didn't realize there was already an R answer, my answer might be helpful for a bit more golfing? \$\endgroup\$ASCII-only– ASCII-only2019年02月25日 22:56:43 +00:00Commented Feb 25, 2019 at 22:56
Python 2, 116 bytes
b=input()
i=0
exec"a=i=i+1;d=[]\nwhile a:d=[a%b]+d;a/=b\nprint'Fizz'*(i%(b/2+1)<1)+'Buzz'*(i%(~-b/3+4)<1)or d;"*5000
Or with 0-9a-zA-Z output:
Python 2, 143 bytes
b=input()
i=0
exec"a=i=i+1;d=''\nwhile a:d=chr(a%b+48+(a%b>9)*39-a%b/36*58)+d;a/=b\nprint'Fizz'*(i%(b/2+1)<1)+'Buzz'*(i%(~-b/3+4)<1)or d;"*5000
JavaScript (ES6), (削除) 117 (削除ここまで) 116 bytes
Outputs comma-delimited digits, each digit being expressed as a decimal quantity (e.g. \19ドル_{20}\$ is \19ドル\$ and \21ドル_{20}\$ is \1,1ドル\$).
b=>(g=n=>n>1?g(n-1)+`
`+((s=n%(b+2>>1)?'':'Fizz',n%(b/3+3.9|0)?s:s+'Buzz')||(g=n=>n?[...g(n/b|0),n%b]:s)(n)):1)(5e3)
(limited to 100 so that TIO's output does not blow up)
-
\$\begingroup\$ Any chance you could explain what
|0and(5e3)does? \$\endgroup\$user85659– user856592019年02月26日 15:16:16 +00:00Commented Feb 26, 2019 at 15:16 -
\$\begingroup\$ @psinaught Performing a bitwise OR with \0ドル\$ on a number \$n\$ forces it to be coerced to the nearest smaller integer, so it's basically equivalent to
Math.floor(n)(except it only works for \0ドル\le n<2^{31}\$).5e3is the exponential notation for5000-- which is here the argument passed to the outer arrow function \$g\$. \$\endgroup\$Arnauld– Arnauld2019年02月26日 15:48:35 +00:00Commented Feb 26, 2019 at 15:48
Julia, 108
h(b,N)=join((n->[string(n,base=b),"Buzz","Fizz","FizzBuzz"][1+(1>n%(b÷2+1))*2+(1>n%((b+11)÷3))]).(1:N),"\n")
ATOable
-
1
-
\$\begingroup\$ you're supposed to count to 5000, not take it as an input I think \$\endgroup\$MarcMush– MarcMush2022年06月13日 21:15:19 +00:00Commented Jun 13, 2022 at 21:15
Perl 6, 91 bytes
{$!=0;('Fizz'x++$!%%($_/2+|0+1)~'Buzz'x$!%%(($_+8)/3+|0+1)||[R,] $!.polymod($_ xx*))xx𐄦}
Anonymous code block that returns a list of either strings of Fizz/Buzz/FizzBuzz or a (削除) reversed (削除ここまで) list of integers in the base.
-
\$\begingroup\$ See codegolf.stackexchange.com/questions/180247/…. I've added a clarification in the question about this. \$\endgroup\$Geza Kerecsenyi– Geza Kerecsenyi2019年02月23日 10:15:08 +00:00Commented Feb 23, 2019 at 10:15
05AB1E, (削除) 39 (削除ここまで) (削除) 37 (削除ここまで) 36 bytes
8+‚U54*LεX23S÷>Ö"Fi×ばつJDõQiyIв]»
-2 bytes by creating a port of @JonathanAllan's Jelly answer.
Try it online or verify all test cases (but as list-output and with the first 100 instead of 5000).
Explanation:
8+ # Add 8 to the (implicit) input
‚ # Pair it with the (implicit) input
U # Pop and store it in variable `X`
54*L # Create a list in the range [1,5000]
ε # Map each value `y` to:
X23S÷ # Integer-divide the input by 2, and the input+8 by 3
> # Increase both by 1
Ö # Check for both if they divide value `y` evenly (1 if truthy; 0 if falsey)
"FizzÒÖ" # Push dictionary string "Fizz Buzz"
# # Split on spaces
×ばつ # Repeat the strings the result amount of times (0 or 1)
J # Join both strings together to a single string
DõQi # If this string is empty:
yIв # Push value `y` in Base-input (as list) instead
] # Close the if-statement and map
» # Join the list by new-lines (and inner lists by spaces implicitly)
# (and output the result implicitly)
See this 05AB1E tip of mine (section How to use the dictionary?) to understand why "FizzÒÖ" is "Fizz Buzz".
-
\$\begingroup\$ I have a looping version at 33 bytes in case you wanna try and optimize this. Still a byte longer than Jelly though :( \$\endgroup\$Emigna– Emigna2019年02月26日 11:52:29 +00:00Commented Feb 26, 2019 at 11:52
-
\$\begingroup\$ @Emigna Have you already post it? Or it's fairly similar to my answer and it is meant as a golf? PS: Jelly has a few short-cut builtins with for example the
[input, input+8]part, and the filter part afterwards (which I now do asDõQi yIв, but I have the feeling it can be golfed some more..) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年02月26日 12:31:14 +00:00Commented Feb 26, 2019 at 12:31 -
\$\begingroup\$ Levenshtein-distance wise it's very different from yours (although parts can be rewritten to be more like yours). But I also use Jonathan's n+8 trick so I figured I'd let you try and golf yours down towards it if you want. Otherwise I'll post it as a separate answer. \$\endgroup\$Emigna– Emigna2019年02月26日 12:38:24 +00:00Commented Feb 26, 2019 at 12:38
-
\$\begingroup\$ @Emigna I'm currently a bit busy at work, so don't really have time to golf this answer. So if you want you can post it as your own answer. You've got my upvote. ;) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年02月26日 12:42:23 +00:00Commented Feb 26, 2019 at 12:42
-
\$\begingroup\$ Unfortunately I discovered a bug in my version so now it's 34. I have some ideas that might make yours or mine shorter that I'll explore later. Beating/tying Jelly seems tough now though. \$\endgroup\$Emigna– Emigna2019年02月26日 15:00:53 +00:00Commented Feb 26, 2019 at 15:00
R, 138 bytes
function(b,`*`=rep)Map(function(i)"if"((s=paste0("","Fizz"*!i%%(b%/%2+1),"Buzz"*!i%%((b+11)%/%3)))>0,s,i%/%b^((log(i,b)%/%1):0)%%b),1:5e3)
05AB1E, 34 bytes
Uses Jonathan's mathematical insight that ceil(n/3+3) = floor((n+8)//3)+1
ŽJćG8+‚2L>÷>Ns×ばつJNIв‚õKн,
Explanation
ŽJćG # for N in [1 ...5001)
8+‚2L>÷> # push [input//2+1, (input+8)//3+1]
NsÖ # check each if N is divisible by it
...TMÁzz # push "fizz"
'ÒÖ # push "buzz"
‚ # pair
×ばつ # repeat a number of times corresponding to the result of the
# divisibility test
J # join to string
NIв‚ # pair with N converted to base <input>
õK # remove empty string
н, # print head of the remaining list
C# (Visual C# Interactive Compiler), (削除) 180 (削除ここまで) (削除) 171 (削除ここまで) 168 bytes
n=>{for(int i=1,p;i<5001;){var j=new Stack<int>();for(p=i;p>0;p/=n)j.Push(p%n);var s=i%~(n/2)<1?"Fizz":"";Print(i++%((n+11)/3)<1?s+"Buzz":s==""?string.Join("-",j):s);}}
Outputs like Arnauld's answer. Thanks to digEmAll for the idea of using a stack to reverse the output.
Saved 3 bytes thanks to ceilingcat
-
1\$\begingroup\$ I'm going to have to say 'no' to the reverse digits. While it's the correct idea mathematically, it's not the Fizzbuzz program, say an employer would want. It's unfortunate that C# doesn't have an array reverse function. \$\endgroup\$Geza Kerecsenyi– Geza Kerecsenyi2019年02月23日 07:18:39 +00:00Commented Feb 23, 2019 at 7:18
buzzappears by itself at index553391,fizzat724463, andfizzbuzzat1216820199599. Sadly, none of them are divisible by that base's numbers \$\endgroup\$