Introduction
Everyone knows the FizzBuzz sequence. It goes something like this:
12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz...In case you don't know, if the number is divisible by 3, it'sFizz. If it is divisible by 5, it'sBuzz. If it is divisible by both, it'sFizzBuzz. If it is not divisible by both, it's just the original number.
Task
Take two inputs, for example (space separated here)
Fizz 3For this specific example input, you should output9, the thirdFizz. To be more general, take aword and anumber, output thenumberthword.
Theword input may beNumber, and in this case, you should output thenumberth number in the FizzBuzz sequence that is notFizz,Buzz, orFizzBuzz.
You may choose any 4 distinct, consistent inputs representingFizz,Buzz,FizzBuzz andNumber.
Test Cases
Fizz 3 => 9Buzz 4 => 25FizzBuzz 2 => 30Number 312 => 584Scoring
Shortest code wins!
Rules
- No standard loopholes.
- 2\$\begingroup\$May we choose to take different 4 distinct consistent inputs to represent Fizz, Buzz, FizzBuzz and Number?\$\endgroup\$pajonk– pajonk2021-12-17 19:02:11 +00:00CommentedDec 17, 2021 at 19:02
- 1\$\begingroup\$@pajonk Yes, you may.\$\endgroup\$Alan Bagel– Alan Bagel2021-12-17 19:03:20 +00:00CommentedDec 17, 2021 at 19:03
- 3\$\begingroup\$I think
Number 312might be584.\$\endgroup\$Arnauld– Arnauld2021-12-17 19:19:23 +00:00CommentedDec 17, 2021 at 19:19 - \$\begingroup\$Why space separated input? I'd suggest just going by our default I/O rules\$\endgroup\$2021-12-17 19:32:20 +00:00CommentedDec 17, 2021 at 19:32
- \$\begingroup\$@cairdcoinheringaahing That was an example. You can do list, newline sep, etc.\$\endgroup\$Alan Bagel– Alan Bagel2021-12-17 19:35:22 +00:00CommentedDec 17, 2021 at 19:35
14 Answers14
Jelly, 10 bytes
³3,5ḍḄ⁼ʋ#ṪThis uses0 = Number,1 = Buzz,2 = Fizz and3 = FizzBuzz
Jelly, 8 bytes
1g15=ɗ#ṪThanks toLynn. This uses1 = Number,3 = Fizz,5 = Buzz,15 = FizzBuzz. Included separately as the numbers could encode extra data
How they work
³3,5ḍḄ⁼ʋ#Ṫ - Main link. Takes W=0,1,2,3 on the left, n on the right ʋ - Last 4 links as a dyad f(k, W): 3,5ḍ - Divisible by 3 or 5? Yields [0,0], [0,1], [1,0], [1,1] Ḅ - From binary; Yields 0, 1, 2, 3 ⁼ - Equals W?³ #Ṫ - Starting from W, count up k = W, W+1, ..., returning the nth integer such that f(k, W) is true1g15=ɗ#Ṫ - Main link. Takes W=1,3,5,15 on the left, n on the right ɗ - Last 3 links as a dyad f(k, W): g15 - GCD(k, 15) = - Does that equal W?1 #Ṫ - Count up k = 1, 2, ..., returning the nth integer such that f(k, W) is true- 5\$\begingroup\$
1g15=ɗ#Ṫworks for 8, with Number = 1, Fizz = 3, Buzz = 5, FizzBuzz = 15.\$\endgroup\$lynn– lynn2021-12-17 20:06:23 +00:00CommentedDec 17, 2021 at 20:06 - \$\begingroup\$@Lynn I've added that as an alternative, as using the specific values might be taken to encode extra data\$\endgroup\$2021-12-17 20:10:13 +00:00CommentedDec 17, 2021 at 20:10
- \$\begingroup\$I was going to upvote, but I accidentally downvoted. Now fixed.\$\endgroup\$Alan Bagel– Alan Bagel2021-12-18 11:53:30 +00:00CommentedDec 18, 2021 at 11:53
JavaScript (ES6), 44 bytes
Expects(s)(n) with s=0 forNumber, s=1 forFizz, s=2 forBuzz and s=3 forFizzBuzz.
(s,k=0)=>g=n=>n?g(n-=s==!(++k%3)+2*!(k%5)):kCheaty version, 35 bytes
Expects 4680 forFizz, 1056 forBuzz, 1 forFizzBuzz and 27030 forNumber.
(s,k=0)=>g=n=>n?g(n-=s>>++k%15&1):kPython 2, 47 bytes
f=lambda n,c,k=1:n and-~f(n-(k**4%15==c),c,k+1)Take in the category labelc as:
Number -> 1Fizz -> 6Buzz -> 10FizzBuzz -> 0We fingerprint the category fork usingk**4%15, which produces the corresponding value as listed above. This is wrapped in arecursive function for the n'th number meeting a condition.
Python 2, 54 bytes
lambda n,c:([n*7/8-3*~n/4%2,~-n/4,~-n/2,0][c/2%4]+n)*cA short at writing direct formulas for each case, with inputc as one of1,3,5,15.
- \$\begingroup\$For the direct formulas you can combine the Fizz and Buzz cases into one:
[n*7/8-3*~n/4%2,0,~-n>>6/c][c/3/-3]\$\endgroup\$ovs– ovs2021-12-18 00:05:33 +00:00CommentedDec 18, 2021 at 0:05
Perl 5 -p,52 47 bytes
Saved 5 bytes thanks to @Dom Hastings.
/ /;$_=332312332132330x$';/(.*?$`){$'}/g;$_=posWhere 0=FizzBuzz, 1=Buzz, 2=Fizz, 3=Number in the first of the two inputs. Finds the position of the n'th occurrence of what's wanted with a regexp search in a repeated 15 char string encoding number, fizz, buzz and fizzbuzz in their right places.
- \$\begingroup\$Nice approach. I ended up with a much longer version... You can save a few bytes swapping the order of input too!:Try it online!\$\endgroup\$Dom Hastings– Dom Hastings2021-12-18 08:31:02 +00:00CommentedDec 18, 2021 at 8:31
- 1\$\begingroup\$Actually, using
/ /and$'/ `` $` `` works out a bit better:Try it online!\$\endgroup\$Dom Hastings– Dom Hastings2021-12-18 23:03:08 +00:00CommentedDec 18, 2021 at 23:03 - 1\$\begingroup\$Thx @DomHastings – I need to remember those special vars.\$\endgroup\$Kjetil S– Kjetil S2021-12-19 16:20:14 +00:00CommentedDec 19, 2021 at 16:20
Python 2, 51 bytes
lambda d,n,k=0:n and-~f(d,n-(k%3/2*2+k%5/4==d),k+1)-2 bytes thanks to AnttiP; becomes -4 using Python 2
-3 bytes thanks to ovs
0 for Number, 1 for Buzz, 2 for Fizz, 3 for FizzBuzz.
- \$\begingroup\$-2 bytes with
lambda d,n,k=0:n and f(d,n-(k%3//2*2+k%5//4==d),k+1)or k\$\endgroup\$AnttiP– AnttiP2021-12-17 20:44:51 +00:00CommentedDec 17, 2021 at 20:44 - \$\begingroup\$@AnttiP Thanks! Using python 2 gets another 2 bytes as well\$\endgroup\$2021-12-17 21:41:04 +00:00CommentedDec 17, 2021 at 21:41
- \$\begingroup\$Using Python 2 is cheating, it doesn't officially exist anymore.\$\endgroup\$Mark Ransom– Mark Ransom2021-12-18 03:42:51 +00:00CommentedDec 18, 2021 at 3:42
- 3\$\begingroup\$@MarkRansom It's not officially supported by Python anymore but as long as a language has an implementation available it's valid.\$\endgroup\$2021-12-18 07:04:23 +00:00CommentedDec 18, 2021 at 7:04
R,5654 52 bytes
Edit: -4 bytes thanks to pajonk
function(n,i){while(n<-n-!(!T%%3)-i+2*!T%%5)T=T+1;T}This really seems to have too many parentheses...
Edit: This is really pajonk's answer, now, after removing all the useless parentheses that I left in the original...
- \$\begingroup\$Here's one pair of parens less.\$\endgroup\$pajonk– pajonk2021-12-18 06:36:35 +00:00CommentedDec 18, 2021 at 6:36
- \$\begingroup\$@pajonk - Thanks! I really should try to understand the precedence rules...\$\endgroup\$Dominic van Essen– Dominic van Essen2021-12-18 08:07:47 +00:00CommentedDec 18, 2021 at 8:07
- \$\begingroup\$stat.ethz.ch/R-manual/R-devel/library/base/html/Syntax.html is my favourite site when golfing in R ;)\$\endgroup\$pajonk– pajonk2021-12-18 10:04:05 +00:00CommentedDec 18, 2021 at 10:04
- \$\begingroup\$And one pair less again.\$\endgroup\$pajonk– pajonk2021-12-18 10:11:18 +00:00CommentedDec 18, 2021 at 10:11
- \$\begingroup\$@pajonk - Thanks. I think I ought to transfer this answer to you, after you've fixed all my parentheses...\$\endgroup\$Dominic van Essen– Dominic van Essen2021-12-18 12:42:49 +00:00CommentedDec 18, 2021 at 12:42
05AB1E (legacy), 9bytes
µN53SÖ2βQFirst input is the\$number\$; second is a digit\$0\$ forNumber,\$1\$ forFizz,\$2\$ forBuzz, and\$3\$ forFizzBuzz.
Uses the legacy version of 05AB1E, because it'll implicitly output the indexN after a while-loopµ. In the new 05AB1E version, an explicit trailing}N should be added to accomplish that.
Try it online orverify all test cases.
Explanation:
µ # Loop until the counter_variable is equal to the first (implicit) input N # Push the loop-index 53S # Push [5,3] Ö # Check if the loop-index is divisible by either 5 or 3 # (resulting in [0,0], [1,0], [0,1], or [1,1]) 2β # Convert it from a base-2 list to an integer # ([0,0],[1,0],[0,1],[1,1] will become 0,1,2,3 respectively) Q # And check if it's equal to the second (implicit) input # (if this is truthy: implicitly increase the counter_variable by 1) # (after the loop, the loop-index `N` is output implicitly)05AB1E (legacy), 6bytes
µN15¿QFirst input is the\$number\$; second is an integer\$1\$ forNumber,\$3\$ forFizz,\$5\$ forBuzz, and\$15\$ forFizzBuzz.
Port of@Lynn's Jelly comment (I now also notice my original program above uses a similar approach as@cairdCoinheringaahing's 10-bytes Jelly program, even though we came up with it independently. Not too surprising, since it's a pretty straight-forward approach.)
Try it online orverify all test cases.
Explanation:
µ # Loop until the counter_variable is equal to the first (implicit) input N # Push the loop-index 15¿ # Get the GCD (Greatest Common Divisor) of this index and 15 Q # And check if it's equal to the second (implicit) input # (if this is truthy: implicitly increase the counter_variable by 1) # (after the loop, the loop-index `N` is output implicitly)Retina, 60 bytes
L$`^.$'*$&¶$'*$(NNFNBFNNFBNFNNZL$`(.)+¶(?<-1>.*?\1)+$.>%`Try it online! Link includes test cases. Takes input as the letterF,B,Z orN followed by the numbern. Explanation:
L$`^.Match the letter. At this point,$' refers to the numbern following it.
$'*$&¶$'*$(NNFNBFNNFBNFNNZRepeat the lettern times, then on the next line repeat the Fizz Buzz sequence15n times.
L$`(.)+¶(?<-1>.*?\1)+Matchn copies of the letter on the first line, and use those to find thenth match of the letter on the second line.
$.>%`Output the offset (.`) of the end (>) of the match relative to the start of the second line (%).
Desmos,112 99 bytes
Input into the function\$f(n,k)\$, where\$n\$ is the number, and\$k\$ is the word.
Number = 0, Fizz = 3, Buzz = 5, FizzBuzz = 15
h(n)=floor(n)a=mod(n-1,8)f(n,k)=\{k=0:15h(n/8-1/8)+2a+h(2a/5+1)-h(a/3+2/3),k=15:kn,kn+kh(kn/15)\}Try It On Desmos! - Prettified
Uses formula inOEIS A229829:
a(n) = 15*floor((n-1)/8) +2*f(n) +floor((2*f(n)+5)/5) -floor((f(n)+2)/3), where f(n) = (n-1) mod 8.
99.99% sure this could be golfed. Maybe I could shorten the OEIS formula somehow.
Husk, 8 bytes
!¥⁰m⌋15NInput as1=number,3=Fizz,5=Buzz,15=FizzBuzz. Same approach asLynn's comment to caird coinheringaahing's answer.
Alternative versions with successively less pre-processing squeezed into the values chosen as input:
12 bytes, with input as[0,0]=number,[1,0]=Fizz,[0,1]=Buzz &[1,1]=FizzBuzz.
!fo≡⁰§e¦3¦5NTheHuskcongr command -≡ - checks whether two lists have the same distribution of truthy/falsy elements: in this case, divisibility (¦) by3 and5.
or14 bytes, finally with input just as0=number,1=Fizz,2=Buzz &3=FizzBuzz.
!¥mȯḋm¬§e%5%3NTreats list of not-modulo (¬ &%) 3 or 5 as binary digits (ḋ).
Charcoal, 22 bytes
I⊕§⌕A×”{⊞‴¡*RX⭆eR”NS⊖θTry it online! Link is to verbose version of code. Takes the number as the first input and one of the lettersF,B,Z orN as the second input. Explanation:
... Compressed string `NNFNBFNNFBNFNNZ` × Repeated by N First input as a number ⌕A Find all indices of S Second input § Indexed by θ First input ⊖ Decremented ⊕ IncrementedI Cast to string Implicitly printPython 2, 26 bytes
lambda(a,b),c:c/8*b+a[c%8]The value (a,b) should be a list/tuple of 2:
[[-1,1,2,4,7,8,11,13],15] for Number[[-3,3,6,9,12,18,21,24],30] for Fizz[[-5,5,10,20,25,35,40,50],60] for Buzz[[0,15,30,45,60,75,90,105],120] for FizzBuzzWonder if this violatesthis loophole as it's just a list. If it does, I will delete this answer.
Explore related questions
See similar questions with these tags.







