Author Topic: FizzBuzz Plus  (Read 8646 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
FizzBuzz Plus
« on: March 31, 2019, 11:03:01 am »
Code: QB64: [Select]
  1. _TITLE "FizzBuzz Plus"
  2. check$ = "0203050711": say$ = "BizzFizzBuzzFuzzWow"
  3. FOR i = 1 TO 100
  4.     Flag = 1
  5.     FOR j = 0 TO 4
  6.         IF i MOD VAL(MID$(check$, j * 2 + 1, 2)) = 0 THEN PRINT MID$(say$, j * 4 + 1, 4);: Flag = 0
  7.     NEXT
  8.     IF Flag THEN PRINT i, ELSE PRINT ,
  9.  
  10.  
  11.  

FellippeHeitor

  • Guest
Re: FizzBuzz Plus
« Reply #1 on: March 31, 2019, 11:09:42 am »
No idea what I'm looking at.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #2 on: March 31, 2019, 11:13:31 am »
Hi Fellippe,

Here is what I watched last night:

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: FizzBuzz Plus
« Reply #3 on: March 31, 2019, 12:33:17 pm »
Pretty amazing, B+

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #4 on: March 31, 2019, 12:41:12 pm »
Thanks _vince,

I had fun inventing a variation involving first few prime numbers and making it generic enough for an easy mod.

FellippeHeitor

  • Guest
Re: FizzBuzz Plus
« Reply #5 on: March 31, 2019, 06:18:57 pm »
Hi Fellippe,

Here is what I watched last night:


Thanks for clarifying!

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: FizzBuzz Plus
« Reply #6 on: February 03, 2021, 07:26:21 pm »
5 variants of FizzBuzz 10^7  by 24.5 sec   and   by 23.5 sec   and  by 26 sec   and  by 24.5 sec    and  by 25.4 sec  from Russia

Code: QB64: [Select]
  1. 'FizzBuzzDaRus.bas    ' 10^7  by 24.5 sec   and   by 23.5 sec   and  by 26 sec   and  by 24.5 sec    and  by 25.4 sec
  2. REDIM i AS LONG 'DEFLNG A-Z ' lines by bplus b+
  3. GOTO 20 '  for variant 2
  4. 'GOTO 30 '  for variant 3
  5. 'GOTO 40 '  for variant 4
  6. 'GOTO 50 '  for variant 5
  7.  
  8. 10 start = TIMER: i = 0 ' 24.5 sec
  9. WHILE i < 10 ^ 7: i = i + 1
  10.     IF i MOD 3 = 0 THEN
  11.         PRINT "Fizz", i + 1, "Buzz", "Fizz", i + 4, i + 5, "Fizz", "Buzz", i + 8, "Fizz", i + 10, i + 11, "FizzBuzz", i + 13,
  12.         i = i + 14
  13.     END IF
  14. PRINT: PRINT TIMER - start
  15.  
  16.  
  17. 20 start = TIMER: i = 0 ' 23.5 sec
  18. 5 i = i + 1
  19. IF i MOD 3 = 0 THEN
  20.     PRINT "Fizz", i + 1, "Buzz", "Fizz", i + 4, i + 5, "Fizz", "Buzz", i + 8, "Fizz", i + 10, i + 11, "FizzBuzz", i + 13,
  21.     i = i + 14
  22. PRINT i,: IF i < 10 ^ 7 THEN GOTO 5
  23. PRINT: PRINT TIMER - start
  24.  
  25.  
  26. 30 start = TIMER ' 26 sec
  27. FOR i = 1 TO 10 ^ 7
  28.     IF i MOD 3 = 0 AND i MOD 5 = 0 THEN a$ = "FizzBuzz" ELSE IF i MOD 3 = 0 THEN a$ = "Buzz" ELSE IF i MOD 5 = 0 THEN a$ = "Fizz" ELSE a$ = STR$(i) 'STR$(i) is slow
  29.     PRINT a$,
  30. PRINT: PRINT TIMER - start
  31.  
  32.  
  33. 40 start = TIMER ' 24.7 sec
  34. FOR i = 1 TO 10 ^ 7
  35.     IF i MOD 3 = 0 AND i MOD 5 = 0 THEN a$ = "FizzBuzz": GOTO 45 ELSE IF i MOD 3 = 0 THEN a$ = "Buzz": GOTO 45 ELSE IF i MOD 5 = 0 THEN a$ = "Fizz": GOTO 45 ELSE a$ = STR$(i) 'STR$(i) is slow
  36.    45 PRINT a$,
  37. PRINT: PRINT TIMER - start
  38.  
  39.  
  40. 50 start = TIMER ' 25.4 sec
  41. FOR i = 1 TO 10 ^ 7
  42.     IF i MOD 3 = 0 AND i MOD 5 = 0 THEN PRINT "FizzBuzz", ELSE IF i MOD 3 = 0 THEN PRINT "Buzz", ELSE IF i MOD 5 = 0 THEN PRINT "Fizz", ELSE PRINT i,
  43. PRINT: PRINT TIMER - start
  44.  

Plus variants with integer constants are slow
« Last Edit: February 04, 2021, 01:00:25 pm by DANILIN »
Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #7 on: February 03, 2021, 09:06:34 pm »
@DANILIN

If you are going for speed (not really the point of FizzBizz, but),

Try adding this line at the top:
Code: QB64: [Select]

and changing the times to single! You will like :)

This doesn't stop on a dime (at 100) but nice!
Code: QB64: [Select]
  1. 20 start = TIMER: i = 0 ' 23.5 sec
  2. 5 i = i + 1
  3. IF i MOD 3 = 0 THEN
  4.     PRINT "Fizz", i + 1, "Buzz", "Fizz", i + 4, i + 5, "Fizz", "Buzz", i + 8, "Fizz", i + 10, i + 11, "FizzBuzz", i + 13,
  5.     i = i + 14
  6. PRINT i,: IF i < 100 THEN GOTO 5
  7. fin = TIMER: PRINT: PRINT fin - start
  8.  
« Last Edit: February 03, 2021, 09:29:55 pm by bplus »

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: FizzBuzz Plus
« Reply #8 on: February 03, 2021, 09:26:10 pm »
adding line became faster exactly 2 times: 13 seconds each

day before I just composed program FizzBuzz as everyone can simple

https://rosettacode.org/wiki/FizzBuzz/Basic

and today I checked acceleration

but classic FizzBuzz program should be on forum

and nothing newer is coming up
« Last Edit: February 03, 2021, 09:34:44 pm by DANILIN »
Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #9 on: February 03, 2021, 09:32:21 pm »
Quote
faster by exactly 2 times: writes whole seconds

you forgot to change times to Single Type, add ! suffix to start and fin

Actually it's better just to
REDIM i as long

then you dont have to fix times.

If you want to compare yours to regular version here's mine:
Code: QB64: [Select]
  1. start = TIMER: i = 0 ' 23.5 sec
  2. 5 i = i + 1
  3. IF i MOD 3 = 0 THEN
  4.     PRINT "Fizz", i + 1, "Buzz", "Fizz", i + 4, i + 5, "Fizz", "Buzz", i + 8, "Fizz", i + 10, i + 11, "FizzBuzz", i + 13,
  5.     i = i + 14
  6. PRINT i,: IF i < 10 ^ 7 THEN GOTO 5
  7. fin = TIMER: PRINT: PRINT fin - start
  8.  
  9. PRINT "Note time of Danilin's version and press any..."
  10.  
  11. start = TIMER
  12. i = 1
  13. WHILE i < 10 ^ 7
  14.     flag = 0
  15.     IF i MOD 3 = 0 AND i MOD 5 = 0 THEN
  16.         PRINT "FizzBuzz"
  17.     ELSEIF i MOD 3 = 0 THEN
  18.         PRINT "Fizz"
  19.     ELSEIF i MOD 5 = 0 THEN
  20.         PRINT "Buzz"
  21.     ELSE
  22.         PRINT i
  23.     END IF
  24.     i = i + 1
  25. PRINT TIMER - start
  26. PRINT "Time of a more regular version of FizzBuzz."
  27.  
« Last Edit: February 03, 2021, 09:49:31 pm by bplus »

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: FizzBuzz Plus
« Reply #10 on: February 03, 2021, 10:26:23 pm »
having found out correctness we will remove all prints:

0.055: Danilin

0.885: Standart

Code: QB64: [Select]
  1. ' fizzbuzzDaRusNOprint.bas
  2. start = TIMER: i = 0 ' 14 sec
  3. 5 i = i + 1
  4. IF i MOD 3 = 0 THEN
  5.     'PRINT "Fizz", i + 1, "Buzz", "Fizz", i + 4, i + 5, "Fizz", "Buzz", i + 8, "Fizz", i + 10, i + 11, "FizzBuzz", i + 13,
  6.     i = i + 14
  7. 'PRINT i,
  8. IF i < 10 ^ 7 THEN GOTO 5
  9. PRINT TIMER - start
  10.      
  11. PRINT "Note time of Danilin's version and press any..."
  12. 'SLEEP
  13.      
  14. start = TIMER
  15. i = 1
  16. WHILE i < 10 ^ 7
  17.     flag = 0
  18.     IF i MOD 3 = 0 AND i MOD 5 = 0 THEN
  19.         'PRINT "FizzBuzz"
  20.     ELSEIF i MOD 3 = 0 THEN
  21.         'PRINT "Fizz"
  22.     ELSEIF i MOD 5 = 0 THEN
  23.         'PRINT "Buzz"
  24.     ELSE
  25.         'PRINT i
  26.     END IF
  27.     i = i + 1
  28. PRINT TIMER - start
  29. PRINT "Time of a more regular version of FizzBuzz."
Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #11 on: February 03, 2021, 11:09:46 pm »
Code: QB64: [Select]
  1. 'PRINT "Fizz", i + 1, "Buzz", "Fizz", i + 4, i + 5, "Fizz", "Buzz", i + 8, "Fizz", i + 10, i + 11, "FizzBuzz", i + 13,
  2.  

No, comparing Apples to Oranges because you are skipping the calculations for these print jobs.

Just take the time difference in the 2 print jobs both have to print the same amount of letters though how I do standard is one per line which is more LF+CR's, but I get about a half a sec which is significant.

Update: ran some more tests and yeah looks more like a 1 sec +/- on my system between the 2 different times printing everything out. So your times of not printing pretty much agree.
« Last Edit: February 03, 2021, 11:48:29 pm by bplus »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: FizzBuzz Plus
« Reply #12 on: February 03, 2021, 11:13:39 pm »
Never heard of FizzBuzz before, so I googled it.  Saw a one line javascript version.  That's probably not possible in basic, but I played around with it, tried to get it down to the fewest lines possible without using a separator.   Eventually got it down to 3, but kind of cheated because it's just one big if/then/else /if/else line
 inside a for/next.

I find it very interesting that companies ask this at coding job interviews.  I would have probably coded a 100 line version, and commented in a 'REM hope you pay per line...
 
- Dav

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: FizzBuzz Plus
« Reply #13 on: February 03, 2021, 11:50:55 pm »
Luv to see that 3 liner Dav ;-))

Can you do: If Then ElseIF ... END IF on one line without separators?

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: FizzBuzz Plus
« Reply #14 on: February 04, 2021, 12:06:45 am »
I didn't save what I had earlier, but I think this was it....

- Dav

Code: QB64: [Select]
  1. FOR f = 1 TO 100
  2.     IF f MOD 15 <> 0 AND f MOD 5 <> 0 AND f MOD 3 <> 0 THEN PRINT STR$(f) ELSE IF f MOD 15 = 0 THEN PRINT "FizzBuzz" ELSE IF f MOD 3 = 0 THEN PRINT "Fizz" ELSE IF f MOD 5 = 0 THEN PRINT "Buzz"

Edit: i guess you could do a big one line, if f = 1 then else if f = 2 then, etc up to 100.  But, id really have to want that job bad...
-
« Last Edit: February 04, 2021, 12:13:20 am by Dav »