| Bytes | Lang | Time | Link |
|---|---|---|---|
| 118 | Go | 240721T025705Z | bigyihsu |
| 035 | CJam | 230213T174851Z | Helen |
| 012 | 05AB1E | 210831T065331Z | Kevin Cr |
| 122 | Python 3 | 210829T120438Z | OliveIsA |
| 317 | CSASM v2.5.1 | 210826T183439Z | absolute |
| 109 | Java JDK | 210813T090703Z | Olivier |
| 094 | Scala | 210813T185550Z | user |
| 075 | Bash | 210811T185631Z | plentyof |
| 063 | Python 2 | 210810T041505Z | Wasif |
| 048 | J | 210811T055012Z | Jonah |
| 064 | R | 210811T073411Z | pajonk |
| 069 | Haskell | 210810T160358Z | lynn |
| 065 | Python 3 | 210810T122113Z | Jitse |
| 008 | Jelly | 210810T031155Z | caird co |
| 059 | Julia 0.7 | 210810T125757Z | MarcMush |
| 029 | K ngn/k | 210810T101329Z | Razetime |
| 019 | Charcoal | 210810T100911Z | Neil |
| 056 | Perl 5 | 210810T095324Z | Kjetil S |
| 014 | Japt R | 210810T091916Z | Shaggy |
| 059 | JavaScript V8 | 210810T074642Z | Arnauld |
| 008 | Vyxal | 210810T031118Z | lyxal |
Go, 118 bytes
import(."fmt")
func f(m map[int]string,n int)(s string){for k,v:=range m{if n%k<1{s+=v}}
if s==""{s=Sprint(n)}
return}
Input is a mapping of a factor to its string, and a number to get the FizzBuzz string of it. Returns the FizzBuzzed string.
CJam, 36 35 bytes
q~{)1${\_@~;%!},_@aa?{1=}%:+oNo}%];
Explanation
q~ read the factor array and upper bound in one go
{) }% for each i from 1 to n
1${ }, get all the input factors i contains
\_@~;%! by checking i mod f = 0 for each input factor f
_@aa? if there *are* factors in i,
keep the relevant factors [[f, "Foo"], ...]
otherwise keep the number in the form [[i]]
{1=}% keep the 2nd element in each subarray
this is the texts ["Foo","Bar","Baz",...]
or just [i] if there were no input factors
:+ sum them all (concatenating texts together)
oNo print in the required format
]; clean up
(-1 byte) dropping ,: CJam automatically turns a number n into [0, n) when you map using %
Possible Changes
(-2 bytes) using p instead of oNo but this prints "Foo"/"BarBaz" instead of Foo/BarBaz
(-2 bytes) using n instead of oNo but this feature isn't in any releases (and most likely will never be)
05AB1E, 12 bytes
∞ε¹ÖÏJy‚õKн,
Integers and strings as two separated input-lists.
Outputs the infinite sequence.
Explanation:
∞ # Push an infinite positive list: [1,2,3,...]
ε # Foreach `y` over these integers:
¹Ö # Check for each integer in the first input-list whether it divides the
# current integer
Ï # Get the strings of the second (implicit) input-list at the truthy indices
J # Join this list of strings (it's now "" if none were divisible)
y‚ # Pair it with the current integer `y`
õK # Remove empty strings from this pair
н # Pop and leave the first item
, # And print it with trailing newline
Python 3, 122 bytes
exec("g="+input())
n=1
while 1:
b=1
for k,v in g:
if n%k<1:
print(v,end='')
b=0
print(n)if b else print()
n+=1
Input:
((4, "Foo"), (7, "Bar"), (9, "Baz"))
Fairly obvious way of going about the challenge. Using a tuple of tuples saves 1 byte over a dict. Looking at the other python submissions, I realize I missed a lot of optimizations. Lessons to bring to the next code golf, I hope.
CSASM v2.5.1, 317 bytes
func a:
.local a : i32
push 1
add
pop $3
pop $2
dup
len
pop $4
pop $1
lda 1
.lbl a
push 0
pop a
push -1
pop $5
push $a
push 1
sub
brfalse c
.lbl b
inc $5
push $5
push $4
sub
brfalse c
push $a
push $1
ldelem $5
rem
brtrue b
push 1
pop a
push $2
ldelem $5
print
br b
.lbl c
push a
brtrue d
push $a
print
.lbl d
push '\n'
print
inc $a
push $a
push $3
sub
brtrue a
ret
end
A function named a which expects an ~arr:i32 for the values, an ~arr:str for the names and an i32 representing \$n\$ on the stack, pushed in that order specifically.
Explanation:
func a:
; Used as the flag for if a string was printed
.local a : i32
; stack: [ values array, strings array, amount to print ]
push 1
add
pop $3
pop $2
; Duplicate "values array", get its length and store it into $4
dup
len
pop $4
pop $1
; $1 = values, $2 = strings, $3 = final number
; Initialize $a to 1
lda 1
.lbl a
; Reset the "string printed" flag
push 0
pop a
; Reset the "numbers array" iteration
push -1
pop $5
; If $a == 1, skip to the end
push $a
push 1
sub
brfalse c
.lbl b
inc $5
push $5
push $4
sub
brfalse c
; Loop through each value in the numbers array and check if a$ % them is zero
; If they are, print that string
push $a
push $1
ldelem $5
rem
brtrue b
; Modulo was zero
push 1
pop a
push $2
ldelem $5
print
br b
.lbl c
push a
brtrue d
; Print the current number
push $a
print
.lbl d
; Print the newline
push '\n'
print
; Loop while $a != $3
inc $a
push $a
push $3
sub
brtrue a
ret
end
Sample Program:
func main:
push $i32:[3,5]
push 2
newarr str
pop $a
push $a
push "Fizz"
stelem 0
push $a
push "Buzz"
stelem 1
push $a
push 30
call a
ret
end
; "func a" would be defined here
Java (JDK), 109 bytes
l->{for(int i=1;;i++){var s="";for(var e:l)if(i%e.getKey()<1)s+=e.getValue();System.out.println(s==""?i:s);}}
Notes
The s=="" works here because of the constants pool: the two references to the empty string are actually one reference and have the same address in memory. s==new String("") wouldn't have worked for instance.
Credits
- 3 bytes saved thanks to AZTECCO
Scala, 94 bytes
b=>1 to _ map(x=>println(Seq(b.collect{case(k,s)if x%k<1=>s},Seq(x))maxBy(_.size)mkString ""))
Outputs the first n lines of the sequence.
b => //A Map with the keys being divisors (4, 7, 9) and the values being strings ("Foo", "Fizz", etc.)
1 to _ //Make a range to n
map(x=> //For each x in the range, output either "x" or a string made from its divisors
println( //Print with newline
Seq( //Make a Seq with the two alternatives ^
b.collect{ //The Fizz, Buzz, FizzBuzz alternative
case(k,s) //For every divisor k and its accompanying string s,
if x%k<1 //If k divides x
=>s}, //Add s to this alternative
Seq(x) //The second alternative is just the number itself
) maxBy(_.size) //Find the alternative with more elements (if both have 1, the first is chosen)
mkString "" //Join into a single string
)
)
Bash, 100 80 75 bytes
-20 bytes thanks to @Dude coinheringaahing
-5 bytes and extra readability thanks to @Jonah
for((i=1;;i++));{
o=
for a;{((0==i%${a% *}))&&o=$o${a#* };}
echo ${o:-$i}
}
Takes arguments as strings in the form '$num $str' and uses variable expansion to split them out into their component parts. Outputs infinitely.
I'm pretty sure this one has lots of potential for further golfing, but I'll take an even 100 for my first submission.
Python 2, 63 bytes
def f(k,i=1):
print''.join(y*(i%x<1)for x,y in k)or i;f(k,i+1)
-1 bytes thanks to @dingledooper
-3 bytes thanks to @pxeger
J, 48 bytes
1 :'(|:@([:(,0=+/)0=u|/]);@#"1(,"#:<@":"+))1+i.'
This was bizarrely difficult to golf in J.
R, 76 64 bytes
-12 bytes thanks to @Dominic
function(N,S)repeat{F=F+1;cat(F[all(k<-F%%N)],S[!k],"
",sep="")}
Takes vectors of Numbers and corresponding Strings.
Outputs infinitely.
Haskell, 69 bytes
[]%n=show n
x%_=x>>=id
f a=unlines[[y|(x,y)<-a,mod i x<1]%i|i<-[1..]]
f [(3, "Fizz"), (5, "Buzz"), (10, "Boom")] is an infinite string with newlines in it.
Python 3, 65 bytes
f=lambda a,n=1:print(''.join(j*(n%i<1)for i,j in a)or n)+f(a,n+1)
-1 byte thanks to MarcMush
Jelly, 8 bytes
⁴ḍTị⁵ȯ)Y
Full program that takes \$n\$, the list of numbers \$L\$ and the list of strings \$S\$ as command line argument, and returns a newline separated string
How it works
⁴ḍTị⁵ȯ)Y - Main link. Takes n on the left, L as ⁴ and S as ⁵
) - Over each integer 1 ≤ i ≤ n:
⁴ḍ - Map each element in L to 1 if i is divisible by it, else 0
T - Indices of 1s
ị⁵ - Select the elements of S at those indices
ȯ - If this is the empty list, replace it with i
Y - Join on newlines
K (ngn/k), 29 bytes
{{$[a:&~x!'y;,/a;y]}[x]'1+!y}
Takes a dict with (string, number) pairs.
Explanation
{{$[a:&~x!'y;,/a;y]}[x]'1+!y}
1+!y range 1..y
' for each number
[x] with x as constant first argument:
{$[ ]} if:
x!'y the values of x mod y
~ logical NOTed
& filter out keys which have truthy values
a: store in a
;,/a return a if non-empty
;y otherwise return number y
Charcoal, 19 bytes
Eθ∨⭆η⎇﹪⊕ι§λ⁰ω§λ¹I⊕ι
Try it online! Link is to verbose version of code. Prints the first n terms. Explanation:
θ Input `n`
E Map over implicit range
η Input list of pairs
⭆ Map over pairs and join
ι Outer index
⊕ Incremented
﹪ Modulo
§λ⁰ First element of pair
⎇ If non-zero then
ω Empty string
§λ¹ Else second element of pair
∨ Logical Or
ι Current index
⊕ Incremented
I Cast to string
Implicitly print on separate lines
Note: Using trunk Charcoal (not available on TIO) this would be only 15 bytes:
Eθ∨⭆η⎇﹪⊕ιμωλI⊕ι
Explanation: Takes the input as a dict, which means that iterating over it populates the loop variables directly without having to index into the pairs.
Japt -R, 14 bytes
õ@VËÌpXvDÎìªX
õ@VËÌpXvDÎìªX :Implicit input of integer U & array V
õ :Range [1,U]
@ :Map each X
VË : Map each D in V
Ì : Last element
p : Repeat
Xv : Is X divisible by
DÎ : First element of D
à : End map
¬ : Join
ªX : Logical OR with X
:Implicit output joined with newlines
JavaScript (V8), 59 bytes
Prints the sequence forever.
a=>{for(k=0;++k;)print(a.map(([m,s])=>k%m?'':s).join``||k)}
Vyxal, 8 bytes
ƛ¹Ḋ⁰*∑∴,
Since y'all complaining about flags, I'll give you a tie with jelly.
Expects Limit, Divisors, Strings
Explained
ƛ¹Ḋ⁰*∑∴,
ƛ # for each n in the range [1, L]:
¹Ḋ # Push n % D == 0 (epicly vectorised)
⁰* # Push ^ * S (which is vectorised python string multiplication)
∑∴ # Push max(sum(^), n)
, # and print ^ with a newline
This would be 7 with the j flag