![]() |
Forum Index : Microcontroller and PC projects : CMM2: FizzBuzz
Author | Message | ||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 419 |
One of my students introduced a class to FizzBuzz (via Python - where this is apparently a "thing"). Anyway, the "challenge" is to code something in MMBasic (via CMM2 for us) that prints the numbers from 1 to 100 and replaces the number with Fizz for multiples of 3 and Buzz for multiples of 5. 15 will therefore get FizzBuzz as it is both. Currently this is my example code: For i = 1 To 100 T= i Mod 3 F = i Mod 5 If T = 0 Then Print "Fizz"; If F = 0 Then Print "Buzz"; If T<>0 And F<>0 Then Print I; Next i Any variants? Can it be simpler (ie less code, less If statements)? My class have been challenged to drop few my way this week.. Nim Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 419 |
Student #1 For i = 1 To 100 s$="" If i Mod 3 = 0 Then s$ = s$ + "Fizz" If i Mod 5 = 0 Then s$ = s$ + "Buzz" If Len(s$) > 0 Then Print s$ Else Print i End If Next i Different - but still 3 IFs (actually and else - so does that count as 4?) Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 419 |
Student #2 (am sure this is via the Internet..) dim fb$(100) for x=1 to 100 fb$(x)=str$(x) next x for x=3 to 100 step 3 fb$(x)="Fizz" next x for x=5 to 100 step 5 fb$(x)="Buzz" next x for x=3*5 to 100 step 3*5 fb$(x)="Fizz"+"Buzz" next x for x=1 to 100 Print fb$(x) next x Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 419 |
Am going to extend to 1 to 1,000,000 and get some timings --- interested to know if the variant without the if's is quicker.... Nim Entropy is not what it used to be |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4298 |
Not what I would write it I wanted someone else to maintain it: For i = 1 To 100 t = i Mod 3 f = i Mod 5 If Not t Then ? "Fizz"; If Not f Then ? "Buzz"; If t * f Then ? i; Next Tom Edited 2020-11-25 02:51 by thwill MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10180 |
In Verion 5.06.00b4 onwards ![]() for i=1 to 100 print choice(i mod 15=0,"FizzBuzz",choice( i mod 3=0,"Fizz",choice(i mod 5=0,"Buzz",i))) next i |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4298 |
Untested, but with latest firmware this might do it too: For i = 1 To 100 Print Choice(i Mod 3, Choice(i Mod 5, Str$(i), "Buzz"), Choice(i Mod 5, "Fizz", "FizzBuzz")) Next Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 419 |
BEst.excuse.ever to update to the latest firmware. Love it. Diolch. Nim Entropy is not what it used to be |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |