FizzBuzz

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#16 Post by technosaurus »

rcrsn51 wrote:
technosaurus wrote:

Code: Select all

switch (n%15){
Very nice. Doing three divisions is clearly overkill. Doing one division is better.
Its not exactly a duff's device, but that is what I was recalling as a template.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#17 Post by GustavoYz »

@technosaurus: Nice, thanks. :D

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#18 Post by jamesbond »

rcrsn51 wrote:Very nice. Doing three divisions is clearly overkill. Doing one division is better.
I have one that does not use division at all; but surprisingly it is slower :shock:

Code: Select all

for ((a=1,b=2,c=4;a<101;a++,b--,c--));do d=$a;((\!b && \!c))&&d=FizzBuzz b=3 c=5;((\!b))&&d=Fizz b=3;((\!c))&&d=Buzz c=5;echo $d; done
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#19 Post by rcrsn51 »

jamesbond wrote:I have one that does not use division at all; but surprisingly it is slower
Is this an example of "loop unwinding"? Where the amount of processing needed to manage the loop is more than what's required to execute the contents of the loop?

So maybe three divisions is not a bad thing if it reduces the total amount of processing.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#20 Post by technosaurus »

here is an example how less comparisons can actually be bad

Code: Select all

#include <stdio.h>
	
int void main(){ 
int a=0,b=0,max=100;
char buf[500];
while(b<max/15){ //if max > 15 do blocks of 15 first
 printf("%d\t%d\tFizz\t%d\tBuzz\nFizz\t%d\t%d\tFizz\tBuzz\n%d\tFizz\t%d\t%d\tFizzBuzz\n",
		a+1,a+2,a+4,a+7,a+8,a+11,a+13,a+14);
 a=++b*15;
}
/* now do the remainder, but in order to do 1 comparison, we must reverse
 * this is _much_ slower than the comparisons so don't use it
 * - just an example of how using less comparisons can be slower
 * especially if doing so requires using additional slower functions
 * we don't always know what the compiler will do
*/
switch(max%15){
case 14 : sprintf(buf,"%d",a+14);
case 13 : sprintf(buf,"%d\t%s",a+13,buf);
case 12 : sprintf(buf,"Fizz\t%s",buf);
case 11 : sprintf(buf,"%d\t%s",a+11,buf);
case 10 : sprintf(buf,"Buzz\n%s",buf);
case 9 :  sprintf(buf,"Fizz\t%s",buf);
case 8 :  sprintf(buf,"%d\t%s",a+8,buf);
case 7 : sprintf(buf,"%d\t%s",a+7,buf);
case 6 : sprintf(buf,"Fizz\t%s",buf);
case 5 : sprintf(buf,"Buzz\n%s",buf);
case 4 : sprintf(buf,"%d\t%s",a+4,buf);
case 3 : sprintf(buf,"Fizz\t%s",buf);
case 2 : sprintf(buf,"%d\t%s",a+2,buf);
case 1 : sprintf(buf,"%d\t%s",a+1,buf);
default : break
}
printf("%s",buf);
} 

	return 0;
}
as opposed partial unwinding to something like:

Code: Select all

#include <stdio.h>
	
int main(){ 
int a=0,b=0,max=100;
while(b<max/15){ //if max > 15 do blocks of 15 first
	printf("%d\t%d\tFizz\t%d\tBuzz\nFizz\t%d\t%d\tFizz\tBuzz\n%d\tFizz\t%d\t%d\tFizzBuzz\n",a+1,a+2,a+4,a+7,a+8,a+11,a+13,a+14);
	a=++b*15;
}
while (a++ < 100){ 
	switch (a%15){ 
		case 3 : case 6 : case 9 : case 12 : printf("Fizz\t");break; 
		case 5 : case 10 : printf("Buzz\n"); break; 
		default : printf("%d\t",a);break;
	} 
} 

	return 0;
}
btw, you can do this same algorithm in shell
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Ibidem
Posts: 549
Joined: Wed 26 May 2010, 03:31
Location: State of Jefferson

#21 Post by Ibidem »

POSIX sh:

Code: Select all

for n in `seq 100`; do out=""; case $n in (*0|*5) out=Buzz ;; esac; [ 0 = $(( $n % 3 )) ] && out=Fizz$out; echo ${out:-$n}; done
Note that this uses 1 comparison (to determine divisibility by 5) and also one division; 15 is handled automatically, rather than as a special case.

starhawk
Posts: 4906
Joined: Mon 22 Nov 2010, 06:04
Location: Everybody knows this is nowhere...

#22 Post by starhawk »

Oldschool style. Even I can do this -- although it's rather embarrassing to note that working out all the (very boneheaded) bugs made the whole thing take about an hour and fifteen minutes to crank out.

Good thing I'm not paid to do this!

This was implemented (tested and working, finally!) in M$ QuickBASIC PDS 7.1. You can still download it! (I tested it using an XP box.)

Strictly speaking, there's a little fluff -- lines 10, 20, 30, 140, and 150 could all technically be eliminated w/o problem. But I think the added weight is worth it :P and yes, I know, LINE NUMBERS FOR PETE'S SAKE.

But I'm old-fashioned :D

Code: Select all

10  REM FIZZBUZZ SOLUTION BY CHRISTOPHER HAVEL, 2012
20  REM IMPLEMENTED IN QBASIC
30  PRINT
40  COUNT% = 0
50  COUNT% = COUNT% + 1
60  IF COUNT% MOD 3 = 0 THEN GOTO 100 ELSE GOTO 80
70  IF COUNT% MOD 5 = 0 THEN GOTO 120 ELSE GOTO 80
80  PRINT COUNT%
90  IF COUNT% = 100 THEN GOTO 140 ELSE GOTO 50
100 IF COUNT% MOD 5 = 0 THEN PRINT "FIZZBUZZ" ELSE PRINT "FIZZ"
110 IF COUNT% = 100 THEN GOTO 140 ELSE GOTO 50
120 IF COUNT% MOD 3 = 0 THEN PRINT "FIZZBUZZ" ELSE PRINT "BUZZ"
130 IF COUNT% < 100 THEN GOTO 50
140 PRINT
150 PRINT "ALL DONE!"
160 END

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#23 Post by jamesbond »

Ibidem wrote:POSIX sh:
Note that this uses 1 comparison (to determine divisibility by 5) and also one division; 15 is handled automatically, rather than as a special case.
Speed aside, I think this is the best one-liner Image. Especially since it is POSIX, too Image Image.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

starhawk
Posts: 4906
Joined: Mon 22 Nov 2010, 06:04
Location: Everybody knows this is nowhere...

#24 Post by starhawk »

Any thoughts on my attempt? Just curious to see folks' opinions...

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#25 Post by amigo »

starhawk, only your omission of a 'goto' has saved you from my wrath. basic just sucks -even worse in the 21st century.

starhawk
Posts: 4906
Joined: Mon 22 Nov 2010, 06:04
Location: Everybody knows this is nowhere...

#26 Post by starhawk »

amigo wrote:starhawk, only your omission of a 'goto' has saved you from my wrath. basic just sucks -even worse in the 21st century.
Er, sorry to poke the proverbial hornet's nest... but I count nine goto's in that code! Might want to look again... or not... :P

Ibidem
Posts: 549
Joined: Wed 26 May 2010, 03:31
Location: State of Jefferson

#27 Post by Ibidem »

I suspect part of the point was to think about the interaction of functions.
ie, multiple of 15=found in both the "fizz" and the "buzz" cases.
If you think it through, you can just make the two cases handle that, but if you don't, you must use a third case.

starhawk
Posts: 4906
Joined: Mon 22 Nov 2010, 06:04
Location: Everybody knows this is nowhere...

#28 Post by starhawk »

I'm not so good with object oriented stuff. Tried to learn C++ three times. Twice in college, once from a Dummies book (which was actually before college). Didn't learn a thing. I can sort of read the stuff, but that's about it.

College professor both times could only explain the difference between Java and whatever he was trying to teach. Never took Java. Don't care to -- doesn't interest me.

So, Prof, I guess you weren't too helpful to me. Oh well.

Dummies book didn't work so well, either -- typed up a program with the included software, compiler wouldn't budge. Checked the code against the book... exact match. Decided both book and software were junk. I still have the book somewhere... dunno why...

Back to BASIC. Simple, procedural code. Does what it needs to. No, I never knew that much. Yes, I'm rusty with what I do know. But I know it better than any of that newfangled object oriented stuff for sure.

It's good enough for me.

I even wrote a game in BASIC. Search this section of the forum -- IIRC I did post it at some point (if not, or if the link's dead, tell me and I'll put it [back] up). It's called QUICKGEM. You'll need DOSbox if you don't have real DOS... I didn't say BASIC was a new language, did I? ;)

Someday I'll mess around with QB64, which is crossplatform, and rewrite QUICKGEM for it. Or not. QUICKGEM was a real mess. No parser, just discrete IF...THEN statements. Yeah, I know -- very very bad. If someone can show me a simple-to-type REAL parser for BASIC, I'll rewrite the game for it. Haven't had that happen yet, though.

Before someone brings up Visual BASIC... I've used it. VB5 or 6, courtesy of MS, in college. Not bad... but... memorable for a big minus: the editor suggested commands and such because there were too many commands for one programmer to remember. Too fancy all around, particularly for me.

"...the more they overtech the plumbing, the easier it is to stop up the drain." Montgomery Scott, Star Trek III: The Search For Spock

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#29 Post by vovchik »

Dear starhawk,

You might want to give BaCon a try. It is very structured and makes using GTK easy with its HUG API.

Check out http://www.basic-converter.org.

With kind regards,
vovchik

PS If you use proper compile strings in Bacon, such as:

Code: Select all

bacon -o -s -o -Os -o -fdata-sections -o -ffunction-sections -o -Wl,--gc-sections "$myfile"
and then strip and upx the binary, you will get an executable (with GTK gui) of some 18-25k. With QB64, you get ridiculous binaries in the 1 MB range, which I find generally unacceptable, in spite of the cross-platform feature. BaCon compiles on any *nix (including OSX), and people have also succeded in getting it running in Windows using Cygwin.

And fizzbuzz in BaCon by Joe and myself: http://basic-converter.proboards.com/in ... thread=399

starhawk
Posts: 4906
Joined: Mon 22 Nov 2010, 06:04
Location: Everybody knows this is nowhere...

#30 Post by starhawk »

Hmmm... Cygwin and I tend to have bad reactions to each other... but I might give it a chance.

So how do I use BaCon? Does it come with an editor, or is it a BASIC-to-C adapter only? Does it automatically run gcc after it's done the crossover?

...and I'd love to see instructions on how to write a text parser in BASIC. Keep it simple, I'm stupid :P well, when programming, anyways.

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#31 Post by vovchik »

Dear starhawk,

It has a command-line converter that also runs gcc and a gui version that also runs gcc and has syntax highlighting....

With kind regards,
vovchik

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#32 Post by GustavoYz »

starhawk wrote:But I'm old-fashioned :D

Code: Select all

10  REM FIZZBUZZ SOLUTION BY CHRISTOPHER HAVEL, 2012
20  REM IMPLEMENTED IN QBASIC
30  PRINT
40  COUNT% = 0
50  COUNT% = COUNT% + 1
60  IF COUNT% MOD 3 = 0 THEN GOTO 100 ELSE GOTO 80
70  IF COUNT% MOD 5 = 0 THEN GOTO 120 ELSE GOTO 80
80  PRINT COUNT%
90  IF COUNT% = 100 THEN GOTO 140 ELSE GOTO 50
100 IF COUNT% MOD 5 = 0 THEN PRINT "FIZZBUZZ" ELSE PRINT "FIZZ"
110 IF COUNT% = 100 THEN GOTO 140 ELSE GOTO 50
120 IF COUNT% MOD 3 = 0 THEN PRINT "FIZZBUZZ" ELSE PRINT "BUZZ"
130 IF COUNT% < 100 THEN GOTO 50
140 PRINT
150 PRINT "ALL DONE!"
160 END
By some reason, they still teach Basic (Pascal too) to programmers in some Universities here... :roll:
Didn't knew people still "GOTO" voluntarily... :P

Post Reply