QB64.org Forum

Active Forums => Programs => Topic started by: bplus on March 31, 2019, 11:03:01 am

Title: FizzBuzz Plus
Post by: bplus 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.  
Title: Re: FizzBuzz Plus
Post by: FellippeHeitor on March 31, 2019, 11:09:42 am
No idea what I'm looking at.
Title: Re: FizzBuzz Plus
Post by: bplus on March 31, 2019, 11:13:31 am
Hi Fellippe,

Here is what I watched last night:
Title: Re: FizzBuzz Plus
Post by: _vince on March 31, 2019, 12:33:17 pm
Pretty amazing, B+
Title: Re: FizzBuzz Plus
Post by: bplus 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.
Title: Re: FizzBuzz Plus
Post by: FellippeHeitor on March 31, 2019, 06:18:57 pm
Hi Fellippe,

Here is what I watched last night:


Thanks for clarifying!
Title: Re: FizzBuzz Plus
Post by: DANILIN 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
Title: Re: FizzBuzz Plus
Post by: bplus 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.  
Title: Re: FizzBuzz Plus
Post by: DANILIN 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
Title: Re: FizzBuzz Plus
Post by: bplus 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.  
Title: Re: FizzBuzz Plus
Post by: DANILIN 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."
Title: Re: FizzBuzz Plus
Post by: bplus 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.
Title: Re: FizzBuzz Plus
Post by: Dav 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
Title: Re: FizzBuzz Plus
Post by: bplus 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?
Title: Re: FizzBuzz Plus
Post by: Dav 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...
-
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 12:25:04 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...
-

Oh nice! I didn't think you could do ElseIf's on one line, good to know.

That code works without the first test mod 15 <> 0 because the AND of next 2 cover it mod 5 <> 0 AND mod 3 <> 0.

Update: I looked back and Danilin did try a version of Dav code but he kept putting in an extra line.
Title: Re: FizzBuzz Plus
Post by: STxAxTIC on February 04, 2021, 12:27:05 am
How about doing it with the fewest possible characters? Lines are too ambiguous now.
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 12:37:15 am
How about doing it with the fewest possible characters? Lines are too ambiguous now.

Hey is this a spontaneous jam? LOL

Yeah let's see who can do it in least bytes as read in Windows properties or Linux version of that (could there be a discrepancy between the 2? and/or mac.) I am thinking CR+LF versus just LF.

Lines are OK as long as colon isn't allowed, I think.
Title: Re: FizzBuzz Plus
Post by: SMcNeill on February 04, 2021, 12:37:44 am
Here's an odd little experimental version:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. start## = TIMER
  3. DIM answer(10 ^ 7) AS STRING
  4. FOR i& = 3 TO 10 ^ 7 STEP 3: answer(i&) = "Fizz": NEXT
  5. FOR i& = 5 TO 10 ^ 7 STEP 5: answer(i&) = answer(i&) + "Buzz": NEXT
  6. FOR i& = 10 ^ 7 - _HEIGHT * 10 TO 10 ^ 7
  7.     IF LEN(answer(i&)) THEN PRINT answer(i&); ","; ELSE PRINT STR$(i&); ",";
  8. PRINT: PRINT TIMER - start##

Less than a second for the 10^7 test that Danlin was running above, and now we have our fizzes and buzzes all stored in a nice little array for future reference.
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 12:49:09 am
I tried a neater comma between and oddly it takes a tad longer, also am not seeing any time difference with
$Checking:Off in fact one run was faster without it.

Why is that so fast?
 

Update: Ah ha! No decisions!
Title: Re: FizzBuzz Plus
Post by: SMcNeill on February 04, 2021, 01:12:02 am
I tried a neater comma between and oddly it takes a tad longer, also am not seeing any time difference with
$Checking:Off in fact one run was faster without it.

Why is that so fast?
 

Update: Ah ha! No decisions!

I cheated...  Look close, and you'll see my NINJA trick!!  If you can't figure it out by morning, I'll post it for you, but I know you like a chance to study and figure things out for yourself first.  ;)
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 01:58:54 am
I cheated...  Look close, and you'll see my NINJA trick!!  If you can't figure it out by morning, I'll post it for you, but I know you like a chance to study and figure things out for yourself first.  ;)

Ohhhh you only printed the final screen LOL!
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 02:10:05 am
OK for the least Bytes I have Dav's with some fixing help from me:
Code: QB64: [Select]
  1. FOR f = 1 TO 100
  2.     IF f MOD 5 <> 0 AND f MOD 3 <> 0 THEN PRINT 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"
  3.  
194 bytes in 3 lines.

bplus after learning that Else If can be used on one line as long as you divide Else with If:
Code: QB64: [Select]
  1. FOR i = 1 TO 100
  2.     IF i MOD 3 = 0 AND i MOD 5 = 0 THEN PRINT "FizzBuzz" ELSE IF i MOD 3 = 0 THEN PRINT "Fizz" ELSE IF i MOD 5 = 0 THEN PRINT "Buzz" ELSE PRINT i
  3.  
171 bytes also 3 lines.

Wait if we are doing bytes, colons for line statement separators don't matter? but I don't think there is savings for the 3 liners above.

Edit: I did not need the first line I had.
Anyway here is another bplus submission, a little unusual because it's from his SB Interpreter:
Code: [Select]
[
n i i+1
i i > 100
x
f
i i%5 = 0 and i%3 = 0
, FizzBuzz
e
i i%3 = 0
, Fizz
e
i i%5 = 0
, Buzz
e
, i
f
f
f
]
126 bytes and looks like about 19 lines!

 







Title: Re: FizzBuzz Plus
Post by: SMcNeill on February 04, 2021, 02:19:57 am
Ohhhh you only printed the final screen LOL!

Since these are interview type questions, those are my interview type answers.  It tells them a bit about me, and helps me learn a bit about them.

A boss who looks over that and says, “You didn’t print results from 1 to 10 ^ 7”, without asking me WHY, is one I don’t want to work for.  They’re not into creative solutions, original thinking, or individual problem solving — they just want a monkey that can code up to perfect specifications.  It’s too high stress of a job for me, so, “NO THANKS!”

Now, if he asks why I chose such an approach, I can tell him:

“Our view port is limited by printing to the monitor, so there’s no reason to just run and scroll the screen excessively.  Instead, I printed a whole screen of results, stored the solutions in an array, improved execution times, and now have access to any portion of that data that you’d like to view and work with, on hand.”

I not only completed his task, but I *improved* upon it.

Which in some companies will get you promoted.  In others, it’ll get you fired.

Luckily for me, I don’t want to work for those second BLEEPERS!
Title: Re: FizzBuzz Plus
Post by: STxAxTIC on February 04, 2021, 02:22:51 am
While we're showing off interpreters, here's an example I had kicking around. By no means the smallest code. Kindof a lot of formatting going on that doesn't need to happen:

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 02:38:32 am
Now if you want to compare how much time it took for me to write that 126 byte program...  ;(

STx did yours take longer than an QB64 program? maybe you stay current with yours.

Title: Re: FizzBuzz Plus
Post by: STxAxTIC on February 04, 2021, 02:42:03 am
I doubt any version of sxipt will be faster than qb64 - although the particular implementation we're seeing above is javascript (obviously I guess), which has zero compile time. So in a way, it's quite faster... Meaning, the workflow is faster. Make a tweak, you get the result instantly.
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 02:52:08 am
Oh you know what? I bet I could setup Notepad++ to run my code through SB.exe all it needs is the filename to Run; that would beat editing and then drag and drop onto SB.exe. I just recently loaded the NppExec extension for editing and running JB code.
Title: Re: FizzBuzz Plus
Post by: STxAxTIC on February 04, 2021, 02:53:00 am
If you use Dav's idea from the other day, you can make it directly into an exe, too!
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 02:57:24 am
Yes but nothing you can run on SB is worth the trouble of immortalization.
Title: Re: FizzBuzz Plus
Post by: STxAxTIC on February 04, 2021, 03:19:43 am
What about SB itself? Should we throw that up into the Samples section? We have a few interpreters there gathering dust.

I have to get busy with that section again...
Title: Re: FizzBuzz Plus
Post by: bplus on February 04, 2021, 03:32:18 am
What about SB itself? Should we throw that up into the Samples section? We have a few interpreters there gathering dust.

I have to get busy with that section again...

Handling strings is still on the back burner and I definitely want to fix my array handling to what I learned from Luke for array strings.

We have a number of tools in some stage of development that are far more important for learning and extending QB64 usage, in my opinion quilty time better spent on that kind of thing.
Title: Re: FizzBuzz Plus
Post by: DANILIN on February 04, 2021, 06:00:12 am
Saw a one line ... version.  That's probably not possible in basic ...- Dav

there are examples on Internet in different languages
and there are 1-line examples here see search

GUESS MY NUMBER GAME
« on: September 12, 2019, 06:07:54 AM