FizzBuzz Solution Dumping Ground

Many have done it, now it’s my turn…

<?php
for ($i=1; $i<=100; $i++) {
    $string = '';
    $string = $i%3==0 ? $string.'Fizz' : $string;
    $string = $i%5==0 ? $string.'Buzz' : $string;
    $string = $string ? $string : $i;
    echo $string.', ';
}
2 Likes

Someone decided (in a fit of self mutilation) to optimize fizzbuzz using assembly: fastest code - High throughput Fizz Buzz - Code Golf Stack Exchange

2 Likes

A late entry for GameMaker, with something they call ‘commenting’ and some ‘color’. Could be made more efficient but I was going for speed and clarity:

//DRAW STEP

var pos_x = 20;
var pos_y = 5;
var mod_y = 12;

//looper
for(var number = 1; number <= 100; number++;)
	{

	//my 3 by 5s
	if( (number mod 3) == 0 && (number mod 5) == 0)
		{ 
		//FizzBuzz in yellow
		draw_set_color(c_yellow);
		draw_text(pos_x,pos_y,"FizzBuzz"); 
		}
	else
		{
		//my 3s
		if( (number mod 3) == 0 ) 
			{
			//Fizz in gray
			draw_set_color(c_gray);
			draw_text(pos_x,pos_y,"Fizz"); 
			}
		else
			{
			//my 5s
			if( (number mod 5) == 0 ) 
				{
				//Buzz in green
				draw_set_color(c_lime);
				draw_text(pos_x,pos_y,"Buzz");
				}	
			else
				{
				//'leftover' numbers
				//Number in white
				draw_set_color(c_white);
				draw_text(pos_x,pos_y,string(number));
				}
					
			}
	
		}
	
	//move down
	pos_y += mod_y;
	
	}
	
2 Likes

I don’t think anyone has used BASIC yet (Oops! Wrong - see message 275, which I can’t link to because I am a new user - Apologies to Mark_Smith1… and Hugh_Fokker post 256. Sigh - and quite a few more*). I looked at the original Dartmouth BASIC (manual here: https://www.dartmouth.edu/basicfifty/basicmanual_1964.pdf ), but I couldn’t easily find an interpreter for the The First Edition from May 1964.

So I used BASIC-256, which was available for my Linux distribution.

*In my defence, Mark_Smith1 used mod, which isn’t in the original Dartmouth BASIC. Hugh_Fokker wrote an emulation of mod using floating point arithmetic.

Note that Dartmouth BASIC does not have a modulo function, so rather than write my own, I used a different technique. Dartmouth BASIC used line numbers, but BASIC-256 does not, so targets of GOTOs and GOSUBs are labels, not line numbers.

Oh, and testing to see if something is zero is dangerous, as numeric variables in Dartmouth BASIC are all floating point. No integers. You can occasionally not get the results you expect. I believe the comparison function was set up with a degree of ‘slop’, something like 0.99999 would be regarded as equal to 1.00001 (!) [The fine manual said that “A number may contain up to 9 digits (the limit of accuracy of the computer), with or without a decimal point, and possibly with a minus sign”].

REM FizzBuzz program
REM Set up Variables
REM X is the Fizz interval
REM Y is the Buzz interval
REM Z is the FizzBuzz interval
LET X = 3
LET Y = 5
LET Z = 15
REM S is the loop initial value
REM E is the loop end value
LET S = 1
LET E = 100
REM O$ is the output string
LET O$ = ""
REM F is the Fizz counter
LET F = X
REM B is the Buzz counter
LET B = Y
REM Z is the FizzBuzz counter
LET Q = Z
REM Main Loop
FOR N = S TO E
LET O$ = N
LET F = F - 1
LET B = B - 1
LET Q = Q - 1
IF F = 0 THEN GOSUB FIZZ
IF B = 0 THEN GOSUB BUZZ
IF Q = 0 THEN GOSUB FIZZBUZZ
PRINT O$
NEXT N
END

FIZZ:
LET O$ = "Fizz"
LET F = X
RETURN

BUZZ:
LET O$ = "Buzz"
LET B = Y
RETURN

FIZZBUZZ:
LET O$= "FizzBuzz"
LET Q = Z
RETURN

If I put in meaningful variable names, it gets easier to interpret (pun not intended):

REM FizzBuzz program
REM Set up Variables
LET ResetFizzCounter = 3
LET ResetBuzzCounter = 5
LET ResetFizzBuzzCounter = 15
LET LoopStart = 1
LET LoopStop = 100
LET OutputString$ = ""
LET FizzCounter = ResetFizzCounter
LET BuzzCounter = ResetBuzzCounter
LET FizzBuzzCounter = ResetFizzBuzzCounter
REM Main Loop
FOR LoopCounter = LoopStart TO LoopStop
LET OutputString$ = LoopCounter
LET FizzCounter = FizzCounter - 1
LET BuzzCounter = BuzzCounter - 1
LET FizzBuzzCounter = FizzBuzzCounter - 1
IF FizzCounter = 0 THEN GOSUB FIZZ
IF BuzzCounter = 0 THEN GOSUB BUZZ
IF FizzBuzzCounter = 0 THEN GOSUB FIZZBUZZ
PRINT OutputString$
NEXT LoopCounter
END

FIZZ:
LET OutputString$ = "Fizz"
LET FizzCounter = ResetFizzCounter
RETURN

BUZZ:
LET OutputString$ = "Buzz"
LET BuzzCounter = ResetBuzzCounter
RETURN

FIZZBUZZ:
LET OutputString$= "FizzBuzz"
LET FizzBuzzCounter = ResetFizzBuzzCounter
RETURN
2 Likes
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)
1 Like
public static void fizzFuzzer(int max) 
			{
				/* this is a 
				*  multi-line comment*/
				
				/* a pattern of numbers.
				 * its actually the difference between
				 * adjcent sorted multiples. their sum is not above max factor i.e 15*/ 
				int [] pattern=new int[]{3,2,1,3,1,2,3};
				/*the fizz and buzz pattern generated with the multiples before sorting*/
				String [] fuzzy=new String[]{"fizz","buzz","fizz","fizz","buzz","fizz","fizzbuzz"};
				/*variable to increment
				*and the max of max multiple
				*/
				int variable=0,floor=max-(max%15);
				
                out: while(variable<max)
					{ /*first loop to prevent if checks for max and thus is fast*/
						for(int indx=0;indx<7;indx++)
							{/*print out*/
							   System.out.println(( variable+=pattern[indx] )+" "+fuzzy[indx]);
							}
                            while(variable>=floor)
							{
								for(int indx=0;indx<7;indx++)
									{
										/*slower here due to if check for max.
										* end code when equal or above max */
									   if(variable>=max)break out;
									   System.out.println(( variable+=pattern[indx] )+" "+fuzzy[indx]);
									}
							}

					}

			}
	}

I want to thank you for the head start on the version that I copied from you!

:grin:

; FILE: fizzbuzz.asm

; Author: David Billsbrough
; influenced by Matt_Giuca version of Feb '07
; Revised: Monday, September 18, 2017 at 09:42:39 AM (EDT)
; another weird fizzbuzz example
;
; =====
; Uh, someone ordered an 86_64 assembly version?

;  (FreeBSD syscalls/NASM/ELF64)

%macro writestr 2
; Argument 1: char* string to write
; Argument 2: length of string
	mov	rax, write                      ; write %1 to stdout
	mov	rdi, stdout
	mov	rsi, %1
	mov	rdx, %2
	syscall
%endmacro

; ========================================================================

section .data

; ========================================================================

section .rodata

FIZZ	db	"Fizz", 0
LENF	equ	4
BUZZ	db	"Buzz", 0
LENB	equ	4
NL	db	10

; ========================================================================

section .bss

index:	resb 1
char:	resb 1
tmp:	resb 1

; ========================================================================

section .text

global _start

_start:

write:		equ	4		; FreeBSD syscall for write
stdout:		equ	1		; standard output file handle
sysexit:	equ	1		; FreeBSD syscall for exit
maxnum:		equ	100		; count to where?

	mov byte [index], 1
forloop:
	cmp byte [index], maxnum
	jbe not_exit
	jmp exit
not_exit:

	; if (i % 3 == 0)
	mov al, [index]
	xor ah, ah
	mov cl, 3
	div cl
	test ah, ah
	jnz trybuzz
	writestr FIZZ, LENF

	; if (i % 5 == 0)
	mov al, [index]
	mov cl, 5
	div cl
	test ah, ah
	jnz not_buzz
	writestr BUZZ, LENB

not_buzz:
	jmp continue
trybuzz:
	; if (i % 5 == 0)
	mov al, [index]
	xor ah, ah
	mov cl, 5
	div cl
	test ah, ah
	jnz printnum

	writestr BUZZ, LENB 
	jmp continue
printnum:
	; Write the number i
	mov al, [index]
	xor ah, ah
	cmp al, 100
	jl tens
	mov cl, 100
	div cl ; ah = al % 100
	add al, '0'
	mov [char], al
	mov [tmp], ah
	writestr char, 1
	mov al, [tmp]
	xor ah, ah
tens:
	mov al, [index]
	cmp al, 10
	jl ones
	mov cl, 10
	div cl ; ah = al % 10
	add al, '0'
	mov [char], al
	mov [tmp], ah
	writestr char, 1
	mov al, [tmp]
	xor ah, ah
ones:
	add al, '0'
	mov [char], al
	writestr char, 1
continue:
	writestr NL, 1
	inc byte [index]
	jmp forloop

exit:
	mov	rax, sysexit		; exit program
	xor	rdi, rdi		; set a zero exit code (I think)
        syscall

; vim: ts=8: nowrap:

; OH my God, that took two ages!!

regards,

David

2 Likes
for(let i = 1; i<101; i++){
    if(i % 3 === 0){
        if(i % 5 === 0){
            console.log("fizzbuzz");
        }
        else{
            console.log("fizz");
        }
    }
    else if(i % 5 === 0) {console.log("Buzz");}
    else{console.log(i)};
}

No Powershell yet?
With use of arrays for the lack of the ternary operator in Powershell (using the behaviour of Powershell returning $null for any non-existing array members, and that the Modulo operator returns 0 which can be used as array index) - tested in PS 5.1.19041.4170 on Windows:

$f = @("Fizz"); $b = @("Buzz")
for ($i = 1; $i -le 100; $i++) { $r = $f[$i % 3]; $r += $b[$i % 5]; if (!$r) { $r = $i }; $r }