General FizzBuzz: Difference between revisions

Add SETL
(Added MiniScript version)
(Add SETL)
 
(29 intermediate revisions by 17 users not shown)
Line 57:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F genfizzbuzz(factorwords, numbers)
V sfactorwords = sorted(factorwords, key' p -> p[0])
[String] lines
Line 65:
R lines.join("\n")
 
print(genfizzbuzz([(5, ‘Buzz’), (3, ‘Fizz’), (7, ‘Baxx’)], 1..20))</langsyntaxhighlight>
 
{{out}}
Line 89:
19
Buzz
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE MAX_NUMBERS="200"
DEFINE MAX_LEN="20"
DEFINE MAX_FACTORS="5"
DEFINE PTR="CARD"
 
PROC PrintResult(BYTE max,n BYTE ARRAY factors PTR ARRAY texts)
BYTE i,j,t
BYTE ARRAY values(MAX_FACTORS)
 
FOR j=0 TO n-1
DO
values(j)=1
OD
 
FOR i=1 TO max
DO
t=0
FOR j=0 TO n-1
DO
IF values(j)=0 THEN
t=1 Print(texts(j))
FI
values(j)==+1
IF values(j)=factors(j) THEN
values(j)=0
FI
OD
IF t=0 THEN PrintB(i) FI
Put(32)
OD
RETURN
 
BYTE FUNC Find(CHAR ARRAY s CHAR c BYTE POINTER err)
BYTE i
 
FOR i=1 TO s(0)
DO
IF s(i)=c THEN
err^=0 RETURN (i)
FI
OD
err^=1
RETURN (0)
 
PROC Main()
BYTE max,i,n,pos,err
BYTE ARRAY factors(MAX_FACTORS)
PTR ARRAY texts(MAX_FACTORS)
CHAR ARRAY
s(100),tmp(100),
t0(MAX_LEN),t1(MAX_LEN),t2(MAX_LEN),
t3(MAX_LEN),t4(MAX_LEN)
texts(0)=t0 texts(1)=t1 texts(2)=t2
texts(3)=t3 texts(4)=t4
 
DO
PrintF("Max number (1-%B): ",MAX_NUMBERS)
max=InputB()
UNTIL max>=1 AND max<=MAX_NUMBERS
OD
 
n=0
DO
PrintF("Number of rules (1-%B): ",MAX_FACTORS)
n=InputB()
UNTIL n>=1 AND n<=MAX_FACTORS
OD
 
FOR i=0 TO n-1
DO
DO
PrintF("Rule #%B (number space text):",i+1)
InputS(s)
pos=Find(s,' ,@err)
IF pos=1 OR pos=s(0) THEN
err=1
FI
IF err=0 THEN
SCopyS(tmp,s,1,pos-1)
factors(i)=ValB(tmp)
IF factors(i)<2 THEN
err=1
FI
SCopyS(texts(i),s,pos+1,s(0))
FI
UNTIL err=0
OD
OD
 
PutE()
PrintResult(max,n,factors,texts)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/General_FizzBuzz.png Screenshot from Atari 8-bit computer]
<pre>
Max number (1-200): 105
Number of rules (1-5): 3
Rule #1 (number space text):3 Fizz
Rule #2 (number space text):5 Buzz
Rule #3 (number space text):7 Baxx
 
1 2 Fizz 4 Buzz Fizz Baxx 8 Fizz Buzz 11 Fizz 13 Baxx FizzBuzz 16 17 Fizz 19 Buzz FizzBaxx 22 23
Fizz Buzz 26 Fizz Baxx 29 FizzBuzz 31 32 Fizz 34 BuzzBaxx Fizz 37 38 Fizz Buzz 41 FizzBaxx 43 44
FizzBuzz 46 47 Fizz Baxx Buzz Fizz 52 53 Fizz Buzz Baxx Fizz 58 59 FizzBuzz 61 62 FizzBaxx 64 Buzz
Fizz 67 68 Fizz BuzzBaxx 71 Fizz 73 74 FizzBuzz 76 Baxx Fizz 79 Buzz Fizz 82 83 FizzBaxx Buzz 86
Fizz 88 89 FizzBuzz Baxx 92 Fizz 94 Buzz Fizz 97 Baxx Fizz Buzz 101 Fizz 103 104 FizzBuzzBaxx
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 143 ⟶ 253:
general_fizz_buzz (20, fizzy);
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 170 ⟶ 280:
=={{header|ALGOL 68}}==
Uses a modified version of the Algol 68 Quicksort task sample.
<langsyntaxhighlight lang="algol68">BEGIN # generalised FizzBuzz #
# prompts for an integer, reads it and returns it #
PROC read integer = ( STRING prompt )INT:
Line 246 ⟶ 356:
print( ( text, newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 280 ⟶ 390:
=={{header|AppleScript}}==
 
<langsyntaxhighlight AppleScriptlang="applescript">--------------------- GENERAL FIZZBUZZ -------------------
 
-- fizzEtc :: [(Int, String)] -> [Symbol]
Line 406 ⟶ 516:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
13
Baxx
FizzBuzz
16
17
Fizz
19
Buzz</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">maxNum: to :integer strip input "Set maximum number: "
facts: map 1..3 'x -> split.words strip input ~"Enter factor |x|: "
loop 1..maxNum 'i [
printNum: true
loop facts 'fact ->
if zero? i % to :integer fact\0 [
prints fact\1
printNum: false
]
print (printNum)? -> i -> ""
]</syntaxhighlight>
 
{{out}}
 
<pre>Set maximum number: 20
Enter factor 1: 3 Fizz
Enter factor 2: 5 Buzz
Enter factor 3: 7 Baxx
1
2
Fizz
Line 430 ⟶ 581:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">; Test parameters
max := 20, fizz := 3, buzz := 5, baxx := 7
 
Line 446 ⟶ 597:
FileAppend %output%`n, *
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 486 ⟶ 637:
 
;Usage:
<langsyntaxhighlight lang="bash">awk -f fizzbuzzGenerate.awk input.txt > fizzbuzzCustom.awk
awk -f fizzbuzzCustom.awk numbers.txt</langsyntaxhighlight>
 
;Program:
<!-- http://ideone.com/fACMfK -->
<!-- (!!) Note: the sandbox at ideone.com does not allow output to files -->
<langsyntaxhighlight lang="awk"># usage: awk -f fizzbuzzGen.awk > fizzbuzzCustom.awk
#
function Print(s) {
Line 524 ⟶ 675:
print "END {print " q2 "# Done." q2 "}"
Print( "# Done." )
}</langsyntaxhighlight>
 
Example output see [[FizzBuzz/AWK#Custom_FizzBuzz]]
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
rem input range
set /p "range=> "
Line 559 ⟶ 710:
)
pause
exit /b 0</langsyntaxhighlight>
{{Out}}
<pre>> 20
Line 590 ⟶ 741:
=={{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.
<langsyntaxhighlight lang="bbcbasic">REM >genfizzb
INPUT "Maximum number: " max%
INPUT "Number of factors: " n%
Line 609 ⟶ 760:
NEXT
IF matched% THEN PRINT ELSE PRINT;i%
NEXT</langsyntaxhighlight>
Output:
<pre>Maximum number: 20
Line 638 ⟶ 789:
 
=={{header|BQN}}==
<langsyntaxhighlight BQNlang="bqn">GenFizzBuzz ← {factors‿words ← <˘⍉>𝕨 ⋄ (∾´∾⟜words/˜·(¬∨´)⊸∾0=factors|⊢)¨1+↕𝕩}</langsyntaxhighlight>
Example usage:
<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" ⟩</langsyntaxhighlight>
 
=={{header|C}}==
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
#include <stdlib.h>
Line 698 ⟶ 849:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 725 ⟶ 876:
=={{header|C sharp}}==
Not extremely clever and doesn't use anything too fancy.
<langsyntaxhighlight lang="csharp">
using System;
 
Line 786 ⟶ 937:
}
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <algorithm>
#include <iostream>
Line 827 ⟶ 978:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 853 ⟶ 1,004:
 
=={{header|Caché ObjectScript}}==
<langsyntaxhighlight Cachélang="caché ObjectScriptobjectscript">GENFIZBUZZ(MAX,WORDS,NUMBERS)
; loop until max, casting numeric to avoid errors
for i=1:1:+MAX {
Line 874 ⟶ 1,025:
} ; next value until MAX
quit</langsyntaxhighlight>
 
 
Line 881 ⟶ 1,032:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
print("enter the max value");
Line 920 ⟶ 1,071:
}
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn fix [pairs]
(map second pairs))
 
Line 937 ⟶ 1,088:
(apply str
(fix f)))))
numbers)))</langsyntaxhighlight>
 
'''Usage:'''
Line 968 ⟶ 1,119:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
(defun fizzbuzz (limit factor-words)
(loop for i from 1 to limit
Line 996 ⟶ 1,147:
(not (zerop (parse-integer input :junk-allowed t))))
finally (fizzbuzz (parse-integer input :junk-allowed t) (read-factors))))
</syntaxhighlight>
</lang>
 
=={{header|Crystal}}==
<syntaxhighlight lang="crystal">
<lang Crystal>
counter = 0
 
Line 1,050 ⟶ 1,201:
exit
end
</syntaxhighlight>
</lang>
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import core.stdc.stdlib;
import std.stdio;
 
Line 1,101 ⟶ 1,252:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,129 ⟶ 1,280:
19
Buzz</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
max = 20
words$[] = [ "Fizz" "Buzz" "Baxx" ]
keys[] = [ 3 5 7 ]
#
for n = 1 to max
prnt = 1
for j = 1 to len keys[]
if n mod keys[j] = 0
write words$[j]
prnt = 0
.
.
if prnt = 1
write n
.
print ""
.
</syntaxhighlight>
{{out}}
<pre>
1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
13
Baxx
FizzBuzz
16
17
Fizz
19
Buzz
</pre>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule General do
def fizzbuzz(input) do
[num | nwords] = String.split(input)
Line 1,150 ⟶ 1,345:
7 Baxx
"""
General.fizzbuzz(input)</langsyntaxhighlight>
 
{{out}}
Line 1,185 ⟶ 1,380:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">
%%% @doc Implementation of General FizzBuzz
%%% @see https://rosettacode.org/wiki/General_FizzBuzz
Line 1,211 ⟶ 1,406:
[fizzbuzz(X, Factors, []) || X <- lists:seq(1, N)]
).
</syntaxhighlight>
</lang>
'''Usage:'''
<pre>
Line 1,245 ⟶ 1,440:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs combinators.extras io kernel math math.parser
math.ranges prettyprint sequences splitting ;
IN: rosetta-code.general-fizzbuzz
Line 1,265 ⟶ 1,460:
: main ( -- ) get-input [ fizzbuzz ] with each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,297 ⟶ 1,492:
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 ..'
<langsyntaxhighlight Forthlang="forth">\ gfb.fs - generalized fizz buzz
: times ( xt n -- )
BEGIN dup WHILE
Line 1,322 ⟶ 1,517:
\ Example:
\ 1 fb[ 3 s" fizz" ]+[ 5 s" buzz" ]+[ 7 s" dizz" ]+[ ]fb 40 times drop
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,333 ⟶ 1,528:
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang="freebasic">' version 01-03-2018
' compile with: fbc -s console
 
Line 1,391 ⟶ 1,586:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Enter maximum number, if number < 1 then the program wil end 110
Line 1,419 ⟶ 1,614:
109
Buzz</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
_mfile = 1
begin enum
_iNewGame
_
_iClose
end enum
_mEdit = 2
 
_window = 1
begin enum 1
_maxNumLabel
_maxNumFld
_f1Label
_f1Fld
_wd1Label
_wd1Fld
_f2Label
_f2Fld
_wd2Label
_wd2Fld
_f3Label
_f3Fld
_wd3Label
_wd3Fld
_playBtn
end enum
 
void local fn BuildMenu
menu _mFile,,, @"File"
menu _mFile, _iNewGame,, @"New Game"
menu _mFile, _iClose,, @"Close", @"w"
MenuItemSetAction( _mFile, _iClose, @"performClose:" )
editmenu _mEdit
end fn
 
void local fn BuildWindow
window _window, @"General FizzBuzz", (0,0,362,188), NSWindowStyleMaskTitled
textlabel _maxNumLabel, @"Maximum number:", (18,150,116,16)
textfield _maxNumFld,, @"20", (140,147,202,21)
ControlSetFormat( _maxNumFld, @"0123456789", YES, 0, 0 )
textlabel _f1Label, @"Factor 1:", (18,121,58,16)
textfield _f1Fld,, @"3", (80,118,54,21)
ControlSetFormat( _f1Fld, @"0123456789", YES, 0, 0 )
textlabel _wd1Label, @"Word 1:", (138,121,52,16)
textfield _wd1Fld,, @"Fizz", (196,118,146,21)
textlabel _f2Label, @"Factor 2:", (18,92,58,16)
textfield _f2Fld,, @"5", (80,89,54,21)
ControlSetFormat( _f2Fld, @"0123456789", YES, 0, 0 )
textlabel _wd2Label, @"Word 2:", (138,92,52,16)
textfield _wd2Fld,, @"Buzz", (196,89,146,21)
textlabel _f3Label, @"Factor 3:", (18,63,58,16)
textfield _f3Fld,, @"7", (80,60,54,21)
ControlSetFormat( _f3Fld, @"0123456789", YES, 0, 0 )
textlabel _wd3Label, @"Word 3:", (138,63,52,16)
textfield _wd3Fld,, @"Baxx", (196,60,146,21)
button _playBtn,,, @"Play FizzBuzz", (122,13,118,32)
WindowMakeFirstResponder( _window, _maxNumFld )
end fn
 
void local fn PlayFizzBuzz
long maxNum = intval(textfield(_maxNumFld))
long f1 = intval(textfield(_f1Fld))
long f2 = intval(textfield(_f2Fld))
long f3 = intval(textfield(_f3Fld))
CFStringRef f1Word = textfield(_wd1Fld)
CFStringRef f2Word = textfield(_wd2Fld)
CFStringRef f3Word = textfield(_wd3Fld)
CFStringRef string = NULL
NSLogClear
long i
for i = 1 to maxNum
string = @""
if ( i mod f1 == 0 ) then string = f1Word
if ( i mod f2 == 0 ) then string = fn StringByAppendingString( string, f2Word )
if ( i mod f3 == 0 ) then string = fn StringByAppendingString( string, f3Word )
if ( len(string) == 0 ) then string = fn StringWithFormat( @"%ld", i )
NSLog(@"%@",string)
next
end fn
 
 
void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick
select ( tag )
case _playBtn : fn PlayFizzBuzz
end select
end select
end fn
 
fn BuildMenu
fn BuildWindow
 
on dialog fn DoDialog
 
HandleEvents
</syntaxhighlight>
[[file:General FizzBuzz2 FB.png]]
 
=={{header|Go}}==
<syntaxhighlight lang="go">
<lang Go>
package main
 
Line 1,455 ⟶ 1,763:
}
 
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight Groovylang="groovy">def log = ''
(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'
Line 1,464 ⟶ 1,772:
:(value+'\n')}
println log
</syntaxhighlight>
</lang>
<pre>
1
Line 1,507 ⟶ 1,815:
BUZZ
</pre>
 
''' or (inspired by normal FizzBuzz in Groovy: https://rosettacode.org/wiki/FizzBuzz#Groovy '''
<syntaxhighlight lang="groovy">
def fizzBuzz = { i -> [[3,'Fizz'],[5,'Buzz'],[7,'Baxx']].collect{i%it[0]?'':it[1]}.join()?:i}
1.upto(100){println fizzBuzz(it)}
</syntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">fizz :: (Integral a, Show a) => a -> [(a, String)] -> String
fizz a xs
| null result = show a
Line 1,526 ⟶ 1,840:
mapM_ (\ x -> putStrLn $ fizz x multiples) [1..n]
where convert [x, y] = (read x, y)
</syntaxhighlight>
</lang>
 
Or, as a function which takes a list of rules as an argument:
 
<langsyntaxhighlight lang="haskell">type Rule = (Int, String)
 
----------------- FIZZETC (USING RULE SET) ---------------
Line 1,553 ⟶ 1,867:
main :: IO ()
main = mapM_ putStrLn $ take 20 fizzTest
</syntaxhighlight>
</lang>
{{Out}}
<pre>1
Line 1,580 ⟶ 1,894:
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:
 
<langsyntaxhighlight Jlang="j">genfb=:1 :0
:
b=. * x|/1+i.y
>,&":&.>/(m#inv"_1~-.b),(*/b)#&.>1+i.y
)</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> 3 5 7 ('Fizz';'Buzz';'Baxx')genfb 20
1
2
Line 1,608 ⟶ 1,922:
Fizz
19
Buzz </langsyntaxhighlight>
 
For our example, b looks like this:
 
<langsyntaxhighlight Jlang="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 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1</langsyntaxhighlight>
 
<code>*/b</code> gives us 1s where we want numbers and 0s where we want to plug in the strings:
 
<langsyntaxhighlight Jlang="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</langsyntaxhighlight>
 
<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:
 
<langsyntaxhighlight Jlang="j">┌┬┬────┬┬────┬────┬────┬┬────┬────┬┬────┬┬────┬────┬┬┬────┬┬────┐
│││Fizz││ │Fizz│ ││Fizz│ ││Fizz││ │Fizz│││Fizz││ │
├┼┼────┼┼────┼────┼────┼┼────┼────┼┼────┼┼────┼────┼┼┼────┼┼────┤
Line 1,629 ⟶ 1,943:
├┼┼────┼┼────┼────┼────┼┼────┼────┼┼────┼┼────┼────┼┼┼────┼┼────┤
│││ ││ │ │Baxx││ │ ││ ││Baxx│ │││ ││ │
└┴┴────┴┴────┴────┴────┴┴────┴────┴┴────┴┴────┴────┴┴┴────┴┴────┘</langsyntaxhighlight>
 
All that remains is to assemble these pieces into the final result...
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class FizzBuzz {
 
public static void main(String[] args) {
Line 1,662 ⟶ 1,976:
}
 
}</langsyntaxhighlight>
 
 
'''For a more complete example see [[/jFizzBuzz|jFizzBuzz]]'''
 
''' Or using a lot of Lambdas ... '''
 
<syntaxhighlight lang="java">
import java.util.stream.*;
import java.util.function.*;
import java.util.*;
public class fizzbuzz_general {
/**
* To run: java fizzbuzz_general.java 3=Fizz 5=Buzz 7=Baxx 100
*
*/
public static void main(String[] args) {
Function<String[],Function<Integer,String>> make_cycle_function =
parts -> j -> j%(Integer.parseInt(parts[0]))==0?parts[1]:"";
List<Function<Integer,String>> cycle_functions = Stream.of(args)
.map(arg -> arg.split("="))
.filter(parts->parts.length==2)
.map(make_cycle_function::apply)
.collect(Collectors.toList());
Function<Integer,String> moduloTesters = i -> cycle_functions.stream()
.map(fcn->fcn.apply(i))
.collect(Collectors.joining());
BiFunction<Integer,String,String> formatter =
(i,printThis) -> "".equals(printThis)?Integer.toString(i):printThis;
Function<Integer,String> fizzBuzz = i -> formatter.apply(i,moduloTesters.apply(i));
IntStream.rangeClosed(0,Integer.parseInt(args[args.length-1]))
.mapToObj(Integer::valueOf)
.map(fizzBuzz::apply)
.forEach(System.out::println);
}
}
</syntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,675 ⟶ 2,023:
 
First as compacted by Google's Closure compiler:
<langsyntaxhighlight JavaScriptlang="javascript">function fizz(d, e) {
return function b(a) {
return a ? b(a - 1).concat(a) : [];
Line 1,683 ⟶ 2,031:
}, "") || a.toString()) + "\n";
}, "");
}</langsyntaxhighlight>
 
and then in the original expanded form, for better legibility:
 
<langsyntaxhighlight JavaScriptlang="javascript">function fizz(lstRules, lngMax) {
 
return (
Line 1,712 ⟶ 2,060:
}
 
fizz([[3, 'Fizz'], [5, 'Buzz'], [7, 'Baxx']], 20);</langsyntaxhighlight>
 
{{out}}
Line 1,739 ⟶ 2,087:
===ES6===
 
<langsyntaxhighlight JavaScriptlang="javascript">// range :: Int -> Int -> [Int]
const range = (min, max) =>
Array.from({ length: max - min }, (_, i) => min + i)
Line 1,757 ⟶ 2,105:
).join('\n')
 
console.log(fizzBuzz(20))</langsyntaxhighlight>
 
{{Out}}
Line 1,785 ⟶ 2,133:
=={{header|Julia}}==
For simplicity, assume that the user will enter valid input.
<langsyntaxhighlight Julialang="julia">function fizzbuzz(triggers :: Vector{Tuple{Int, ASCIIString}}, upper :: Int)
for i = 1 : upper
triggered = false
Line 1,813 ⟶ 2,161:
 
println("EOF\n")
fizzbuzz(triggers, upper)</langsyntaxhighlight>
 
{{out}}
Line 1,847 ⟶ 2,195:
=={{header|Kotlin}}==
 
<langsyntaxhighlight Kotlinlang="kotlin">fun main(args: Array<String>) {
 
//Read the maximum number, set to 0 if it couldn't be read
Line 1,874 ⟶ 2,222:
println(i)
}
}</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">function generalisedFizzBuzz m, f1, f2, f3
put f1 & cr & f2 & cr & f3 into factors
sort factors ascending numeric
Line 1,898 ⟶ 2,246:
end repeat
return fizzbuzz
end generalisedFizzBuzz</langsyntaxhighlight>Example<langsyntaxhighlight LiveCodelang="livecode">put generalisedFizzBuzz(20,"7 baxx","3 fizz","5 buzz")</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function genFizz (param)
local response
print("\n")
Line 1,920 ⟶ 2,268:
param.factor[i], param.word[i] = io.read("*number", "*line")
end
genFizz(param)</langsyntaxhighlight>
 
=== Without modulo ===
{{trans|Python}}
<langsyntaxhighlight Lualang="lua">local function fizzbuzz(n, mods)
local res = {}
 
Line 1,959 ⟶ 2,307:
print(fizzbuzz(n, mods))
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,979 ⟶ 2,327:
 
===Fast Version without Modulo===
<syntaxhighlight lang="lua">#!/usr/bin/env luajit
<lang lua>
 
#!/usr/bin/env luajit
local tonum = arg[1] or tonumber(arg[1]) or 110
local t = {
{3, "Fizz"},
{5, "Buzz"},
{7, "Gazz"},
}
 
local cnt=setmetatable({},{__index=function() return 0 end})
-- `cnt` contains counters for each factor-word pair; when a counter of a pair reaches its factor,
for i=1,to do
-- the counter is reset to zero and the word is written to output
for i=1,#t do
local cnt = setmetatable({}, {__index = function() return 0 end})
cnt[i]=cnt[i]+1
 
for i = 1,num do
for i = 1,#t do
cnt[i] = cnt[i]+1
end
local match = false
for i=1,#t do
if cnt[i] == t[i][1] then
io.write(t[i][2])
cnt[i] = 0
match = true
end
end
Line 2,005 ⟶ 2,357:
io.write(", ")
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,013 ⟶ 2,365:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">findNum := proc(str) #help parse input
local i;
i := 1:
Line 2,044 ⟶ 2,396:
if (not factored) then printf("%d", i): end if:
printf("\n");
end do:</langsyntaxhighlight>
{{Out|Output}}
<pre>1
Line 2,068 ⟶ 2,420:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">list={{5,"Buzz"},{3,"Fizz"},{7,"Baxx"}};
runTo=(*LCM@@list[[All,1]]+1*)20;
Column@Table[
Select[list,Mod[x,#[[1]]]==0&][[All,2]]/.{}->{x}
,{x,1,runTo}
]</langsyntaxhighlight>
 
<pre>1
Line 2,100 ⟶ 2,452:
This implementation improves slightly over the proposed task, making the prompts a little friendlier.
 
<langsyntaxhighlight MiniScriptlang="miniscript">factorWords = {}
 
maxNr = val(input("Max number? "))
Line 2,123 ⟶ 2,475:
end for
if matchingWords then print matchingWords else print nr
end for</langsyntaxhighlight>
 
Sample session:
Line 2,153 ⟶ 2,505:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE GeneralFizzBuzz;
FROM Conversions IMPORT StrToInt;
FROM FormatString IMPORT FormatString;
Line 2,255 ⟶ 2,607:
 
ReadChar
END GeneralFizzBuzz.</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
{{trans|Ursa}}
<langsyntaxhighlight Nanoquerylang="nanoquery">factors = {}
words = {}
 
Line 2,292 ⟶ 2,644:
end
println
end</langsyntaxhighlight>
 
{{out}}
Line 2,323 ⟶ 2,675:
=={{header|Nim}}==
This solution has no input validation
<langsyntaxhighlight lang="nim">
import parseutils, strutils, algorithm
 
Line 2,369 ⟶ 2,721:
 
 
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
<syntaxhighlight lang="ocaml">
<lang OCaml>
(* Task : General_FizzBuzz *)
 
Line 2,406 ⟶ 2,758:
 
gen_fizz_buzz 20 [(3, "Fizz"); (5, "Buzz"); (7, "Baxx")] ;;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,434 ⟶ 2,786:
{{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).
<langsyntaxhighlight lang="parigp">fizz(n,v[..])=
{
v=vecsort(v,1);
Line 2,448 ⟶ 2,800:
);
}
fizz(20,[3,"Fizz"],[5,"Buzz"],[7,"Baxx"])</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,472 ⟶ 2,824:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">
#!bin/usr/perl
use 5.020;
Line 2,524 ⟶ 2,876:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,559 ⟶ 2,911:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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;">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,575 ⟶ 2,927:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,601 ⟶ 2,953:
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php"><?php
 
$max = 20;
Line 2,617 ⟶ 2,969:
}
 
?></langsyntaxhighlight>
{{out}}
<pre>1
Line 2,640 ⟶ 2,992:
Buzz
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">interactive =>
print("> "),
MaxNum = read_int(),
Map = new_map(),
print("> "),
while (Line = read_line(), Line != "")
[N,V] = split(Line),
Map.put(N.to_int,V),
print("> ")
end,
general_fizzbuzz(MaxNum,Map.to_list.sort),
nl.
 
general_fizzbuzz(N,L) =>
FB = [I.to_string : I in 1..N],
foreach(I in 1..N)
Vs = [V : K=V in L, I mod K == 0].join(''),
if Vs != "" then
FB[I] := Vs
end
end,
println([F : F in FB].join(" ")).</syntaxhighlight>
 
Testing:
<pre>$ echo "106\n3 Fizz\n5 Buzz\n7 Bazz\n" | picat -g interactive general_fizzbuzz.pi</pre>
 
Here's an interactive session:
{{out}}
<pre>> 106
> 3 Fizz
> 5 Buzz
> 7 Baxx
>
1
2
Fizz
4
Buzz
Fizz
Bazz
8
Fizz
Buzz
11
Fizz
13
Bazz
FizzBuzz
16
17
Fizz
19
Buzz
FizzBazz
22
...
Bazz
Fizz
Buzz
101
Fizz
103
104
FizzBuzzBazz
106</pre>
 
 
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de general (N Lst)
(for A N
(prinl
Line 2,652 ⟶ 3,073:
A ) ) ) )
 
(general 20 '((3 . Fizz) (5 . Buzz) (7 . Baxx)))</langsyntaxhighlight>
 
{{out}}
Line 2,677 ⟶ 3,098:
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">$limit = 20
$data = @("3 Fizz","5 Buzz","7 Baxx")
#An array with whitespace as the delimiter
Line 2,695 ⟶ 3,116:
Write-HoSt $outP
}
}</langsyntaxhighlight>
{{Out}}
<pre>PS> ./GENFB
Line 2,722 ⟶ 3,143:
=={{header|Prolog}}==
Assuming the user specifies as input two facts of the form:
<langsyntaxhighlight lang="prolog">maxNumber(105).
factors([(3, "Fizz"), (5, "Buzz"), (7, "Baxx")]).</langsyntaxhighlight>
 
A simple Prolog solution to the generalised FizzBuzz problem is as follows:
 
<langsyntaxhighlight lang="prolog">go :- maxNumber(M), factors(Fs), MLast is M+1, loop(1,MLast,Fs).
 
loop(B,B,_).
Line 2,737 ⟶ 3,158:
fizzbuzz(N,[(F,S)|Fs],Res) :-
fizzbuzz(N,Fs,OldRes),
( N mod F =:= 0, string_concat(S,OldRes,Res) ; Res = OldRes ).</langsyntaxhighlight>
 
The program can be launched by querying the predicate
<syntaxhighlight lang ="prolog">?- go.</langsyntaxhighlight>
 
It is worth noting that
<langsyntaxhighlight lang="prolog">factors([(3, "Fizz"), (5, "Buzz")]).</langsyntaxhighlight>
corresponds to basic FizzBuzz and that the proposed solution can handle an arbitrary number of factors.
 
Line 2,750 ⟶ 3,171:
===Elegant naive version===
 
<langsyntaxhighlight lang="python">def genfizzbuzz(factorwords, numbers):
factorwords.# sort(key=lambda p:entries by p[0])factor
factorwords.sort(key=lambda factor_and_word: factor_and_word[0])
lines = []
for num in numbers:
words = ''.join(wrdword for factfactor, wrdword in factorwords if (num % factfactor) == 0)
lines.append(words if words else str(num))
return '\n'.join(lines)
 
if __name__ == '__main__':
print(genfizzbuzz([(5, 'Buzz'), (3, 'Fizz'), (7, 'Baxx')], range(1, 21)))</langsyntaxhighlight>
 
{{out}}
Line 2,783 ⟶ 3,205:
Buzz</pre>
 
===One-liner using generator expressions===
===Unelegant naive version===
<syntaxhighlight lang="python">n = 20
 
'''First contribution to rosettacode, uses list comprehension to join the lists and args to allow for arbitrary range'''
<lang python>def genfizzbuzz(numberlist, wordlist, *args):
nml = [[numberlist[i], wordlist[i]] for i in range(len(numberlist))]
for z in range(*args):
res = ""
for j in nml:
if z % j[0] == 0:
res += j[1]
print(res or z)
 
 
genfizzbuzz([3, 5, 7], ['Fizz', 'Buzz', 'Baxx'], 1, 21)
</lang>
 
{{out}}
<pre>
1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
13
Baxx
FizzBuzz
16
17
Fizz
19
Buzz
</pre>
 
===One liner using generator expressions===
<lang python>n = 20
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) </langsyntaxhighlight>
 
 
===Generator using counters instead of modulo===
{{works with|Python|3.x}}
<langsyntaxhighlight Pythonlang="python">from collections import defaultdict
 
nN = 100
modsFACTOR_TO_WORD = {
3: "Fizz",
5: "Buzz",
}
 
def fizzbuzz(n=nN, modsfactor_to_word=modsFACTOR_TO_WORD):
 
factors = defaultdict(list)
for mod in mods:
factors[mod].append(mod)
 
for ifactor in range(1,n+1)factor_to_word:
factors[factor].append(factor)
 
for i in range(1, n+1):
res = ''
for modfactor in sorted(factors.pop(i, ())):
factors[i+modfactor].append(modfactor)
res += modsfactor_to_word[modfactor]
yield res or str(i)
 
if __name__ == '__main__':
 
n = int(input())
modsn = { int(k): v for k,v in (input().split(maxsplit=1)'Enter fornumber: _ in range(3')) }
 
mods = {
int(k): v
for k, v in (
input('Enter "<factor> <word>" (without quotes): ').split(maxsplit=1)
for _ in range(3)
)
}
 
for line in fizzbuzz(n, mods):
print(line)
</syntaxhighlight>
</lang>
 
===Sieve of Eratosthenes===
'''This variant uses ranges with step 3, 5, etc. Preserves order and duplicate moduli.'''
<langsyntaxhighlight Pythonlang="python">from collections import defaultdict
 
n = 100
Line 2,892 ⟶ 3,284:
 
print(fizzbuzz(n, mods))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 2,921 ⟶ 3,313:
Four Two eight...
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ sortwith
[ dip [ 1 peek ]
1 peek > ]
$ "" swap rot
times
[ i^ 1+
over $ "" unrot
witheach
[ dip dup do
dip swap mod
iff drop
else
[ rot swap
join swap ] ]
over $ "" =
iff [ number$ join ]
else drop
rot swap join
space join swap ]
drop
-1 split drop ] is generalfizzbuzz ( n [ --> $ )
 
105
' [ [ $ "fizz" 3 ]
[ $ "baxx" 7 ]
[ $ "buzz" 5 ] ] generalfizzbuzz
nest$ 60 wrap$ cr</syntaxhighlight>
{{out}}
 
<pre>1 2 fizz 4 buzz fizz baxx 8 fizz buzz 11 fizz 13 baxx
fizzbuzz 16 17 fizz 19 buzz fizzbaxx 22 23 fizz buzz 26 fizz
baxx 29 fizzbuzz 31 32 fizz 34 buzzbaxx fizz 37 38 fizz buzz
41 fizzbaxx 43 44 fizzbuzz 46 47 fizz baxx buzz fizz 52 53
fizz buzz baxx fizz 58 59 fizzbuzz 61 62 fizzbaxx 64 buzz
fizz 67 68 fizz buzzbaxx 71 fizz 73 74 fizzbuzz 76 baxx fizz
79 buzz fizz 82 83 fizzbaxx buzz 86 fizz 88 89 fizzbuzz baxx
92 fizz 94 buzz fizz 97 baxx fizz buzz 101 fizz 103 104
fizzbuzzbaxx</pre>
 
=={{header|R}}==
===... 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.
<langsyntaxhighlight rlang="rsplus">genFizzBuzz <- function(n, ...)
{
args <- list(...)
Line 2,941 ⟶ 3,375:
}
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"))</langsyntaxhighlight>
 
===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.
<langsyntaxhighlight rlang="rsplus">namedGenFizzBuzz <- function(n, namedNums)
{
factors <- sort(namedNums)#Required by the task: We must go from least factor to greatest.
Line 2,957 ⟶ 3,391:
namedGenFizzBuzz(105, namedNums)
shuffledNamedNums <- c(Buzz=5, Prax=9, Fizz=3, Baxx=7)
namedGenFizzBuzz(105, shuffledNamedNums)</langsyntaxhighlight>
 
=={{header|Racket}}==
{{trans|Python}}
<langsyntaxhighlight Racketlang="racket">#lang racket/base
 
(define (get-matches num factors/words)
Line 2,979 ⟶ 3,413:
(gen-fizzbuzz 1 21 '((3 "Fizz")
(5 "Buzz")
(7 "Baxx")))</langsyntaxhighlight>
{{out}}
<pre>1
Line 3,003 ⟶ 3,437:
 
===Alternate Solution===
<langsyntaxhighlight Racketlang="racket">#lang racket
(require racket/match)
 
Line 3,019 ⟶ 3,453:
(void))
 
(fizz-buzz 20 '(3 . "Fizz") '(5 . "Buzz") '(7 . "Baxx"))</langsyntaxhighlight>
 
{{out}}
Line 3,046 ⟶ 3,480:
(formerly Perl 6)
{{works with|rakudo|2015-09-20}}
<syntaxhighlight lang="raku" perl6line># General case implementation of a "FizzBuzz" class.
# Defaults to standard FizzBuzz unless a new schema is passed in.
class FizzBuzz {
Line 3,073 ⟶ 3,507:
# And for fun
say 'Using: 21 ' ~ <2 Pip 4 Squack 5 Pocketa 7 Queep>;
say join ', ', GeneralFizzBuzz(21, <2 Pip 4 Squack 5 Pocketa 7 Queep>);</langsyntaxhighlight>
{{Out}}
<pre>Using: 20 3 Fizz 5 Buzz 7 Baxx
Line 3,101 ⟶ 3,535:
 
Here's the same program in a more functional idiom:
<syntaxhighlight lang="raku" perl6line>sub genfizzbuzz($n, +@fb) {
[Z~](
do for @fb || <3 fizz 5 buzz> -> $i, $s {
Line 3,109 ⟶ 3,543:
}
 
.say for genfizzbuzz(20, <3 Fizz 5 Buzz 7 Baxx>);</langsyntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">Red ["FizzBuzz"]
 
nmax: to-integer ask "Max number: "
Line 3,125 ⟶ 3,559:
print either empty? res [n] [res]
]
halt</langsyntaxhighlight>
{{Out}}
<pre>Max number: 21
Line 3,154 ⟶ 3,588:
FizzBaxx
(halted)</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, <Numb <Card>>: s.Max
, <ReadFactors>: e.Factors
, <Iota 1 s.Max>: e.Nums
= <Each Fizz (e.Factors) e.Nums>;
};
Fizz {
s.I e.Facs, <Each Fac (s.I) e.Facs>: {
= <Prout <Symb s.I>>;
e.X = <Prout e.X>;
};
}
 
Fac {
(s.Fac e.FacWord) s.I, <Mod s.I s.Fac>: {
0 = e.FacWord;
s.X = ;
};
};
 
ReadFactors {
, <Card>: {
0 = ; = ;
e.Line = (<Line e.Line>) <ReadFactors>;
};
};
 
Line {
e.X, <Split ' ' e.X>: (e.Num) e.Word =
<Numb e.Num> <Trim ' ' e.Word>;
};
Split {
s.C e.X s.C e.Y = (e.X) e.Y;
s.C e.X = (e.X);
};
 
Trim {
s.C = ;
s.C s.C e.X = <Trim s.C e.X>;
s.C e.X s.C = <Trim s.C e.X>;
s.C s.I e.X = s.I <Trim s.C e.X>;
};
 
Iota {
s.End s.End = s.End;
s.Start s.End = s.Start <Iota <+ 1 s.Start> s.End>;
};
 
Each {
s.F (e.Arg) = ;
s.F (e.Arg) t.I e.X = <Mu s.F t.I e.Arg> <Each s.F (e.Arg) e.X>;
};</syntaxhighlight>
{{out}}
<tt><i>20<br>
3 Fizz<br>
5 Buzz<br>
7 Baxx<br></i>
<br>
1<br>
2<br>
Fizz<br>
4<br>
Buzz<br>
Fizz<br>
Baxx<br>
8<br>
Fizz<br>
Buzz<br>
11<br>
Fizz<br>
13<br>
Baxx<br>
FizzBuzz<br>
16<br>
17<br>
Fizz<br>
19<br>
Buzz</tt>
 
=={{header|REXX}}==
=== idiomatic version ===
<langsyntaxhighlight lang="rexx">/*REXX program shows a generalized FizzBuzz program: #1 name1 #2 name2 ··· */
parse arg h $ /*obtain optional arguments from the CL*/
if h='' | h="," then h= 20 /*Not specified? Then use the default.*/
Line 3,172 ⟶ 3,688:
end /*k*/ /* [↑] Note: the factors may be zero.*/
say word(z j, 1) /*display the number or its factors. */
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 3,198 ⟶ 3,714:
 
=== optimized version ===
<langsyntaxhighlight lang="rexx">/*REXX program shows a generalized FizzBuzz program: #1 name1 #2 name2 ··· */
parse arg h $ /*obtain optional arguments from the CL*/
if h='' | h="," then h= 20 /*Not specified? Then use the default.*/
Line 3,208 ⟶ 3,724:
end /*k*/ /* [↑] Note: the factors may be zero.*/
say word(Z j, 1) /*display the number or its factors. */
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
limit = 20
for n = 1 to limit
Line 3,221 ⟶ 3,737:
next
 
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def general_fizzbuzz(text)
num, *nword = text.split
num = num.to_i
Line 3,241 ⟶ 3,757:
EOS
 
general_fizzbuzz(text)</langsyntaxhighlight>
 
{{out}}
Line 3,269 ⟶ 3,785:
=={{header|Rust}}==
 
<langsyntaxhighlight lang="rust">use std::io;
use std::io::BufRead;
 
Line 3,307 ⟶ 3,823:
}
}
}</langsyntaxhighlight>
 
This solution stores the Fizz Buzz state in a struct, leveraging types and the standard library for a more general solution:
 
<langsyntaxhighlight lang="rust">use std::collections::BTreeMap;
use std::fmt::{self, Write};
use std::io::{self, Stdin};
Line 3,435 ⟶ 3,951:
assert_eq!(expected, &printed);
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 3,472 ⟶ 3,988:
===Simple===
 
<langsyntaxhighlight Scalalang="scala">import scala.io.{Source, StdIn}
 
object GeneralFizzBuzz extends App {
Line 3,485 ⟶ 4,001:
println(if (words.nonEmpty) words.mkString else i)
}
}</langsyntaxhighlight>
 
===LazyList (f/k/a Stream)===
 
<langsyntaxhighlight Scalalang="scala">import scala.io.{Source, StdIn}
 
object GeneralFizzBuzz extends App {
Line 3,506 ⟶ 4,022:
.sorted
fizzBuzz(factors).take(max).foreach(println)
}</langsyntaxhighlight>
 
===Scala 3 (Dotty)===
 
{{trans|LazyList (f/k/a Stream)}}
<langsyntaxhighlight Scalalang="scala">import scala.io.{Source, StdIn}
 
def fizzBuzzTerm(n: Int, factors: Seq[(Int, String)]): String | Int =
Line 3,526 ⟶ 4,042:
.map { case Array(k, v) => k.toInt -> v }
.sorted
fizzBuzz(factors).take(max).foreach(println)</langsyntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program general_fizzbuzz;
fizzbuzz(20, {[3,"Fizz"], [5,"Buzz"], [7,"Baxx"]});
 
proc fizzbuzz(maxno, factors);
loop for i in [1..maxno] do
print(+/[word : word = factors(f) | i mod f=0] ? str i);
end loop;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
13
Baxx
FizzBuzz
16
17
Fizz
19
Buzz</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">class FizzBuzz(schema=Hash(<3 Fizz 5 Buzz>...)) {
method filter(this) {
var fb = ''
Line 3,547 ⟶ 4,094:
}
 
GeneralFizzBuzz(20, <3 Fizz 5 Buzz 7 Baxx>).each { .say }</langsyntaxhighlight>
{{out}}
<pre>
Line 3,570 ⟶ 4,117:
19
Buzz
</pre>
 
=={{header|SQL}}==
{{works with|ORACLE 19c}}
This is not a particularly efficient solution, but it gets the job done.
 
<syntaxhighlight lang="sql">
/*
This code is an implementation of "General FizzBuzz" in SQL ORACLE 19c
*/
select lpad( nvl( case when mod(level, 3) = 0 then 'Fizz' end
|| case when mod(level, 5) = 0 then 'Buzz' end
|| case when mod(level, 7) = 0 then 'Baxx' end
, level)
,12) as output
from dual
connect by level <= 107
;
/
</syntaxhighlight>
 
{{out}}
<pre>
1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
13
Baxx
FizzBuzz
16
17
Fizz
19
Buzz
FizzBaxx
...
101
Fizz
103
104
FizzBuzzBaxx
106
107
</pre>
 
=={{header|Swift}}==
 
<syntaxhighlight lang="text">import Foundation
 
print("Input max number: ", terminator: "")
Line 3,611 ⟶ 4,210:
 
print("\(factors.isEmpty ? String(i) : factors)")
}</langsyntaxhighlight>
 
{{out}}
Line 3,641 ⟶ 4,240:
 
=={{header|Tailspin}}==
<langsyntaxhighlight lang="tailspin">
def input: {N: 110"1", words: [ { mod: 3"1", word: 'Fizz' }, { mod: 5"1", word: 'Buzz'}, {mod:7"1", word: 'Baxx'}]};
 
Line 3,648 ⟶ 4,247:
templates maybeSay
def word: $.word;
$i mod $.mod -> \(<=0"1"> $word !\) !
end maybeSay
$input.words... -> maybeSay !
end sayWords
 
[ 1"1"..$input.N -> '$->sayWords;' ] -> \[i](<=''> $i ! <> $ !\) -> '$;
' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,666 ⟶ 4,265:
For fun, the below implementation is a compatible extension of [[FizzBuzz#Tcl]]:
 
<langsyntaxhighlight Tcllang="tcl">proc fizzbuzz {n args} {
if {$args eq ""} {
set args {{3 Fizz} {5 Buzz}}
Line 3,680 ⟶ 4,279:
}
}
fizzbuzz 20 {3 Fizz} {5 Buzz} {7 Baxx}</langsyntaxhighlight>
 
=={{header|Ursa}}==
This program reads a max number, then reads factors until the user enters a blank line.
<langsyntaxhighlight lang="ursa">#
# general fizzbuzz
#
Line 3,724 ⟶ 4,323:
end if
out endl console
end for</langsyntaxhighlight>
Output:
<pre>>20
Line 3,753 ⟶ 4,352:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 3,787 ⟶ 4,386:
If StrPtr(UserChoice.Name) <> 0 And UserChoice.Number < MaxNumber Then ok = True
Loop
End Function</langsyntaxhighlight>
{{out}}
With the entry :
Line 3,841 ⟶ 4,440:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">'The Function
Function FizzBuzz(range, mapping)
data = Array()
Line 3,874 ⟶ 4,473:
y = WScript.StdIn.ReadLine
WScript.StdOut.WriteLine ""
FizzBuzz x, y</langsyntaxhighlight>
{{Out|Sample Run}}
<pre>\Desktop>cscript /nologo fizzbuzz.vbs
Line 3,904 ⟶ 4,503:
 
=={{header|Visual Basic .NET}}==
<langsyntaxhighlight lang="vbnet">Imports System.Globalization
 
Module Program
Line 3,932 ⟶ 4,531:
Next
End Sub
End Module</langsyntaxhighlight>
 
{{out}}
Line 3,959 ⟶ 4,558:
19
Buzz</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import os
fn main() {
max := os.input('Max: ').int()
f1 := os.input('Starting factor (#) and word: ').fields()
f2 := os.input('Next factor (#) and word: ').fields()
f3 := os.input('Next factor (#) and word: ').fields()
//using the provided data
words := {
f1[0].int(): f1[1],
f2[0].int(): f2[1],
f3[0].int(): f3[1],
}
keys := words.keys()
mut divisible := false
for i := 1; i <= max; i++ {
for n in keys {
if i % n == 0 {
print(words[n])
divisible = true
}
}
if !divisible {
print(i)
}
println('')
divisible = false
}
}</syntaxhighlight>
{{out}}
<pre>Max: 20
Starting factor: 3 Fizz
Next factor: 5 Buzz
Next factor: 7 Baxx
1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
13
Baxx
FizzBuzz
16
17
Fizz
19
Buzz
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdin, Stdout
import "./sort" for Sort
 
var n
Line 4,017 ⟶ 4,674:
if (s == "") s = i.toString
System.print(s)
}</langsyntaxhighlight>
 
{{out}}
Line 4,043 ⟶ 4,700:
13
Bazz
FizzBuzz
16
17
Fizz
19
Buzz
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">string 0; \use zero-terminated strings
 
proc GetString(S); \Get string (S) from user
char S;
[loop [S(0):= ChIn(0);
if S(0) = $0D \CR\ then quit;
S:= S+1;
];
S(0):= 0; \replace CR with terminator
];
 
int Limit, I, Factor(3), Str(3,40), F(3), N;
[Limit:= IntIn(0);
for I:= 0 to 2 do
[Factor(I):= IntIn(0);
GetString(Str(I));
F(I):= 0;
];
for N:= 1 to Limit do
[for I:= 0 to 2 do
[F(I):= F(I)+1;
if F(I) >= Factor(I) then
[Text(0, Str(I));
F(I):= 0;
];
];
if F(0)*F(1)*F(2) # 0 then IntOut(0, N);
CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
20
3 Fizz
5 Buzz
7 Baxx
1
2
Fizz
4
Buzz
Fizz
Baxx
8
Fizz
Buzz
11
Fizz
13
Baxx
FizzBuzz
16
Line 4,052 ⟶ 4,768:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">stop:=ask("Count: ").toInt();
fizzBuzzers:=List();
do(3){ n,txt:=ask(">").split(); fizzBuzzers.append(T(n.toInt(),txt)) }
Line 4,058 ⟶ 4,774:
s:=fizzBuzzers.filter('wrap([(fb,txt)]){ n%fb==0 }).apply("get",1).concat();
println(s or n);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,090 ⟶ 4,806:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
<langsyntaxhighlight lang="zxbasic">10 INPUT "Maximum number: ";max
20 INPUT "Number of factors: ";n
30 DIM f(n): DIM w$(n,4)
Line 4,104 ⟶ 4,820:
130 PRINT
140 NEXT i
150 DEF FN m(a,b)=a-INT (a/b)*b</langsyntaxhighlight>
2,094

edits