FizzBuzz: Difference between revisions

3,082 bytes added ,  1 month ago
(Add ABC)
(7 intermediate revisions by 5 users not shown)
Line 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 2,746 ⟶ 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,550 ⟶ 4,603:
 
[[File:Fōrmulæ - FizzBuzz 01.png]]
 
[[File:Fōrmulæ - FizzBuzz 01a.png]]
 
[[File:Fōrmulæ - FizzBuzz 02.png]]
Line 9,133 ⟶ 9,188:
=={{header|Python}}==
===Python2: Simple===
<syntaxhighlight lang="python">for i in xrangerange(1, 101):
if i % 15 == 0:
print "FizzBuzz"
Line 9,801 ⟶ 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,241 ⟶ 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