FizzBuzz: Difference between revisions

5,864 bytes added ,  22 days ago
 
(17 intermediate revisions by 12 users not shown)
Line 202:
fb = |{ fizz }{ buzz }| in
( switch #( fb when space then i else fb ) ) ) ).</syntaxhighlight>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN fizzbuzz num:
PUT "" IN result
PUT {[3]: "Fizz"; [5]: "Buzz"} IN divwords
FOR div IN keys divwords:
IF num mod div=0:
PUT result^divwords[div] IN result
IF result="":
PUT num>>0 IN result
RETURN result
 
FOR i IN {1..100}:
WRITE fizzbuzz i/</syntaxhighlight>
 
=={{header|ACL2}}==
Line 491 ⟶ 505:
[38] ⍝ 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
∇</syntaxhighlight>
 
I prefer the DRYness of solutions that don't have "FizzBuzz" as a separate case; here's such a solution that works with both Dyalog and GNU. You may want to prepend `⍬⊣` to the whole thing in GNU to keep it from returning the list as the value of the expression, causing the interpreter to print it out a second time.
 
{{works with|Dyalog APL}}
{{works with|GNU APL}}
<syntaxhighlight lang="apl">⎕io←1
{⎕←∊'Fizz' 'Buzz'⍵/⍨d,⍱/d←0=3 5|⍵}¨⍳100</syntaxhighlight>
 
Explanation:
<pre>
⎕io←1 Set the index origin to 1; if it were set to 0, the next
step would count from 0 to 99 instead of 1 to 100.
 
{ }¨⍳100 Do the thing in braces for each integer from 1 through 100.
 
3 5|⍵ Make a list of the remainders when the current number is
divided by 3 and 5.
 
d←0= Make it a Boolean vector: true for remainder=0, false
otherwise. Name it d.
 
d,⍱/ Prepend d to the result of reducing itself with XNOR,
yielding a three-element Boolean vector. The first element
is true if the number is divisible by 3; the second if it's
divisible by 5; and the third only if it's divisible by
neither.
 
'Fizz' 'Buzz'⍵/⍨ Use the Boolean vector as a mask to select elements from
a new triple consisting of 'Fizz', 'Buzz', and the current
number. Each of the three elements will be included in the
selection only if the corresponding Boolean is true.
 
∊ Combine the selected elements into one vector/string
 
⎕← And print it out.</pre>
 
=={{header|AppleScript}}==
Line 1,102 ⟶ 1,151:
 
100 times }</syntaxhighlight>
 
=={{header|BabyCobol}}==
<syntaxhighlight lang="cobol">
* NB: ANY does not exist in BabyCobol so the elegant
* EVALUATE-based COBOL-style solution is impossible here.
* Note the subtly unbalanced IF/ENDs yet valid END at the end.
IDENTIFICATION DIVISION.
PROGRAM-ID. FIZZBUZZ.
DATA DIVISION.
01 INT PICTURE IS 9(3).
01 REM LIKE INT.
01 TMP LIKE INT.
PROCEDURE DIVISION.
LOOP VARYING INT TO 100
DIVIDE 3 INTO INT GIVING TMP REMAINDER REM
IF REM = 0
THEN DISPLAY "Fizz" WITH NO ADVANCING
DIVIDE 5 INTO INT GIVING TMP REMAINDER REM
IF REM = 0
THEN DISPLAY "Buzz" WITH NO ADVANCING
DIVIDE 15 INTO INT GIVING TMP REMAINDER REM
IF REM = 0
THEN DISPLAY ""
ELSE DISPLAY INT
END.
</syntaxhighlight>
 
=={{header|BaCon}}==
Line 1,369 ⟶ 1,444:
? str
END FOR</syntaxhighlight>
 
=={{header|Bruijn}}==
<syntaxhighlight lang="bruijn">
:import std/Combinator .
:import std/String .
:import std/Number .
 
main [y [[0 =? (+101) case-end case-rec]] (+1)]
case-rec str ++ "\n" ++ (1 ++0)
str fizzbuzz "FizzBuzz" (fizz "Fizz" (buzz "Buzz" (number→string 0)))
fizz =?(0 % (+3))
buzz =?(0 % (+5))
fizzbuzz fizz buzz fizz
case-end empty
</syntaxhighlight>
 
=={{header|C}}==
Line 2,691 ⟶ 2,781:
 
(fizzbuzz 100)</syntaxhighlight>
 
Solution 8:
<syntaxhighlight lang="lisp">(defun range (min max)
(loop
:for x :from min :to max
:collect x))
 
(defun fizzbuzz ()
(map 'nil #'(lambda (n)
(princ
(cond
((zerop (mod n 15)) "FizzBuzz!")
((zerop (mod n 5)) "Buzz!")
((zerop (mod n 3)) "Fizz!")
(t n))
(terpri)))
(range 1 100)))</syntaxhighlight>
 
First 16 lines of output:
<pre>
Line 2,830 ⟶ 2,938:
Eval compute in fizz_buzz.
</syntaxhighlight>
 
Output
 
<pre>
= "1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz"%string
: string
</pre>
 
=={{header|Cowgol}}==
Line 4,389 ⟶ 4,603:
 
[[File:Fōrmulæ - FizzBuzz 01.png]]
 
[[File:Fōrmulæ - FizzBuzz 01a.png]]
 
[[File:Fōrmulæ - FizzBuzz 02.png]]
Line 5,932 ⟶ 6,148:
=={{header|langur}}==
<syntaxhighlight lang="langur">for .i of 100 {
writeln givenswitch(0; .i rem 15: "FizzBuzz"; .i rem 5: "Buzz"; .i rem 3: "Fizz"; .i)
}</syntaxhighlight>
 
{{works with|langur|0.8.1}}
<syntaxhighlight lang="langur">for .i of 100 {
writeln if(.i div 15: "FizzBuzz"; .i div 5: "Buzz"; .i div 3: "Fizz"; .i)
Line 7,462 ⟶ 7,677:
(iota 100))
</syntaxhighlight>
 
=={{header|Onyx (wasm)}}==
<syntaxhighlight lang="C">
use core { * }
 
fizzbuzz :: (len: u32) -> void {
for i in 1..len+1 {
msg : str;
if (i%3 == 0 && i%5 == 0) { msg = "FizzBuzz !!!"; }
elseif (i%3 == 0) { msg = "Fizz"; }
elseif (i%5 == 0) { msg = "Buzz"; }
else { msg = tprintf("{}", i); }
printf("{}\n", msg);
}
}
 
main :: () {
fizzbuzz(100);
}
</syntaxhighlight>
 
=={{header|OOC}}==
<syntaxhighlight lang="ooc">fizz: func (n: Int) -> Bool {
Line 8,952 ⟶ 9,188:
=={{header|Python}}==
===Python2: Simple===
<syntaxhighlight lang="python">for i in xrangerange(1, 101):
if i % 15 == 0:
print "FizzBuzz"
Line 9,318 ⟶ 9,554:
]
]</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <FizzBuzz 1>;
};
 
FizzBuzz {
101 = ;
s.N = <Prout <Item s.N>>
<FizzBuzz <+ 1 s.N>>;
};
 
Item {
s.N, <Mod s.N 15>: 0 = FizzBuzz;
s.N, <Mod s.N 5>: 0 = Buzz;
s.N, <Mod s.N 3>: 0 = Fizz;
s.N = s.N;
};</syntaxhighlight>
 
=={{header|Retro}}==
Line 9,602 ⟶ 9,856:
Whisper my world
 
=={{header|RPG}}==
<nowiki>**</nowiki>free
dcl-s ix Int(5);
for ix = 1 to 100;
select;
when %rem(ix:15) = 0;
dsply 'FizzBuzz';
when %rem(ix:5) = 0;
dsply 'Buzz';
when %rem(ix:3) = 0;
dsply 'Fizz';
other;
dsply (%char(ix));
endsl;
endfor;
 
=={{header|RPL}}==
Line 11,042 ⟶ 11,312:
@ n += 1
end</syntaxhighlight>
 
=={{header|Uiua}}==
<syntaxhighlight lang="Uiua">
⟨⟨⟨&p|&p"Fizz"◌⟩=0◿3.|&p"Buzz"◌⟩=0◿5.|&p"Fizzbuzz"◌⟩=0◿15.+1⇡100
</syntaxhighlight>
 
=={{header|Ursa}}==
Line 12,031 ⟶ 12,306:
# This program has multiple implementations of "fizzbuzz".
 
# usage: ysyamlscript fizzbuzz.ys [<count>] [<fizzbuzz-fn-#>]
 
# The main function runs a certain requested fizzbuzz implementation function
# for a certain requested number (default is 100).
 
defn main(&[count=100 impl]=1):
count =: count || 100
impl =:
str: impl || "1"
fizzbuzz =: "fizzbuzz-$impl"
 
when-not ENV."'YS_TEST"':
say: "Running function '$fizzbuzz' with count=$count"
 
function =: resolve(fizzbuzz.symbol(fizzbuzz))
result =: function(count)
 
Line 12,056 ⟶ 12,328:
defn fizzbuzz-1(n):
map:
fn [(x]):
cond:
zero?(mod(x % 15)): "'FizzBuzz"'
zero?(mod(x % 5)): "'Buzz"'
zero?(mod(x % 3)): "'Fizz"'
=>:else x
=>: (1 .. n)
 
defn fizzbuzz-2(n):
Line 12,068 ⟶ 12,340:
if (i > n):
=>: l
recur inc(i):
inc: i
conj l:
cond:
zero?(mod(i % 15)): "'FizzBuzz"'
zero?(mod(i % 5)): "'Buzz"'
zero?(mod(i % 3)): "'Fizz"'
=>:else i
 
defn fizzbuzz-3(n):
map:
fn [(x]):
s =:
str:
when zero?(mod(x % 3)): "'Fizz"'
when=>: (zero?(mod(x % 5)): "&& 'Buzz"') || nil
if empty?(s): x s
rng: 1, n
</syntaxhighlight>
{{out}}
1

edit