FizzBuzz: Difference between revisions

3,833 bytes added ,  22 days ago
(+BabyCobol)
 
(11 intermediate revisions by 9 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,106 ⟶ 1,155:
<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 ENDsEND at the end.
IDENTIFICATION DIVISION.
PROGRAM-ID. FIZZ BUZZFIZZBUZZ.
DATA DIVISION.
01 INT PICTURE IS 9(3).
Line 1,115 ⟶ 1,164:
01 TMP LIKE INT.
PROCEDURE DIVISION.
LOOP VARYING IINT TO 100
DIVIDE INT3 INTO 3INT GIVING TMP REMAINDER REM
IF REM = 0
THEN DISPLAY "Fizz" WITH NO ADVANCING
ENDDIVIDE 5 INTO INT GIVING TMP REMAINDER REM
DIVIDE INT INTO 5 GIVING TMP REMAINDER REM
IF REM = 0
THEN DISPLAY "Buzz" WITH NO ADVANCING
ENDDIVIDE 15 INTO INT GIVING TMP REMAINDER REM
DIVIDE INT INTO 15 GIVING TMP REMAINDER REM
IF REM = 0
THEN DISPLAY ""
Line 2,734 ⟶ 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 4,538 ⟶ 4,603:
 
[[File:Fōrmulæ - FizzBuzz 01.png]]
 
[[File:Fōrmulæ - FizzBuzz 01a.png]]
 
[[File:Fōrmulæ - FizzBuzz 02.png]]
Line 6,081 ⟶ 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,611 ⟶ 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 9,101 ⟶ 9,188:
=={{header|Python}}==
===Python2: Simple===
<syntaxhighlight lang="python">for i in xrangerange(1, 101):
if i % 15 == 0:
print "FizzBuzz"
Line 9,769 ⟶ 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,209 ⟶ 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}}==
1

edit