10
\$\begingroup\$

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!
\$\endgroup\$
  • 2
    \$\begingroup\$ I don't really mind. If you want to use emoji instead of digits, I still don't mind. It's the base that matters, not the representation. \$\endgroup\$ – Geza Kerecsenyi Feb 22 at 21:42
  • 2
    \$\begingroup\$ Ok, clarified now. \$\endgroup\$ – Geza Kerecsenyi Feb 22 at 22:00
  • 5
    \$\begingroup\$ Technically, in base 36 the word buzz appears by itself at index 553391, fizz at 724463, and fizzbuzz at 1216820199599. Sadly, none of them are divisible by that base's numbers \$\endgroup\$ – Jo King Feb 22 at 22:36
  • 3
    \$\begingroup\$ Why isn't base 10 the original FizzBuzz?: I couldn't think of an algorithm which would both generate the original numbers in base 10, and transfer well to other bases. And I didn't want to add a special case for denary since that would just overcomplicate the question, in my opinion. \$\endgroup\$ – Geza Kerecsenyi Feb 23 at 10:11
  • 3
    \$\begingroup\$ For your future challenges, please consider allowing more flexible I/O. Cumbersome I/O formats is the most upvoted topic in Things to avoid when writing challenges. \$\endgroup\$ – Arnauld Feb 23 at 17:03

10 Answers 10

3
\$\begingroup\$

Jelly,  42 38 34 33 29  32 bytes

+3 to adhere to strict formatting rules

5ȷɓ;8Ä:2,3‘ḍȧ"“Ƈד=%»ḟ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).

Try it online!

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...

5ȷɓ;8Ä:2,3‘ḍȧ"“Ƈד=%»ḟ0Fȯb@ð€ - Link: integer, b
5ȷ                            - 5*10³ = 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']
\$\endgroup\$
  • \$\begingroup\$ The challenge states that output must be on seperate lines \$\endgroup\$ – Embodiment of Ignorance Feb 23 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 Feb 23 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 Feb 23 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 Feb 23 at 16:11
3
\$\begingroup\$

Charcoal, 40 bytes

NθE…·¹×⁵φ∨⁺⎇﹪ι⊕÷θ²ωFizz⎇﹪ι÷⁺¹¹θ³ωBuzz⍘ιθ

Try it online! Link is to verbose version of code. Explanation:

Nθ                                      Input `b` into variable `q`
   ¹                                    Literal 1
 …·                                     Inclusive range to
      φ                                 Predefined variable 1000
    ×                                   Multiplied by
     ⁵                                  Literal 5
E                                       Map to
           ι                            Current value
          ﹪                             Modulo
              θ                         Input value
             ÷                          Floor divide
               ²                        Literal 2
            ⊕                           Incremented
         ⎇                              If nonzero
                ω                       Then predefined empty string
                 Fizz                   Otherwise literal `Fizz`
        ⁺                               Concatenated with
                       ι                Current value
                      ﹪                 Modulo
                            θ           Input value
                         ⁺              Plus
                          ¹¹            Literal 11
                        ÷               Integer divided by
                             ³          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
\$\endgroup\$
3
\$\begingroup\$

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')

Try it online!

Thanks to @digEmAll for saving 23 bytes. I then further golfed @digEmAll’s efforts to save a further 9.

\$\endgroup\$
  • \$\begingroup\$ 140 byte I worked on this without looking at your answer, but it's too similar for another post ;) \$\endgroup\$ – digEmAll Feb 23 at 16:40
  • \$\begingroup\$ @digEmAll thanks. I’ve further golfed your answer to get 131 bytes and given you credit; hope that’s ok. \$\endgroup\$ – Nick Kennedy Feb 23 at 18:15
  • \$\begingroup\$ absolutely ! ;) \$\endgroup\$ – digEmAll Feb 23 at 18:16
  • \$\begingroup\$ BTW, if you have any discussion about golfing in R ask on this chat \$\endgroup\$ – digEmAll Feb 23 at 19:04
  • \$\begingroup\$ Oops, didn't realize there was already an R answer, my answer might be helpful for a bit more golfing? \$\endgroup\$ – ASCII-only Feb 25 at 22:56
3
\$\begingroup\$

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)

Try it online!

(limited to 100 so that TIO's output does not blow up)

\$\endgroup\$
  • \$\begingroup\$ Any chance you could explain what |0 and (5e3) does? \$\endgroup\$ – njras Feb 26 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}\$). 5e3 is the exponential notation for 5000 -- which is here the argument passed to the outer arrow function \$g\$. \$\endgroup\$ – Arnauld Feb 26 at 15:48
2
\$\begingroup\$

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

Try it online!

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 39 37 36 bytes

8+‚U5₄*LεX23S÷>Ö”FizzÒÖ”#×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`
5₄*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".

\$\endgroup\$
  • \$\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 Feb 26 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 as DõQi yIв, but I have the feeling it can be golfed some more..) \$\endgroup\$ – Kevin Cruijssen Feb 26 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 Feb 26 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 Feb 26 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 Feb 26 at 15:00
0
\$\begingroup\$

Perl 6, 91 bytes

{$!=0;('Fizz'x++$!%%($_/2+|0+1)~'Buzz'x$!%%(($_+8)/3+|0+1)||[R,] $!.polymod($_ xx*))xx𐄦}

Try it online!

Anonymous code block that returns a list of either strings of Fizz/Buzz/FizzBuzz or a reversed list of integers in the base.

\$\endgroup\$
0
\$\begingroup\$

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)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 180 171 bytes

n=>{for(int i=1,p;i<5001;){p=i;var j=new Stack<int>();for(;p>0;p/=n)j.Push(p%n);var s=i%(n/2+1)<1?"Fizz":"";Print(i++%((n+8)/3+1)<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.

Try it online!

\$\endgroup\$
  • 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 Feb 23 at 7:18
0
\$\begingroup\$

05AB1E, 34 bytes

Uses Jonathan's mathematical insight that ceil(n/3+3) = floor((n+8)//3)+1

ŽJćG8+‚2L>÷>NsÖ…™Ázz'ÒÖ‚×JNIв‚õKн,

Try it online!

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
               …™Á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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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