Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

90% of /prog/ can't write FizzBuzz

Name: The antagonist 2008-04-25 12:38

Prove me wrong

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

Name: Anonymous 2008-04-25 18:14

esle

Name: Anonymous 2008-04-25 18:14

#!/usr/local/bin/ploki
LET i += 1
IF i > 100
    END
END IF
WUNT \AUSG @FIZZBUZZi
IF
FOR FIZZBUZZ LEET r
    IF \@ % 3 = 0
        LET r "Fizz"
    FI
    IF \@ % 5 = 0
        LET r _= "Buzz"
    FI
    IF @NOT r
        LET r \@
    FI
KTHX r

Name: Anonymous 2008-04-25 18:36

UM LOL C
void fizzbuzz()
{
for(int x=1;x<=100;x++)
{
if(x%3==0) {printf("FIZZ\n");}
else if(x%5==0) {printf("BUZZ\n");}
else if(x%15=0) {printf("FIZZBUZZ\n");}
else {printf("%i",x);}
}
}
too lazy to format. simple shit yo

Name: Anonymous 2008-04-25 18:38

Sage for FizzBuzz. Sure, I've met "Programmers" who couldn't code it, but their numbers are few.
The problem's real nature lies in making people who read it think "Hey, I could solve that!" and feel good about themselves. They then feel somehow compelled to post their own solution, to show that "Hello, I'm anonymous, and I am superior because I can solve a problem any eight-year old who've ever sat down at a BASIC prompt can handle."

Clever obfuscations or circumlocutions are still encouraged.

Name: Anonymous 2008-04-25 18:40

[\$@$@\/*-0=]d:1[$100>~][0\$3d;!["Fizz"\%1\]?$5d;!["Buzz"\%1\]?\0=[$.]?1+]#

Name: Anonymous 2008-04-25 18:41

>>43
I refuse to be trolled.

Name: Anonymous 2008-04-25 18:44

>>46
damn

Name: Anonymous 2008-04-25 19:34

Hurr, I remember the thread like this we had a long time ago. Here's my old contribution: fizzbuzz in a pure functional language --

#include <string>
#include <sstream>
#include <iostream>

template < class T >
std::string lexical_cast( T val ) {
        std::string ret;
        std::stringstream ss;
        ss << val;
        ss >> ret;
        return ret;
}
       
template < int n >
struct Fizz {
        static const char* f;
};

template <int n> const char* Fizz<n>::f = "";
               
template <>
struct Fizz<0> {
        static const char* f;
};

template <> const char* Fizz<0>::f = " fizz";
                       
template < int n >
struct Buzz {
        static const char* b;
};

template <int n> const char* Buzz<n>::b = "";
                               
template <>
struct Buzz<0> {
        static const char* b;
};

template <> const char* Buzz<0>::b = " buzz";

template < int n >
struct FizzBuzz : Fizz< n % 5 >, Buzz< n % 3 >
{
        static const std::string s;
};

template <>
struct FizzBuzz<0>
{
        static const std::string s;
};

template < int n >
const std::string FizzBuzz<n>::s =
        FizzBuzz< n - 1 >::s + "\n" + lexical_cast(n) + Fizz< n % 5 >::f + Buzz< n % 3 >::b;

template <>
const std::string FizzBuzz<0>::s = "";

int main( int argc, char* argv[] ) {
        std::cout << FizzBuzz<100>::s << std::endl;
}

Name: Anonymous 2008-04-25 20:38

>>43
I don't refuse to be trolled.

Your implementation does not work. 15 prints "FIZZ" not "FIZZBUZZ".

Name: Anonymous 2008-04-25 21:15

Other problems:
if(x%15=0)
and there's no \n after printing a number.

Name: Anonymous 2008-04-25 21:20

#include <stdio.h>
int main() {
 int i;
 for(i=1;i<=100;i++)
  if(i%3 && i%5)
   printf("%d\n",i);
  else
   printf("%s%s\n",!(i%3)?"Fizz":"",!(i%5)?"Buzz":"");
}

Name: Anonymous 2008-04-25 21:22

>>51
Gross. You're testing your conditions too many times.

Name: Anonymous 2008-04-25 21:31

OMG OPTIMIZED
for(i=1;i<100;) {
 printf("%d\n",i++);
 printf("%d\n",i++);
 printf("Fizz\n"); i++;
 printf("%d\n",i++);
 printf("Buzz\n"); i++;
 printf("Fizz\n"); i++;
 printf("%d\n",i++);
 printf("%d\n",i++);
 printf("Fizz\n"); i++;
 printf("Buzz\n"); i++;
 printf("%d\n",i++);
 printf("Fizz\n"); i++;
 printf("%d\n",i++);
 printf("%d\n",i++);
 printf("FizzBuzz\n"); i++;
}

Name: Anonymous 2008-04-25 21:40

>>53
-funroll-loops

Name: Anonymous 2008-04-25 22:04

It's shocking how many failed to read the spec properly.

Name: Anonymous 2008-04-25 22:10

>>55
What do you mean?

Name: Anonymous 2008-04-25 22:26

f, b = lambda x: not(x%3) and 'Fizz', lambda x: not(x%5) and 'Buzz'
print('\n'.join(''.join((f(x) or '',b(x) or '')) or str(x) for x in range(1,101)))

Name: Anonymous 2008-04-25 22:57

>>53

printf("Fizz\n"); i++;
printf("Buzz\n"); i++;

Wrong. Better luck next time, kid.

Name: Anonymous 2008-04-25 23:01

>>58
Nothing wrong there, chief.

Name: Anonymous 2008-04-25 23:26

[code]
FIZBUZ LDA #0
       STA DIV3
       STA DIV5
       STA FLAG
       TAX
NEXT   LDA #13
       JSR CHROUT
       INX
       CMP #101
       BEQ DONE
       INC DIV3
       LDA DIV3
       CMP #2
       BNE SKPFIZ
       JSR FIZZ
SKPFIZ INC DIV5
       CMP #4
       JSR SKPBUZ
       JSR BUZZ
SKPBUZ LDA FLAG
       BEQ NEXT
       TXA
       JSR NUMOUT
       LDA #0
       STA FLAG
       JMP NEXT
DONE   RTS

FIZZ   LDA >FIZTXT
       STA >STRPTR
       LDA <FIZTXT
       STA <STRPTR
MERGE  JSR STROUT
SETFLG LDA #$FF
       STA FLAG
       RTS

BUZZ   LDA >BUZTXT
       STA >STRPTR
       LDA <BUZTXT
       STA <STRPTR
       JMP MERGE

STROUT LDY #0
STROUL LDA (STRPTR),Y
       BEQ STROUX
       JSR CHROUT
       INY
       JMP STROUL
STROUX RTS

NUMOUT BNE NOT0
       LDA #'0'
       JMP CHROUT
NOT0   STX TEMP
       PHA
       LDY #0
LH     CMP #100
       BCS TENS      
       SEC
       SBC #100
       JMP LH
TENS   TYA
       BEQ H0
       CLC
       ADC #'0'
       JSR CHROUT
H0     LDY #0
LT     CMP #10
       BCS ONES
       SEC
       SBC #10
       JMP LT
ONES   PHA
       TYA
       BEQ T0
       CLC
       ADC #'0'
       JSR CHROUT
T0     PLA
       ADC #'0'
       JSR CHROUT
       LDA #13
       JSR CHROUT
       LDX TEMP
       RTS
;REPLACE WITH A CHARACTER OUTPUT ROUTINE
CHROUT PHP
       PHA
       TXA
       PHA
       TYA
       PHA
       JMP $FFD2
       PLA
       TAY
       PLA
       TAX
       PLA
       PLP
       RTS

FIZTXT .ASC "FIZZ"
       .DB 0
BUZTXT .ASC " BUZZ"
       .DB 0

DIV3   .DB 0
DIV5   .DB 0
FLAG   .DB 0
STRPTR .DW 0
TEMP   .DB 0

Name: Anonymous 2008-04-25 23:27

whoops

FIZBUZ LDA #0
       STA DIV3
       STA DIV5
       STA FLAG
       TAX
NEXT   LDA #13
       JSR CHROUT
       INX
       CMP #101
       BEQ DONE
       INC DIV3
       LDA DIV3
       CMP #2
       BNE SKPFIZ
       JSR FIZZ
SKPFIZ INC DIV5
       CMP #4
       JSR SKPBUZ
       JSR BUZZ
SKPBUZ LDA FLAG
       BEQ NEXT
       TXA
       JSR NUMOUT
       LDA #0
       STA FLAG
       JMP NEXT
DONE   RTS

FIZZ   LDA >FIZTXT
       STA >STRPTR
       LDA <FIZTXT
       STA <STRPTR
MERGE  JSR STROUT
SETFLG LDA #$FF
       STA FLAG
       RTS

BUZZ   LDA >BUZTXT
       STA >STRPTR
       LDA <BUZTXT
       STA <STRPTR
       JMP MERGE

STROUT LDY #0
STROUL LDA (STRPTR),Y
       BEQ STROUX
       JSR CHROUT
       INY
       JMP STROUL
STROUX RTS

NUMOUT BNE NOT0
       LDA #'0'
       JMP CHROUT
NOT0   STX TEMP
       PHA
       LDY #0
LH     CMP #100
       BCS TENS     
       SEC
       SBC #100
       JMP LH
TENS   TYA
       BEQ H0
       CLC
       ADC #'0'
       JSR CHROUT
H0     LDY #0
LT     CMP #10
       BCS ONES
       SEC
       SBC #10
       JMP LT
ONES   PHA
       TYA
       BEQ T0
       CLC
       ADC #'0'
       JSR CHROUT
T0     PLA
       ADC #'0'
       JSR CHROUT
       LDA #13
       JSR CHROUT
       LDX TEMP
       RTS
;REPLACE WITH A CHARACTER OUTPUT ROUTINE
CHROUT PHP
       PHA
       TXA
       PHA
       TYA
       PHA
       JMP $FFD2
       PLA
       TAY
       PLA
       TAX
       PLA
       PLP
       RTS

FIZTXT .ASC "FIZZ"
       .DB 0
BUZTXT .ASC " BUZZ"
       .DB 0

DIV3   .DB 0
DIV5   .DB 0
FLAG   .DB 0
STRPTR .DW 0
TEMP   .DB 0

Name: Anonymous 2008-04-25 23:28

I haven't finished reading SICP so this might not be the most idiomatic scheme code, but:
(define (fizzbuzz)
    (letrec ((fizzbuzz-helper
        (lambda (x) ((lambda (f y) (display (f y)) (newline) (if (not (= 1 y)) (fizzbuzz-helper (- y 1))))
            (lambda (z) (if (= (mod z 3) 0) (if (= (mod z 5) 0) "FizzBuzz" "Fizz") (if (= (mod z 5) 0) "Buzz" z))) x))))
    (fizzbuzz-helper 100)))

Name: Anonymous 2008-04-25 23:31

>>61
SYS 64738
WHERE IS YOUR GODMODORE NOW?

Name: Anonymous 2008-04-25 23:34

(mod x1 x2) is R6RS and (modulo x2 x2) is R5RS what the fuck.
those using non r6rs compilers (define mod modulo) i guess

Name: Anonymous 2008-04-25 23:50

>>63

DIEROM SEI
       LDA #0
       STA 0
HALT   JMP HALT
       .ASC "OMG WHERE IS YOUR KERNAL RESET ROUTINE NOW?"

Name: Anonymous 2008-04-26 0:47


(defun fizzbuzz ()
  (format t "~{~a ~}"
          (loop for i from 1 upto 100
             when (= (mod i 15) 0) collect "FizzBuzz"
             when (= (mod i 3)  0) collect "Fizz"
             when (= (mod i 5)  0) collect "Buzz"
             else collect i)))

Or in my custom Lisp where fizzbuzz is a builtin:

(fizzbuzz)

Name: Anonymous 2008-04-26 1:43

The fun thing about FizzBuzz (other than writing obfuscated versions) is to write versions that seem correct but aren't, present them to others and see if they spot the mistake.

#include <stdio.h>

int main() {
    int i, f, b;
    for(i=1;i<=100;i++) {
        if((f = !(i%3)) || (b = !(i%5)))
            printf("%s%s", f ? "Fizz" : "", b ? "Buzz" : "");
        else
            printf("%d", i);
        printf("\n");
    }
    return 0;
}


MD5 (solution) = 78a1dc71c6d126a040c7da4f006f717c

Name: Anonymous 2008-04-26 2:20

>>67
Apart from newlines, I don't see anything wrong with it

Name: Anonymous 2008-04-26 2:33

>>68
Run it.

Name: Anonymous 2008-04-26 2:43

>>69
d:\>gcc -o go go.c

d:\>go
1
2
Fizz
4
Buzz
FizzBuzz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
16
17
Fizz
19
Buzz
FizzBuzz
22
23
Fizz
Buzz
26
Fizz
28
29
Fizz
31
32
Fizz
34
Buzz
FizzBuzz
37
38
Fizz
Buzz
41
Fizz
43
44
Fizz
46
47
Fizz
49
Buzz
FizzBuzz
52
53
Fizz
Buzz
56
Fizz
58
59
Fizz
61
62
Fizz
64
Buzz
FizzBuzz
67
68
Fizz
Buzz
71
Fizz
73
74
Fizz
76
77
Fizz
79
Buzz
FizzBuzz
82
83
Fizz
Buzz
86
Fizz
88
89
Fizz
91
92
Fizz
94
Buzz
FizzBuzz
97
98
Fizz
Buzz

d:\>

Name: 67 2008-04-26 2:49

>>70
6 says “FizzBuzz,” but 6 is not a multiple of 5. Why does this happen?

Name: Anonymous 2008-04-26 3:19

>>71
Short-circuiting. The first part of the condition is true, so the second is not evaluated, and b remains true. Clever.

Name: Anonymous 2008-04-26 3:19

>>71
I'm going to take a stab at it:
in
if((f = !(i%3)) || (b = !(i%5)))
when the condition is evaluated, if the first one is true then the condition is true so the second isn't evaluated.  So b doesn't change from its state from iteration 5.

if you write instead:
#include <stdio.h>

int main() {
    int i, f, b;
    for(i=1;i<=100;i++) {
        f = !(i%3);
        b = !(i%5);
        if (f || b) {
            printf("%s%s", f ? "Fizz" : "", b ? "Buzz" : "");
        } else {
            printf("%d", i);
        }
        printf("\n");
    }
    return 0;
}

both statements are should be evaluated and it seems to work correctly.

Name: Anonymous 2008-04-26 3:19

>>72
Son of a bitch.

Name: Anonymous 2008-04-26 3:35

[quote]if((f = !(i%3))[/quote][quote]||[/quote] (b = !(i%5)))[/quote]

That's not funny. My dog died like that.

Name: Anonymous 2008-04-26 4:21

>>71
if it's a multiple of 3 then the (b = !(i%5)) doesn't run.

print"Fizz"x!($_%3).Buzz x/0|5$/||$_,$/for 1..100

1[$101\>][$$[$2>][3-]#\[$4>][5-]#$@$@*[@$.@@]?~["Fizz"]?~["Buzz"]?"
"1+]#%


100 [ 1+ dup 5 mod zero? over 3 mod zero? 2dup or not [ 2drop . ] [ [ "Fizz" write ] when [ "Buzz" write ] when drop "" print ] if ] each

Name: Anonymous 2008-04-26 4:28

wait.... this is hard? whut?
i didn't realize /prog/ was the programmers kindergarten

Name: Anonymous 2008-04-26 4:37

>>77
no one here knows how to program, we all just repeat catchphrases

Name: Anonymous 2008-04-26 4:51

>>75
Terrible !

Name: Anonymous 2008-04-26 5:14

>>17
At first I was like ``hey thanks for the helpful advice'', but then I noticed your name and I burst with laugh. Damn you, Patrick.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List