General FizzBuzz: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 57: Line 57:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F genfizzbuzz(factorwords, numbers)
<syntaxhighlight lang="11l">F genfizzbuzz(factorwords, numbers)
V sfactorwords = sorted(factorwords, key' p -> p[0])
V sfactorwords = sorted(factorwords, key' p -> p[0])
[String] lines
[String] lines
Line 65: Line 65:
R lines.join("\n")
R lines.join("\n")


print(genfizzbuzz([(5, ‘Buzz’), (3, ‘Fizz’), (7, ‘Baxx’)], 1..20))</lang>
print(genfizzbuzz([(5, ‘Buzz’), (3, ‘Fizz’), (7, ‘Baxx’)], 1..20))</syntaxhighlight>


{{out}}
{{out}}
Line 92: Line 92:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>DEFINE MAX_NUMBERS="200"
<syntaxhighlight lang="action!">DEFINE MAX_NUMBERS="200"
DEFINE MAX_LEN="20"
DEFINE MAX_LEN="20"
DEFINE MAX_FACTORS="5"
DEFINE MAX_FACTORS="5"
Line 184: Line 184:
PutE()
PutE()
PrintResult(max,n,factors,texts)
PrintResult(max,n,factors,texts)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/General_FizzBuzz.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/General_FizzBuzz.png Screenshot from Atari 8-bit computer]
Line 202: Line 202:


=={{header|Ada}}==
=={{header|Ada}}==
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 253: Line 253:
general_fizz_buzz (20, fizzy);
general_fizz_buzz (20, fizzy);
end Main;
end Main;
</syntaxhighlight>
</lang>
{{output}}
{{output}}
<pre>
<pre>
Line 280: Line 280:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Uses a modified version of the Algol 68 Quicksort task sample.
Uses a modified version of the Algol 68 Quicksort task sample.
<lang algol68>BEGIN # generalised FizzBuzz #
<syntaxhighlight lang="algol68">BEGIN # generalised FizzBuzz #
# prompts for an integer, reads it and returns it #
# prompts for an integer, reads it and returns it #
PROC read integer = ( STRING prompt )INT:
PROC read integer = ( STRING prompt )INT:
Line 356: Line 356:
print( ( text, newline ) )
print( ( text, newline ) )
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 390: Line 390:
=={{header|AppleScript}}==
=={{header|AppleScript}}==


<lang AppleScript>--------------------- GENERAL FIZZBUZZ -------------------
<syntaxhighlight lang="applescript">--------------------- GENERAL FIZZBUZZ -------------------


-- fizzEtc :: [(Int, String)] -> [Symbol]
-- fizzEtc :: [(Int, String)] -> [Symbol]
Line 516: Line 516:
set my text item delimiters to dlm
set my text item delimiters to dlm
s
s
end unlines</lang>
end unlines</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1
<pre>1
Line 541: Line 541:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>maxNum: to :integer strip input "Set maximum number: "
<syntaxhighlight lang="rebol">maxNum: to :integer strip input "Set maximum number: "
facts: map 1..3 'x -> split.words strip input ~"Enter factor |x|: "
facts: map 1..3 'x -> split.words strip input ~"Enter factor |x|: "
loop 1..maxNum 'i [
loop 1..maxNum 'i [
Line 551: Line 551:
]
]
print (printNum)? -> i -> ""
print (printNum)? -> i -> ""
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 581: Line 581:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>; Test parameters
<syntaxhighlight lang="autohotkey">; Test parameters
max := 20, fizz := 3, buzz := 5, baxx := 7
max := 20, fizz := 3, buzz := 5, baxx := 7


Line 597: Line 597:
FileAppend %output%`n, *
FileAppend %output%`n, *
}
}
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 637: Line 637:


;Usage:
;Usage:
<lang bash>awk -f fizzbuzzGenerate.awk input.txt > fizzbuzzCustom.awk
<syntaxhighlight lang="bash">awk -f fizzbuzzGenerate.awk input.txt > fizzbuzzCustom.awk
awk -f fizzbuzzCustom.awk numbers.txt</lang>
awk -f fizzbuzzCustom.awk numbers.txt</syntaxhighlight>


;Program:
;Program:
<!-- http://ideone.com/fACMfK -->
<!-- http://ideone.com/fACMfK -->
<!-- (!!) Note: the sandbox at ideone.com does not allow output to files -->
<!-- (!!) Note: the sandbox at ideone.com does not allow output to files -->
<lang awk># usage: awk -f fizzbuzzGen.awk > fizzbuzzCustom.awk
<syntaxhighlight lang="awk"># usage: awk -f fizzbuzzGen.awk > fizzbuzzCustom.awk
#
#
function Print(s) {
function Print(s) {
Line 675: Line 675:
print "END {print " q2 "# Done." q2 "}"
print "END {print " q2 "# Done." q2 "}"
Print( "# Done." )
Print( "# Done." )
}</lang>
}</syntaxhighlight>


Example output see [[FizzBuzz/AWK#Custom_FizzBuzz]]
Example output see [[FizzBuzz/AWK#Custom_FizzBuzz]]


=={{header|Batch File}}==
=={{header|Batch File}}==
<lang dos>@echo off
<syntaxhighlight lang="dos">@echo off
rem input range
rem input range
set /p "range=> "
set /p "range=> "
Line 710: Line 710:
)
)
pause
pause
exit /b 0</lang>
exit /b 0</syntaxhighlight>
{{Out}}
{{Out}}
<pre>> 20
<pre>> 20
Line 741: Line 741:
=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
This implementation (unlike some of the ones given on this page...) fully obeys the specification, in that it prompts the user for the parameters at run time. It also allows users to specify as many factors as they want, rather than limiting them to three.
This implementation (unlike some of the ones given on this page...) fully obeys the specification, in that it prompts the user for the parameters at run time. It also allows users to specify as many factors as they want, rather than limiting them to three.
<lang bbcbasic>REM >genfizzb
<syntaxhighlight lang="bbcbasic">REM >genfizzb
INPUT "Maximum number: " max%
INPUT "Maximum number: " max%
INPUT "Number of factors: " n%
INPUT "Number of factors: " n%
Line 760: Line 760:
NEXT
NEXT
IF matched% THEN PRINT ELSE PRINT;i%
IF matched% THEN PRINT ELSE PRINT;i%
NEXT</lang>
NEXT</syntaxhighlight>
Output:
Output:
<pre>Maximum number: 20
<pre>Maximum number: 20
Line 789: Line 789:


=={{header|BQN}}==
=={{header|BQN}}==
<lang BQN>GenFizzBuzz ← {factors‿words ← <˘⍉>𝕨 ⋄ (∾´∾⟜words/˜·(¬∨´)⊸∾0=factors|⊢)¨1+↕𝕩}</lang>
<syntaxhighlight lang="bqn">GenFizzBuzz ← {factors‿words ← <˘⍉>𝕨 ⋄ (∾´∾⟜words/˜·(¬∨´)⊸∾0=factors|⊢)¨1+↕𝕩}</syntaxhighlight>
Example usage:
Example usage:
<lang> ⟨3‿"Fizz", 5‿"Buzz", 7‿"Baxx"⟩ GenFizzBuzz 20
<syntaxhighlight lang="text"> ⟨3‿"Fizz", 5‿"Buzz", 7‿"Baxx"⟩ GenFizzBuzz 20
⟨ 1 2 "Fizz" 4 "Buzz" "Fizz" "Baxx" 8 "Fizz" "Buzz" 11 "Fizz" 13 "Baxx" "FizzBuzz" 16 17 "Fizz" 19 "Buzz" ⟩</lang>
⟨ 1 2 "Fizz" 4 "Buzz" "Fizz" "Baxx" 8 "Fizz" "Buzz" 11 "Fizz" 13 "Baxx" "FizzBuzz" 16 17 "Fizz" 19 "Buzz" ⟩</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 849: Line 849:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 876: Line 876:
=={{header|C sharp}}==
=={{header|C sharp}}==
Not extremely clever and doesn't use anything too fancy.
Not extremely clever and doesn't use anything too fancy.
<lang csharp>
<syntaxhighlight lang="csharp">
using System;
using System;


Line 937: Line 937:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<syntaxhighlight lang="cpp">
#include <algorithm>
#include <algorithm>
#include <iostream>
#include <iostream>
Line 978: Line 978:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,004: Line 1,004:


=={{header|Caché ObjectScript}}==
=={{header|Caché ObjectScript}}==
<lang Caché ObjectScript>GENFIZBUZZ(MAX,WORDS,NUMBERS)
<syntaxhighlight lang="caché objectscript">GENFIZBUZZ(MAX,WORDS,NUMBERS)
; loop until max, casting numeric to avoid errors
; loop until max, casting numeric to avoid errors
for i=1:1:+MAX {
for i=1:1:+MAX {
Line 1,025: Line 1,025:
} ; next value until MAX
} ; next value until MAX
quit</lang>
quit</syntaxhighlight>




Line 1,032: Line 1,032:


=={{header|Ceylon}}==
=={{header|Ceylon}}==
<lang ceylon>shared void run() {
<syntaxhighlight lang="ceylon">shared void run() {
print("enter the max value");
print("enter the max value");
Line 1,071: Line 1,071:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang Clojure>(defn fix [pairs]
<syntaxhighlight lang="clojure">(defn fix [pairs]
(map second pairs))
(map second pairs))


Line 1,088: Line 1,088:
(apply str
(apply str
(fix f)))))
(fix f)))))
numbers)))</lang>
numbers)))</syntaxhighlight>


'''Usage:'''
'''Usage:'''
Line 1,119: Line 1,119:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
(defun fizzbuzz (limit factor-words)
(defun fizzbuzz (limit factor-words)
(loop for i from 1 to limit
(loop for i from 1 to limit
Line 1,147: Line 1,147:
(not (zerop (parse-integer input :junk-allowed t))))
(not (zerop (parse-integer input :junk-allowed t))))
finally (fizzbuzz (parse-integer input :junk-allowed t) (read-factors))))
finally (fizzbuzz (parse-integer input :junk-allowed t) (read-factors))))
</syntaxhighlight>
</lang>


=={{header|Crystal}}==
=={{header|Crystal}}==
<syntaxhighlight lang="crystal">
<lang Crystal>
counter = 0
counter = 0


Line 1,201: Line 1,201:
exit
exit
end
end
</syntaxhighlight>
</lang>


=={{header|D}}==
=={{header|D}}==
<lang D>import core.stdc.stdlib;
<syntaxhighlight lang="d">import core.stdc.stdlib;
import std.stdio;
import std.stdio;


Line 1,252: Line 1,252:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,283: Line 1,283:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang elixir>defmodule General do
<syntaxhighlight lang="elixir">defmodule General do
def fizzbuzz(input) do
def fizzbuzz(input) do
[num | nwords] = String.split(input)
[num | nwords] = String.split(input)
Line 1,301: Line 1,301:
7 Baxx
7 Baxx
"""
"""
General.fizzbuzz(input)</lang>
General.fizzbuzz(input)</syntaxhighlight>


{{out}}
{{out}}
Line 1,336: Line 1,336:


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>
<syntaxhighlight lang="erlang">
%%% @doc Implementation of General FizzBuzz
%%% @doc Implementation of General FizzBuzz
%%% @see https://rosettacode.org/wiki/General_FizzBuzz
%%% @see https://rosettacode.org/wiki/General_FizzBuzz
Line 1,362: Line 1,362:
[fizzbuzz(X, Factors, []) || X <- lists:seq(1, N)]
[fizzbuzz(X, Factors, []) || X <- lists:seq(1, N)]
).
).
</syntaxhighlight>
</lang>
'''Usage:'''
'''Usage:'''
<pre>
<pre>
Line 1,396: Line 1,396:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: assocs combinators.extras io kernel math math.parser
<syntaxhighlight lang="factor">USING: assocs combinators.extras io kernel math math.parser
math.ranges prettyprint sequences splitting ;
math.ranges prettyprint sequences splitting ;
IN: rosetta-code.general-fizzbuzz
IN: rosetta-code.general-fizzbuzz
Line 1,416: Line 1,416:
: main ( -- ) get-input [ fizzbuzz ] with each ;
: main ( -- ) get-input [ fizzbuzz ] with each ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,448: Line 1,448:
Uses GForth specific words ']]' and '[['.
Uses GForth specific words ']]' and '[['.
If your forth does not have them: Sequence ']] A B C .. [[' is equivalent with 'postpone A postpone B postpone C ..'
If your forth does not have them: Sequence ']] A B C .. [[' is equivalent with 'postpone A postpone B postpone C ..'
<lang Forth>\ gfb.fs - generalized fizz buzz
<syntaxhighlight lang="forth">\ gfb.fs - generalized fizz buzz
: times ( xt n -- )
: times ( xt n -- )
BEGIN dup WHILE
BEGIN dup WHILE
Line 1,473: Line 1,473:
\ Example:
\ Example:
\ 1 fb[ 3 s" fizz" ]+[ 5 s" buzz" ]+[ 7 s" dizz" ]+[ ]fb 40 times drop
\ 1 fb[ 3 s" fizz" ]+[ 5 s" buzz" ]+[ 7 s" dizz" ]+[ ]fb 40 times drop
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,484: Line 1,484:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
{{trans|BBC BASIC}}
<lang freebasic>' version 01-03-2018
<syntaxhighlight lang="freebasic">' version 01-03-2018
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,542: Line 1,542:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre>Enter maximum number, if number < 1 then the program wil end 110
<pre>Enter maximum number, if number < 1 then the program wil end 110
Line 1,572: Line 1,572:


=={{header|Go}}==
=={{header|Go}}==
<syntaxhighlight lang="go">
<lang Go>
package main
package main


Line 1,606: Line 1,606:
}
}


}</lang>
}</syntaxhighlight>


=={{header|Groovy}}==
=={{header|Groovy}}==
<lang Groovy>def log = ''
<syntaxhighlight lang="groovy">def log = ''
(1..40).each {Integer value -> log +=(value %3 == 0) ? (value %5 == 0)? 'FIZZBUZZ\n':(value %7 == 0)? 'FIZZBAXX\n':'FIZZ\n'
(1..40).each {Integer value -> log +=(value %3 == 0) ? (value %5 == 0)? 'FIZZBUZZ\n':(value %7 == 0)? 'FIZZBAXX\n':'FIZZ\n'
:(value %5 == 0) ? (value %7 == 0)? 'BUZBAXX\n':'BUZZ\n'
:(value %5 == 0) ? (value %7 == 0)? 'BUZBAXX\n':'BUZZ\n'
Line 1,615: Line 1,615:
:(value+'\n')}
:(value+'\n')}
println log
println log
</syntaxhighlight>
</lang>
<pre>
<pre>
1
1
Line 1,661: Line 1,661:
=={{header|Haskell}}==
=={{header|Haskell}}==


<lang haskell>fizz :: (Integral a, Show a) => a -> [(a, String)] -> String
<syntaxhighlight lang="haskell">fizz :: (Integral a, Show a) => a -> [(a, String)] -> String
fizz a xs
fizz a xs
| null result = show a
| null result = show a
Line 1,677: Line 1,677:
mapM_ (\ x -> putStrLn $ fizz x multiples) [1..n]
mapM_ (\ x -> putStrLn $ fizz x multiples) [1..n]
where convert [x, y] = (read x, y)
where convert [x, y] = (read x, y)
</syntaxhighlight>
</lang>


Or, as a function which takes a list of rules as an argument:
Or, as a function which takes a list of rules as an argument:


<lang haskell>type Rule = (Int, String)
<syntaxhighlight lang="haskell">type Rule = (Int, String)


----------------- FIZZETC (USING RULE SET) ---------------
----------------- FIZZETC (USING RULE SET) ---------------
Line 1,704: Line 1,704:
main :: IO ()
main :: IO ()
main = mapM_ putStrLn $ take 20 fizzTest
main = mapM_ putStrLn $ take 20 fizzTest
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>1
<pre>1
Line 1,731: Line 1,731:
The trick here involves looking for where the factors evenly divide the counting numbers. Where no factor is relevant we use the counting number, an in the remaining cases we use the string which corresponds to the factor:
The trick here involves looking for where the factors evenly divide the counting numbers. Where no factor is relevant we use the counting number, an in the remaining cases we use the string which corresponds to the factor:


<lang J>genfb=:1 :0
<syntaxhighlight lang="j">genfb=:1 :0
:
:
b=. * x|/1+i.y
b=. * x|/1+i.y
>,&":&.>/(m#inv"_1~-.b),(*/b)#&.>1+i.y
>,&":&.>/(m#inv"_1~-.b),(*/b)#&.>1+i.y
)</lang>
)</syntaxhighlight>


Example use:
Example use:


<lang J> 3 5 7 ('Fizz';'Buzz';'Baxx')genfb 20
<syntaxhighlight lang="j"> 3 5 7 ('Fizz';'Buzz';'Baxx')genfb 20
1
1
2
2
Line 1,759: Line 1,759:
Fizz
Fizz
19
19
Buzz </lang>
Buzz </syntaxhighlight>


For our example, b looks like this:
For our example, b looks like this:


<lang J>1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1
<syntaxhighlight lang="j">1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1
1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0
1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1</lang>
1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1</syntaxhighlight>


<code>*/b</code> gives us 1s where we want numbers and 0s where we want to plug in the strings:
<code>*/b</code> gives us 1s where we want numbers and 0s where we want to plug in the strings:


<lang J> */*3 5 7|/1+i.20
<syntaxhighlight lang="j"> */*3 5 7|/1+i.20
1 1 0 1 0 0 0 1 0 0 1 0 1 0 0 1 1 0 1 0</lang>
1 1 0 1 0 0 0 1 0 0 1 0 1 0 0 1 1 0 1 0</syntaxhighlight>


<code>m</code> is our strings, and #inv expands values out to match a selection. So, in our example, <code>m#inv"_1~-.b</code> looks like this:
<code>m</code> is our strings, and #inv expands values out to match a selection. So, in our example, <code>m#inv"_1~-.b</code> looks like this:


<lang J>┌┬┬────┬┬────┬────┬────┬┬────┬────┬┬────┬┬────┬────┬┬┬────┬┬────┐
<syntaxhighlight lang="j">┌┬┬────┬┬────┬────┬────┬┬────┬────┬┬────┬┬────┬────┬┬┬────┬┬────┐
│││Fizz││ │Fizz│ ││Fizz│ ││Fizz││ │Fizz│││Fizz││ │
│││Fizz││ │Fizz│ ││Fizz│ ││Fizz││ │Fizz│││Fizz││ │
├┼┼────┼┼────┼────┼────┼┼────┼────┼┼────┼┼────┼────┼┼┼────┼┼────┤
├┼┼────┼┼────┼────┼────┼┼────┼────┼┼────┼┼────┼────┼┼┼────┼┼────┤
Line 1,780: Line 1,780:
├┼┼────┼┼────┼────┼────┼┼────┼────┼┼────┼┼────┼────┼┼┼────┼┼────┤
├┼┼────┼┼────┼────┼────┼┼────┼────┼┼────┼┼────┼────┼┼┼────┼┼────┤
│││ ││ │ │Baxx││ │ ││ ││Baxx│ │││ ││ │
│││ ││ │ │Baxx││ │ ││ ││Baxx│ │││ ││ │
└┴┴────┴┴────┴────┴────┴┴────┴────┴┴────┴┴────┴────┴┴┴────┴┴────┘</lang>
└┴┴────┴┴────┴────┴────┴┴────┴────┴┴────┴┴────┴────┴┴┴────┴┴────┘</syntaxhighlight>


All that remains is to assemble these pieces into the final result...
All that remains is to assemble these pieces into the final result...


=={{header|Java}}==
=={{header|Java}}==
<lang java>public class FizzBuzz {
<syntaxhighlight lang="java">public class FizzBuzz {


public static void main(String[] args) {
public static void main(String[] args) {
Line 1,813: Line 1,813:
}
}


}</lang>
}</syntaxhighlight>




Line 1,826: Line 1,826:


First as compacted by Google's Closure compiler:
First as compacted by Google's Closure compiler:
<lang JavaScript>function fizz(d, e) {
<syntaxhighlight lang="javascript">function fizz(d, e) {
return function b(a) {
return function b(a) {
return a ? b(a - 1).concat(a) : [];
return a ? b(a - 1).concat(a) : [];
Line 1,834: Line 1,834:
}, "") || a.toString()) + "\n";
}, "") || a.toString()) + "\n";
}, "");
}, "");
}</lang>
}</syntaxhighlight>


and then in the original expanded form, for better legibility:
and then in the original expanded form, for better legibility:


<lang JavaScript>function fizz(lstRules, lngMax) {
<syntaxhighlight lang="javascript">function fizz(lstRules, lngMax) {


return (
return (
Line 1,863: Line 1,863:
}
}


fizz([[3, 'Fizz'], [5, 'Buzz'], [7, 'Baxx']], 20);</lang>
fizz([[3, 'Fizz'], [5, 'Buzz'], [7, 'Baxx']], 20);</syntaxhighlight>


{{out}}
{{out}}
Line 1,890: Line 1,890:
===ES6===
===ES6===


<lang JavaScript>// range :: Int -> Int -> [Int]
<syntaxhighlight lang="javascript">// range :: Int -> Int -> [Int]
const range = (min, max) =>
const range = (min, max) =>
Array.from({ length: max - min }, (_, i) => min + i)
Array.from({ length: max - min }, (_, i) => min + i)
Line 1,908: Line 1,908:
).join('\n')
).join('\n')


console.log(fizzBuzz(20))</lang>
console.log(fizzBuzz(20))</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,936: Line 1,936:
=={{header|Julia}}==
=={{header|Julia}}==
For simplicity, assume that the user will enter valid input.
For simplicity, assume that the user will enter valid input.
<lang Julia>function fizzbuzz(triggers :: Vector{Tuple{Int, ASCIIString}}, upper :: Int)
<syntaxhighlight lang="julia">function fizzbuzz(triggers :: Vector{Tuple{Int, ASCIIString}}, upper :: Int)
for i = 1 : upper
for i = 1 : upper
triggered = false
triggered = false
Line 1,964: Line 1,964:


println("EOF\n")
println("EOF\n")
fizzbuzz(triggers, upper)</lang>
fizzbuzz(triggers, upper)</syntaxhighlight>


{{out}}
{{out}}
Line 1,998: Line 1,998:
=={{header|Kotlin}}==
=={{header|Kotlin}}==


<lang Kotlin>fun main(args: Array<String>) {
<syntaxhighlight lang="kotlin">fun main(args: Array<String>) {


//Read the maximum number, set to 0 if it couldn't be read
//Read the maximum number, set to 0 if it couldn't be read
Line 2,025: Line 2,025:
println(i)
println(i)
}
}
}</lang>
}</syntaxhighlight>


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>function generalisedFizzBuzz m, f1, f2, f3
<syntaxhighlight lang="livecode">function generalisedFizzBuzz m, f1, f2, f3
put f1 & cr & f2 & cr & f3 into factors
put f1 & cr & f2 & cr & f3 into factors
sort factors ascending numeric
sort factors ascending numeric
Line 2,049: Line 2,049:
end repeat
end repeat
return fizzbuzz
return fizzbuzz
end generalisedFizzBuzz</lang>Example<lang LiveCode>put generalisedFizzBuzz(20,"7 baxx","3 fizz","5 buzz")</lang>
end generalisedFizzBuzz</syntaxhighlight>Example<syntaxhighlight lang="livecode">put generalisedFizzBuzz(20,"7 baxx","3 fizz","5 buzz")</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
<lang Lua>function genFizz (param)
<syntaxhighlight lang="lua">function genFizz (param)
local response
local response
print("\n")
print("\n")
Line 2,071: Line 2,071:
param.factor[i], param.word[i] = io.read("*number", "*line")
param.factor[i], param.word[i] = io.read("*number", "*line")
end
end
genFizz(param)</lang>
genFizz(param)</syntaxhighlight>


=== Without modulo ===
=== Without modulo ===
{{trans|Python}}
{{trans|Python}}
<lang Lua>local function fizzbuzz(n, mods)
<syntaxhighlight lang="lua">local function fizzbuzz(n, mods)
local res = {}
local res = {}


Line 2,110: Line 2,110:
print(fizzbuzz(n, mods))
print(fizzbuzz(n, mods))
end
end
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,130: Line 2,130:


===Fast Version without Modulo===
===Fast Version without Modulo===
<lang lua>#!/usr/bin/env luajit
<syntaxhighlight lang="lua">#!/usr/bin/env luajit


local num = arg[1] or tonumber(arg[1]) or 110
local num = arg[1] or tonumber(arg[1]) or 110
Line 2,160: Line 2,160:
io.write(", ")
io.write(", ")
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,168: Line 2,168:


=={{header|Maple}}==
=={{header|Maple}}==
<lang Maple>findNum := proc(str) #help parse input
<syntaxhighlight lang="maple">findNum := proc(str) #help parse input
local i;
local i;
i := 1:
i := 1:
Line 2,199: Line 2,199:
if (not factored) then printf("%d", i): end if:
if (not factored) then printf("%d", i): end if:
printf("\n");
printf("\n");
end do:</lang>
end do:</syntaxhighlight>
{{Out|Output}}
{{Out|Output}}
<pre>1
<pre>1
Line 2,223: Line 2,223:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>list={{5,"Buzz"},{3,"Fizz"},{7,"Baxx"}};
<syntaxhighlight lang="mathematica">list={{5,"Buzz"},{3,"Fizz"},{7,"Baxx"}};
runTo=(*LCM@@list[[All,1]]+1*)20;
runTo=(*LCM@@list[[All,1]]+1*)20;
Column@Table[
Column@Table[
Select[list,Mod[x,#[[1]]]==0&][[All,2]]/.{}->{x}
Select[list,Mod[x,#[[1]]]==0&][[All,2]]/.{}->{x}
,{x,1,runTo}
,{x,1,runTo}
]</lang>
]</syntaxhighlight>


<pre>1
<pre>1
Line 2,255: Line 2,255:
This implementation improves slightly over the proposed task, making the prompts a little friendlier.
This implementation improves slightly over the proposed task, making the prompts a little friendlier.


<lang MiniScript>factorWords = {}
<syntaxhighlight lang="miniscript">factorWords = {}


maxNr = val(input("Max number? "))
maxNr = val(input("Max number? "))
Line 2,278: Line 2,278:
end for
end for
if matchingWords then print matchingWords else print nr
if matchingWords then print matchingWords else print nr
end for</lang>
end for</syntaxhighlight>


Sample session:
Sample session:
Line 2,308: Line 2,308:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE GeneralFizzBuzz;
<syntaxhighlight lang="modula2">MODULE GeneralFizzBuzz;
FROM Conversions IMPORT StrToInt;
FROM Conversions IMPORT StrToInt;
FROM FormatString IMPORT FormatString;
FROM FormatString IMPORT FormatString;
Line 2,410: Line 2,410:


ReadChar
ReadChar
END GeneralFizzBuzz.</lang>
END GeneralFizzBuzz.</syntaxhighlight>


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
{{trans|Ursa}}
{{trans|Ursa}}
<lang Nanoquery>factors = {}
<syntaxhighlight lang="nanoquery">factors = {}
words = {}
words = {}


Line 2,447: Line 2,447:
end
end
println
println
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 2,478: Line 2,478:
=={{header|Nim}}==
=={{header|Nim}}==
This solution has no input validation
This solution has no input validation
<lang nim>
<syntaxhighlight lang="nim">
import parseutils, strutils, algorithm
import parseutils, strutils, algorithm


Line 2,524: Line 2,524:




</syntaxhighlight>
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
<syntaxhighlight lang="ocaml">
<lang OCaml>
(* Task : General_FizzBuzz *)
(* Task : General_FizzBuzz *)


Line 2,561: Line 2,561:


gen_fizz_buzz 20 [(3, "Fizz"); (5, "Buzz"); (7, "Baxx")] ;;
gen_fizz_buzz 20 [(3, "Fizz"); (5, "Buzz"); (7, "Baxx")] ;;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,589: Line 2,589:
{{works with|PARI/GP|2.8.0+}}
{{works with|PARI/GP|2.8.0+}}
This version uses a variadic argument to allow more or less than 3 factors. It could be easily modified for earlier versions, either by taking a vector rather than bare arguments (making the call <code>fizz(20,[[3,"Fizz"],[5,"Buzz"],[7,"Baxx"]])</code>) or to support exactly factors (keeping the call the same).
This version uses a variadic argument to allow more or less than 3 factors. It could be easily modified for earlier versions, either by taking a vector rather than bare arguments (making the call <code>fizz(20,[[3,"Fizz"],[5,"Buzz"],[7,"Baxx"]])</code>) or to support exactly factors (keeping the call the same).
<lang parigp>fizz(n,v[..])=
<syntaxhighlight lang="parigp">fizz(n,v[..])=
{
{
v=vecsort(v,1);
v=vecsort(v,1);
Line 2,603: Line 2,603:
);
);
}
}
fizz(20,[3,"Fizz"],[5,"Buzz"],[7,"Baxx"])</lang>
fizz(20,[3,"Fizz"],[5,"Buzz"],[7,"Baxx"])</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 2,627: Line 2,627:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>
<syntaxhighlight lang="perl">
#!bin/usr/perl
#!bin/usr/perl
use 5.020;
use 5.020;
Line 2,679: Line 2,679:
}
}
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,714: Line 2,714:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">procedure</span> <span style="color: #000000;">general_fizz_buzz</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">facts</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">general_fizz_buzz</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">facts</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
Line 2,730: Line 2,730:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">general_fizz_buzz</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Fizz"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Buzz"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Baxx"</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">general_fizz_buzz</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Fizz"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Buzz"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Baxx"</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,756: Line 2,756:


=={{header|PHP}}==
=={{header|PHP}}==
<lang PHP><?php
<syntaxhighlight lang="php"><?php


$max = 20;
$max = 20;
Line 2,772: Line 2,772:
}
}


?></lang>
?></syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 2,797: Line 2,797:


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>interactive =>
<syntaxhighlight lang="picat">interactive =>
print("> "),
print("> "),
MaxNum = read_int(),
MaxNum = read_int(),
Line 2,818: Line 2,818:
end
end
end,
end,
println([F : F in FB].join(" ")).</lang>
println([F : F in FB].join(" ")).</syntaxhighlight>


Testing:
Testing:
Line 2,866: Line 2,866:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de general (N Lst)
<syntaxhighlight lang="picolisp">(de general (N Lst)
(for A N
(for A N
(prinl
(prinl
Line 2,876: Line 2,876:
A ) ) ) )
A ) ) ) )


(general 20 '((3 . Fizz) (5 . Buzz) (7 . Baxx)))</lang>
(general 20 '((3 . Fizz) (5 . Buzz) (7 . Baxx)))</syntaxhighlight>


{{out}}
{{out}}
Line 2,901: Line 2,901:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>$limit = 20
<syntaxhighlight lang="powershell">$limit = 20
$data = @("3 Fizz","5 Buzz","7 Baxx")
$data = @("3 Fizz","5 Buzz","7 Baxx")
#An array with whitespace as the delimiter
#An array with whitespace as the delimiter
Line 2,919: Line 2,919:
Write-HoSt $outP
Write-HoSt $outP
}
}
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>PS> ./GENFB
<pre>PS> ./GENFB
Line 2,946: Line 2,946:
=={{header|Prolog}}==
=={{header|Prolog}}==
Assuming the user specifies as input two facts of the form:
Assuming the user specifies as input two facts of the form:
<lang prolog>maxNumber(105).
<syntaxhighlight lang="prolog">maxNumber(105).
factors([(3, "Fizz"), (5, "Buzz"), (7, "Baxx")]).</lang>
factors([(3, "Fizz"), (5, "Buzz"), (7, "Baxx")]).</syntaxhighlight>


A simple Prolog solution to the generalised FizzBuzz problem is as follows:
A simple Prolog solution to the generalised FizzBuzz problem is as follows:


<lang prolog>go :- maxNumber(M), factors(Fs), MLast is M+1, loop(1,MLast,Fs).
<syntaxhighlight lang="prolog">go :- maxNumber(M), factors(Fs), MLast is M+1, loop(1,MLast,Fs).


loop(B,B,_).
loop(B,B,_).
Line 2,961: Line 2,961:
fizzbuzz(N,[(F,S)|Fs],Res) :-
fizzbuzz(N,[(F,S)|Fs],Res) :-
fizzbuzz(N,Fs,OldRes),
fizzbuzz(N,Fs,OldRes),
( N mod F =:= 0, string_concat(S,OldRes,Res) ; Res = OldRes ).</lang>
( N mod F =:= 0, string_concat(S,OldRes,Res) ; Res = OldRes ).</syntaxhighlight>


The program can be launched by querying the predicate
The program can be launched by querying the predicate
<lang prolog>?- go.</lang>
<syntaxhighlight lang="prolog">?- go.</syntaxhighlight>


It is worth noting that
It is worth noting that
<lang prolog>factors([(3, "Fizz"), (5, "Buzz")]).</lang>
<syntaxhighlight lang="prolog">factors([(3, "Fizz"), (5, "Buzz")]).</syntaxhighlight>
corresponds to basic FizzBuzz and that the proposed solution can handle an arbitrary number of factors.
corresponds to basic FizzBuzz and that the proposed solution can handle an arbitrary number of factors.


Line 2,974: Line 2,974:
===Elegant naive version===
===Elegant naive version===


<lang python>def genfizzbuzz(factorwords, numbers):
<syntaxhighlight lang="python">def genfizzbuzz(factorwords, numbers):
# sort entries by factor
# sort entries by factor
factorwords.sort(key=lambda factor_and_word: factor_and_word[0])
factorwords.sort(key=lambda factor_and_word: factor_and_word[0])
Line 2,984: Line 2,984:


if __name__ == '__main__':
if __name__ == '__main__':
print(genfizzbuzz([(5, 'Buzz'), (3, 'Fizz'), (7, 'Baxx')], range(1, 21)))</lang>
print(genfizzbuzz([(5, 'Buzz'), (3, 'Fizz'), (7, 'Baxx')], range(1, 21)))</syntaxhighlight>


{{out}}
{{out}}
Line 3,009: Line 3,009:


===One-liner using generator expressions===
===One-liner using generator expressions===
<lang python>n = 20
<syntaxhighlight lang="python">n = 20
mappings = {3: "Fizz", 5: "Buzz", 7: "Baxx"}
mappings = {3: "Fizz", 5: "Buzz", 7: "Baxx"}
for i in range(1, n+1): print(''.join(word * (i % key == 0) for key, word in mappings.items()) or i)</lang>
for i in range(1, n+1): print(''.join(word * (i % key == 0) for key, word in mappings.items()) or i)</syntaxhighlight>


===Generator using counters instead of modulo===
===Generator using counters instead of modulo===
{{works with|Python|3.x}}
{{works with|Python|3.x}}
<lang Python>from collections import defaultdict
<syntaxhighlight lang="python">from collections import defaultdict


N = 100
N = 100
Line 3,051: Line 3,051:
for line in fizzbuzz(n, mods):
for line in fizzbuzz(n, mods):
print(line)
print(line)
</syntaxhighlight>
</lang>


===Sieve of Eratosthenes===
===Sieve of Eratosthenes===
'''This variant uses ranges with step 3, 5, etc. Preserves order and duplicate moduli.'''
'''This variant uses ranges with step 3, 5, etc. Preserves order and duplicate moduli.'''
<lang Python>from collections import defaultdict
<syntaxhighlight lang="python">from collections import defaultdict


n = 100
n = 100
Line 3,087: Line 3,087:


print(fizzbuzz(n, mods))
print(fizzbuzz(n, mods))
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 3,119: Line 3,119:
===... solution===
===... solution===
The task asks that we assume 3 factors for the sake of simplicity. However, R makes the k factors case not much more complicated, so we will do that. The only major downside is that checking for malformed user input becomes so difficult that we will not bother.
The task asks that we assume 3 factors for the sake of simplicity. However, R makes the k factors case not much more complicated, so we will do that. The only major downside is that checking for malformed user input becomes so difficult that we will not bother.
<lang rsplus>genFizzBuzz <- function(n, ...)
<syntaxhighlight lang="rsplus">genFizzBuzz <- function(n, ...)
{
{
args <- list(...)
args <- list(...)
Line 3,136: Line 3,136:
}
}
genFizzBuzz(105, c(3, "Fizz"), c(5, "Buzz"), c(7, "Baxx"))
genFizzBuzz(105, c(3, "Fizz"), c(5, "Buzz"), c(7, "Baxx"))
genFizzBuzz(105, c(5, "Buzz"), c(9, "Prax"), c(3, "Fizz"), c(7, "Baxx"))</lang>
genFizzBuzz(105, c(5, "Buzz"), c(9, "Prax"), c(3, "Fizz"), c(7, "Baxx"))</syntaxhighlight>


===Names solution===
===Names solution===
If we deviate from the task's example of how to input parameters and instead use R's names facilities to make our (number, name) pairs, we get a much cleaner solution.
If we deviate from the task's example of how to input parameters and instead use R's names facilities to make our (number, name) pairs, we get a much cleaner solution.
<lang rsplus>namedGenFizzBuzz <- function(n, namedNums)
<syntaxhighlight lang="rsplus">namedGenFizzBuzz <- function(n, namedNums)
{
{
factors <- sort(namedNums)#Required by the task: We must go from least factor to greatest.
factors <- sort(namedNums)#Required by the task: We must go from least factor to greatest.
Line 3,152: Line 3,152:
namedGenFizzBuzz(105, namedNums)
namedGenFizzBuzz(105, namedNums)
shuffledNamedNums <- c(Buzz=5, Prax=9, Fizz=3, Baxx=7)
shuffledNamedNums <- c(Buzz=5, Prax=9, Fizz=3, Baxx=7)
namedGenFizzBuzz(105, shuffledNamedNums)</lang>
namedGenFizzBuzz(105, shuffledNamedNums)</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
{{trans|Python}}
{{trans|Python}}
<lang Racket>#lang racket/base
<syntaxhighlight lang="racket">#lang racket/base


(define (get-matches num factors/words)
(define (get-matches num factors/words)
Line 3,174: Line 3,174:
(gen-fizzbuzz 1 21 '((3 "Fizz")
(gen-fizzbuzz 1 21 '((3 "Fizz")
(5 "Buzz")
(5 "Buzz")
(7 "Baxx")))</lang>
(7 "Baxx")))</syntaxhighlight>
{{out}}
{{out}}
<pre>1
<pre>1
Line 3,198: Line 3,198:


===Alternate Solution===
===Alternate Solution===
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require racket/match)
(require racket/match)


Line 3,214: Line 3,214:
(void))
(void))


(fizz-buzz 20 '(3 . "Fizz") '(5 . "Buzz") '(7 . "Baxx"))</lang>
(fizz-buzz 20 '(3 . "Fizz") '(5 . "Buzz") '(7 . "Baxx"))</syntaxhighlight>


{{out}}
{{out}}
Line 3,241: Line 3,241:
(formerly Perl 6)
(formerly Perl 6)
{{works with|rakudo|2015-09-20}}
{{works with|rakudo|2015-09-20}}
<lang perl6># General case implementation of a "FizzBuzz" class.
<syntaxhighlight lang="raku" line># General case implementation of a "FizzBuzz" class.
# Defaults to standard FizzBuzz unless a new schema is passed in.
# Defaults to standard FizzBuzz unless a new schema is passed in.
class FizzBuzz {
class FizzBuzz {
Line 3,268: Line 3,268:
# And for fun
# And for fun
say 'Using: 21 ' ~ <2 Pip 4 Squack 5 Pocketa 7 Queep>;
say 'Using: 21 ' ~ <2 Pip 4 Squack 5 Pocketa 7 Queep>;
say join ', ', GeneralFizzBuzz(21, <2 Pip 4 Squack 5 Pocketa 7 Queep>);</lang>
say join ', ', GeneralFizzBuzz(21, <2 Pip 4 Squack 5 Pocketa 7 Queep>);</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Using: 20 3 Fizz 5 Buzz 7 Baxx
<pre>Using: 20 3 Fizz 5 Buzz 7 Baxx
Line 3,296: Line 3,296:


Here's the same program in a more functional idiom:
Here's the same program in a more functional idiom:
<lang perl6>sub genfizzbuzz($n, +@fb) {
<syntaxhighlight lang="raku" line>sub genfizzbuzz($n, +@fb) {
[Z~](
[Z~](
do for @fb || <3 fizz 5 buzz> -> $i, $s {
do for @fb || <3 fizz 5 buzz> -> $i, $s {
Line 3,304: Line 3,304:
}
}


.say for genfizzbuzz(20, <3 Fizz 5 Buzz 7 Baxx>);</lang>
.say for genfizzbuzz(20, <3 Fizz 5 Buzz 7 Baxx>);</syntaxhighlight>


=={{header|Red}}==
=={{header|Red}}==
<lang Red>Red ["FizzBuzz"]
<syntaxhighlight lang="red">Red ["FizzBuzz"]


nmax: to-integer ask "Max number: "
nmax: to-integer ask "Max number: "
Line 3,320: Line 3,320:
print either empty? res [n] [res]
print either empty? res [n] [res]
]
]
halt</lang>
halt</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Max number: 21
<pre>Max number: 21
Line 3,352: Line 3,352:
=={{header|REXX}}==
=={{header|REXX}}==
=== idiomatic version ===
=== idiomatic version ===
<lang rexx>/*REXX program shows a generalized FizzBuzz program: #1 name1 #2 name2 ··· */
<syntaxhighlight lang="rexx">/*REXX program shows a generalized FizzBuzz program: #1 name1 #2 name2 ··· */
parse arg h $ /*obtain optional arguments from the CL*/
parse arg h $ /*obtain optional arguments from the CL*/
if h='' | h="," then h= 20 /*Not specified? Then use the default.*/
if h='' | h="," then h= 20 /*Not specified? Then use the default.*/
Line 3,367: Line 3,367:
end /*k*/ /* [↑] Note: the factors may be zero.*/
end /*k*/ /* [↑] Note: the factors may be zero.*/
say word(z j, 1) /*display the number or its factors. */
say word(z j, 1) /*display the number or its factors. */
end /*j*/ /*stick a fork in it, we're all done. */</lang>
end /*j*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 3,393: Line 3,393:


=== optimized version ===
=== optimized version ===
<lang rexx>/*REXX program shows a generalized FizzBuzz program: #1 name1 #2 name2 ··· */
<syntaxhighlight lang="rexx">/*REXX program shows a generalized FizzBuzz program: #1 name1 #2 name2 ··· */
parse arg h $ /*obtain optional arguments from the CL*/
parse arg h $ /*obtain optional arguments from the CL*/
if h='' | h="," then h= 20 /*Not specified? Then use the default.*/
if h='' | h="," then h= 20 /*Not specified? Then use the default.*/
Line 3,403: Line 3,403:
end /*k*/ /* [↑] Note: the factors may be zero.*/
end /*k*/ /* [↑] Note: the factors may be zero.*/
say word(Z j, 1) /*display the number or its factors. */
say word(Z j, 1) /*display the number or its factors. */
end /*j*/ /*stick a fork in it, we're all done. */</lang>
end /*j*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
limit = 20
limit = 20
for n = 1 to limit
for n = 1 to limit
Line 3,416: Line 3,416:
next
next


</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def general_fizzbuzz(text)
<syntaxhighlight lang="ruby">def general_fizzbuzz(text)
num, *nword = text.split
num, *nword = text.split
num = num.to_i
num = num.to_i
Line 3,436: Line 3,436:
EOS
EOS


general_fizzbuzz(text)</lang>
general_fizzbuzz(text)</syntaxhighlight>


{{out}}
{{out}}
Line 3,464: Line 3,464:
=={{header|Rust}}==
=={{header|Rust}}==


<lang rust>use std::io;
<syntaxhighlight lang="rust">use std::io;
use std::io::BufRead;
use std::io::BufRead;


Line 3,502: Line 3,502:
}
}
}
}
}</lang>
}</syntaxhighlight>


This solution stores the Fizz Buzz state in a struct, leveraging types and the standard library for a more general solution:
This solution stores the Fizz Buzz state in a struct, leveraging types and the standard library for a more general solution:


<lang rust>use std::collections::BTreeMap;
<syntaxhighlight lang="rust">use std::collections::BTreeMap;
use std::fmt::{self, Write};
use std::fmt::{self, Write};
use std::io::{self, Stdin};
use std::io::{self, Stdin};
Line 3,630: Line 3,630:
assert_eq!(expected, &printed);
assert_eq!(expected, &printed);
}
}
}</lang>
}</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
Line 3,667: Line 3,667:
===Simple===
===Simple===


<lang Scala>import scala.io.{Source, StdIn}
<syntaxhighlight lang="scala">import scala.io.{Source, StdIn}


object GeneralFizzBuzz extends App {
object GeneralFizzBuzz extends App {
Line 3,680: Line 3,680:
println(if (words.nonEmpty) words.mkString else i)
println(if (words.nonEmpty) words.mkString else i)
}
}
}</lang>
}</syntaxhighlight>


===LazyList (f/k/a Stream)===
===LazyList (f/k/a Stream)===


<lang Scala>import scala.io.{Source, StdIn}
<syntaxhighlight lang="scala">import scala.io.{Source, StdIn}


object GeneralFizzBuzz extends App {
object GeneralFizzBuzz extends App {
Line 3,701: Line 3,701:
.sorted
.sorted
fizzBuzz(factors).take(max).foreach(println)
fizzBuzz(factors).take(max).foreach(println)
}</lang>
}</syntaxhighlight>


===Scala 3 (Dotty)===
===Scala 3 (Dotty)===


{{trans|LazyList (f/k/a Stream)}}
{{trans|LazyList (f/k/a Stream)}}
<lang Scala>import scala.io.{Source, StdIn}
<syntaxhighlight lang="scala">import scala.io.{Source, StdIn}


def fizzBuzzTerm(n: Int, factors: Seq[(Int, String)]): String | Int =
def fizzBuzzTerm(n: Int, factors: Seq[(Int, String)]): String | Int =
Line 3,721: Line 3,721:
.map { case Array(k, v) => k.toInt -> v }
.map { case Array(k, v) => k.toInt -> v }
.sorted
.sorted
fizzBuzz(factors).take(max).foreach(println)</lang>
fizzBuzz(factors).take(max).foreach(println)</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>class FizzBuzz(schema=Hash(<3 Fizz 5 Buzz>...)) {
<syntaxhighlight lang="ruby">class FizzBuzz(schema=Hash(<3 Fizz 5 Buzz>...)) {
method filter(this) {
method filter(this) {
var fb = ''
var fb = ''
Line 3,742: Line 3,742:
}
}
 
 
GeneralFizzBuzz(20, <3 Fizz 5 Buzz 7 Baxx>).each { .say }</lang>
GeneralFizzBuzz(20, <3 Fizz 5 Buzz 7 Baxx>).each { .say }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,771: Line 3,771:
This is not a particularly efficient solution, but it gets the job done.
This is not a particularly efficient solution, but it gets the job done.


<syntaxhighlight lang="sql">
<lang SQL>
/*
/*
This code is an implementation of "General FizzBuzz" in SQL ORACLE 19c
This code is an implementation of "General FizzBuzz" in SQL ORACLE 19c
Line 3,784: Line 3,784:
;
;
/
/
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,821: Line 3,821:
=={{header|Swift}}==
=={{header|Swift}}==


<lang>import Foundation
<syntaxhighlight lang="text">import Foundation


print("Input max number: ", terminator: "")
print("Input max number: ", terminator: "")
Line 3,858: Line 3,858:


print("\(factors.isEmpty ? String(i) : factors)")
print("\(factors.isEmpty ? String(i) : factors)")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,888: Line 3,888:


=={{header|Tailspin}}==
=={{header|Tailspin}}==
<lang tailspin>
<syntaxhighlight lang="tailspin">
def input: {N: 110"1", words: [ { mod: 3"1", word: 'Fizz' }, { mod: 5"1", word: 'Buzz'}, {mod:7"1", word: 'Baxx'}]};
def input: {N: 110"1", words: [ { mod: 3"1", word: 'Fizz' }, { mod: 5"1", word: 'Buzz'}, {mod:7"1", word: 'Baxx'}]};


Line 3,902: Line 3,902:
[ 1"1"..$input.N -> '$->sayWords;' ] -> \[i](<=''> $i ! <> $ !\) -> '$;
[ 1"1"..$input.N -> '$->sayWords;' ] -> \[i](<=''> $i ! <> $ !\) -> '$;
' -> !OUT::write
' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,913: Line 3,913:
For fun, the below implementation is a compatible extension of [[FizzBuzz#Tcl]]:
For fun, the below implementation is a compatible extension of [[FizzBuzz#Tcl]]:


<lang Tcl>proc fizzbuzz {n args} {
<syntaxhighlight lang="tcl">proc fizzbuzz {n args} {
if {$args eq ""} {
if {$args eq ""} {
set args {{3 Fizz} {5 Buzz}}
set args {{3 Fizz} {5 Buzz}}
Line 3,927: Line 3,927:
}
}
}
}
fizzbuzz 20 {3 Fizz} {5 Buzz} {7 Baxx}</lang>
fizzbuzz 20 {3 Fizz} {5 Buzz} {7 Baxx}</syntaxhighlight>


=={{header|Ursa}}==
=={{header|Ursa}}==
This program reads a max number, then reads factors until the user enters a blank line.
This program reads a max number, then reads factors until the user enters a blank line.
<lang ursa>#
<syntaxhighlight lang="ursa">#
# general fizzbuzz
# general fizzbuzz
#
#
Line 3,971: Line 3,971:
end if
end if
out endl console
out endl console
end for</lang>
end for</syntaxhighlight>
Output:
Output:
<pre>>20
<pre>>20
Line 4,000: Line 4,000:


=={{header|VBA}}==
=={{header|VBA}}==
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
Option Explicit


Line 4,034: Line 4,034:
If StrPtr(UserChoice.Name) <> 0 And UserChoice.Number < MaxNumber Then ok = True
If StrPtr(UserChoice.Name) <> 0 And UserChoice.Number < MaxNumber Then ok = True
Loop
Loop
End Function</lang>
End Function</syntaxhighlight>
{{out}}
{{out}}
With the entry :
With the entry :
Line 4,088: Line 4,088:


=={{header|VBScript}}==
=={{header|VBScript}}==
<lang vb>'The Function
<syntaxhighlight lang="vb">'The Function
Function FizzBuzz(range, mapping)
Function FizzBuzz(range, mapping)
data = Array()
data = Array()
Line 4,121: Line 4,121:
y = WScript.StdIn.ReadLine
y = WScript.StdIn.ReadLine
WScript.StdOut.WriteLine ""
WScript.StdOut.WriteLine ""
FizzBuzz x, y</lang>
FizzBuzz x, y</syntaxhighlight>
{{Out|Sample Run}}
{{Out|Sample Run}}
<pre>\Desktop>cscript /nologo fizzbuzz.vbs
<pre>\Desktop>cscript /nologo fizzbuzz.vbs
Line 4,151: Line 4,151:


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
<lang vbnet>Imports System.Globalization
<syntaxhighlight lang="vbnet">Imports System.Globalization


Module Program
Module Program
Line 4,179: Line 4,179:
Next
Next
End Sub
End Sub
End Module</lang>
End Module</syntaxhighlight>


{{out}}
{{out}}
Line 4,208: Line 4,208:


=={{header|Vlang}}==
=={{header|Vlang}}==
<lang vlang>import os
<syntaxhighlight lang="vlang">import os
fn main() {
fn main() {
Line 4,237: Line 4,237:
divisible = false
divisible = false
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Max: 20
<pre>Max: 20
Line 4,267: Line 4,267:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
<lang ecmascript>import "io" for Stdin, Stdout
<syntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout
import "/sort" for Sort
import "/sort" for Sort


Line 4,322: Line 4,322:
if (s == "") s = i.toString
if (s == "") s = i.toString
System.print(s)
System.print(s)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,357: Line 4,357:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>stop:=ask("Count: ").toInt();
<syntaxhighlight lang="zkl">stop:=ask("Count: ").toInt();
fizzBuzzers:=List();
fizzBuzzers:=List();
do(3){ n,txt:=ask(">").split(); fizzBuzzers.append(T(n.toInt(),txt)) }
do(3){ n,txt:=ask(">").split(); fizzBuzzers.append(T(n.toInt(),txt)) }
Line 4,363: Line 4,363:
s:=fizzBuzzers.filter('wrap([(fb,txt)]){ n%fb==0 }).apply("get",1).concat();
s:=fizzBuzzers.filter('wrap([(fb,txt)]){ n%fb==0 }).apply("get",1).concat();
println(s or n);
println(s or n);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,395: Line 4,395:
=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
{{trans|BBC_BASIC}}
<lang zxbasic>10 INPUT "Maximum number: ";max
<syntaxhighlight lang="zxbasic">10 INPUT "Maximum number: ";max
20 INPUT "Number of factors: ";n
20 INPUT "Number of factors: ";n
30 DIM f(n): DIM w$(n,4)
30 DIM f(n): DIM w$(n,4)
Line 4,409: Line 4,409:
130 PRINT
130 PRINT
140 NEXT i
140 NEXT i
150 DEF FN m(a,b)=a-INT (a/b)*b</lang>
150 DEF FN m(a,b)=a-INT (a/b)*b</syntaxhighlight>