In Crystal, which is kind of like a compiled ruby-esque language:
Range.new(1, 100).each do |n|
msg = ""
if n.modulo(3) == 0
msg = msg + "Fizz"
end
if n.modulo(5) == 0
msg = msg + "Buzz"
end
if msg == ""
puts n
else
puts msg
end
end
Not perfect but I just started learning the language, and the language itself isn’t production-ready.
Haven’t seen a GAS solution yet. Well, it’s mostly just JS, but I can cheat a bit.
My first attempt just used Logger.log() and was kind of direct brute force, but worked. Though technically, I failed since, in retrospect, the Logger method doesn’t really satisfy ‘print’ from an end user perspective and the Logger ended up string converting i with 1 decimal place. Took about 2 min.
function fizzBuzz() {
for (var i=1;i<=100;i++){
if (i%3 == 0 && i%5 == 0){
Logger.log('FizzBuzz');
} else if (i%3 == 0) {
Logger.log('Fizz');
} else if (i%5 == 0) {
Logger.log('Buzz');
} else {
Logger.log(i);
}
}
}
Realizing I technically failed, took an extra 5 to optimise a touch while still having it legible and output as a web app. With GAS, you can kind of cheat and provide partial HTML. It will fill in any headers and such that you’re missing, so the client side works.
Here’s the final:
function doGet() {
var str, res = '';
for (var i=1;i<=100;i++){
if (i % 3 == 0) str += 'Fizz';
if (i % 5 == 0) str += 'Buzz';
if (!str) str = i;
res += str + '<br>';
str = '';
}
return HtmlService.createHtmlOutput(res);
}
I’m not a programmer by trade, but I do dabble in a lot of GAS (which leads to client side JS, jQuery, html, css, etc.), and (thankfully much less frequently) VBA stuff related to my job. Fairly happy with 7 minutes for a working web app that meets the goal while not technically being a programmer. And no, not cheating. Replace exec in the url with edit to see the script.
Frankly shocking to read a lot of these from people who are programmers and missed some of the basic requirements.
Just the tiniest bit disappointed to find only one PowerShell solution posted. Does no-one respect this language?
In any case, my own PS piece is a bit different so I’ll share for SnG:
fizzBuzzPrint(A):- A mod 15 =:= 0,display(‘FizzBuzz\n’).
fizzBuzzPrint(A):- A mod 5 =:= 0,display(‘Buzz\n’).
fizzBuzzPrint(A):- A mod 3 =:= 0,display(‘Fizz\n’).
fizzBuzzPrint(A):- display(A), display(’\n’).
fizzBuzz(B,B):- fizzBuzzPrint(B).
fizzBuzz(A,B):- fizzBuzzPrint(A), C is A+1, fizzBuzz(C,B),!.
PHP?
Didn’t program for ages, but heck, don’t understand why this should be difficult.
I prefer readability over short code…
I know 1) recursion takes a lot of heap, but I simply like it.
I know 2) yep, type-checking is not PHP strongest point, but heck, sometimes useful, see below…
<?php
function FizzBuzz ($n) {
// Define the array with dividers and what to shout
$match = array (3 => "Fizz", 5 => "Buzz", 7 => "Woof", 11 => "Waf" ) ;
if ($n>0) {
// Recursively call FizzBuzz when $n > 0
FizzBuzz($n-1) ;
// $fb defines what to shout
$fb="" ;
// Cycle through the $match array and if we found a match add it to the output.
foreach ( $match as $div => $shout) if ($n%$div == 0) $fb .= $shout ;
$fb = ($fb=="") ? $n : $fb ; // If there is nothing to shout show the number
echo $fb."\n" ;
// FizzBuzz($n-1) ; // Put FizzBuzz here if you want it in reverse order
}
}
FizzBuzz (100) ;
?>
It’s been a while since I’ve used python but you seem to have confused return and print. I don’t think this will output anything outside of the interpreter.
Unless I’m missing something this is really easy, I didn’t think about it very hard.
using System;
namespace fizzbuzz {
class Program {
static void Main(string[] args) {
for (int i = 1; i < 101; i++) {
if (i % 3 == 0) Console.Write("Fizz");
if (i % 5 == 0) Console.Write("Buzz");
if (i % 3 != 0 && i % 5 != 0) Console.Write(i);
Console.WriteLine("");
}
Console.Read();
}
}
}
Just ran across this article… So, I decided to share the scary one. My PAINFUL submission is:
Applesoft Basic
Yes, you heard that right! I am writing this in APPLESOFT BASIC.
10 I = 0
15 E = 100
20 I = I + 1
30 IF ((I/15) >< INT(I/15)) THEN 40
35 PRINT "FizzBuzz"
38 IF I < E THEN 20
39 IF I = E THEN 80
40 IF ((I/5) >< INT(I/5)) THEN 50
45 PRINT "Buzz"
48 IF I < E THEN 20
49 IF I = E THEN 80
50 IF ((I/3) >< INT(I/3)) THEN 60
55 PRINT "Fizz"
58 IF I < E THEN 20
59 IF I = E THEN 80
60 Print I
70 IF I < E THEN 20
80 END
To show the fizzBuzz you should write the if (((i % 5) == 0) && ((i % 3) == 0)) at the top of the conditions list.
otherwise, it will not display the fizzbuzz, that’s logic
C# code:
class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 100; i++) {
if (((i % 5) == 0) && ((i % 3) == 0))
{
Console.WriteLine("fizz Wizz");
}
else if ((i % 3) == 0)
{
Console.WriteLine("fizz");
}
else if ((i % 5) == 0)
{
Console.WriteLine("Wizz");
}
else
Console.WriteLine(i);
}
Console.ReadLine();
}
}
for i in range(1, 101):
if (i % 3) == 0 and (i % 5) == 0:
print('FizzBuzz')
elif (i % 3) == 0:
print('Fizz')
elif (i % 5) == 0:
print('Buzz')
else:
print(i)
func fizzbuzzList(n int) []string {
var res []string
for i := 1; i <= n; i++ {
switch {
case i%15 == 0:
res = append(res, `fizzbuzz`)
case i%5 == 0:
res = append(res, `buzz`)
case i%3 == 0:
res = append(res, `fizz`)
default:
res = append(res, strconv.Itoa(i))
}
}
return res
}
And in a lazy manner (Future? Continuation?):
func fizzbuzzLazy(n int) chan string {
var res = make(chan string)
go func() {
for i := 1; i <= n; i++ {
switch {
case i%15 == 0:
res <- `fizzbuzz`
case i%5 == 0:
res <- `buzz`
case i%3 == 0:
res <- `fizz`
default:
res <- strconv.Itoa(i)
}
}
close(res)
}()
return res
}