[DFW.pm] Homework for the list, and for Oct 08 meeting

kevin kbrannen at pwhome.com
Thu Sep 11 22:08:40 PDT 2014


On 09/11/2014 02:03 PM, Tommy Butler wrote:
...
> http://rosettacode.org/wiki/FizzBuzz
>
> Is all about the modulus operator.  To do it another way might be
> creative but would almost certainly likely be less efficient.  The only
> thing I can think of that world be faster is the cheat method I saw,
> where you hard code into your program the known fizzes and buzzes,
> spitting them out at the corresponding integer positions in the 1 to 100
> loop.
...

You mean in the spirit of "Sieve of Eratosthenes" where you trade space 
of complexity? That's the only way I can think of to do it without the 
modulus operator.

my @nums;
for ($i=3 ; $i <= 100 ; $i += 3) { $nums[$i]++; }
for ($i=5 ; $i <= 100 ; $i += 5) { $nums[$i] += 2; }
my @strings = (0, "Fizz", "Buzz", "FizzBuzz");
print (($strings[$nums[$_]] || $_) . "\n") for (1 .. 100);

I don't think I'd call that cheating, but inefficient I'll agree with. :)

By cheating, I assume the method where you create an array with 100 
hard-coded values? Yeah, I go with cheating there. :)

Kevin


More information about the Dfw-pm mailing list