There's more than one way to do things PerlMonks

### Re^3: Golf Challenge: FizzBuzz

by liverpole (Monsignor)
 on Mar 02, 2007 at 20:15 UTC ( #602950=note: print w/replies, xml ) Need Help??

in reply to Re^2: Golf Challenge: FizzBuzz

Unfortunately, the last several entries are all missing newlines, which makes the output very hard to read.

It seems like such output is just a little too golfed!

Update:  Otherwise, you can still make it shorter:

```die+map{(Fizz)[\$_%3].(Buzz)[\$_%5]||\$_}1..100

Update 2:  As Sidhekin pointed out to me, the -l switch makes the newline problems go away.

On an unrelated note, I thought it would be fun writing a version that doesn't use the modulo operator (%).  Originally I tried golfing it, but it didn't golf well, so here's an easy-to-read version, which makes use of divisibility tests:

```use strict;
use warnings;

sub divisible_by_3 {
my \$num = shift;
while (length(\$num) > 1) {
my \$sum = 0;
map { \$sum += \$_ } split '', \$num;
\$num = \$sum;
}
return \$num =~ /^[0369]\$/;
}

sub divisible_by_5 {
my \$num = shift;
return (\$num =~ /[05]\$/);
}

sub divisible_by_15 {
my \$num = shift;
return (divisible_by_3(\$num) and divisible_by_5(\$num));
}

foreach (1 .. 100) {
printf "%s\n",
divisible_by_15(\$_)? "fizzbuzz":
divisible_by_3(\$_)?  "fizz":
divisible_by_5(\$_)?  "buzz":
\$_;
}

s''(q.S:\$/9=(T1';s;(..)(..);\$..=substr+crypt(\$1,\$2),2,3;eg;print\$..\$/

Replies are listed 'Best First'.
Re^4: Golf Challenge: FizzBuzz
by merlyn (Sage) on Mar 02, 2007 at 20:18 UTC
```    while (length(\$num) > 1) {
my \$sum = 0;
map { \$sum += \$_ } split '', \$num;
\$num = \$sum;
}
```1 while \$num =~ s/(\d)(\d)/\$1 + \$2/e;
Re^4: Golf Challenge: FizzBuzz (no %)
by tye (Sage) on Mar 02, 2007 at 20:39 UTC

(Note the lack of %).

```#2345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 6 2345678 7
+ 2345
for('001'x33|'00002'x20){print[pos,Fizz,Buzz,FizzBuzz]->[\$1],\$/while/(
+.)/g}
for('001'x33|'00002'x20){warn+(pos,Fizz,Buzz,FizzBuzz)[\$&],\$/while/./g
+}

map{warn+(FizzBuzz,Fizz,Buzz,pos)[\$&],\$/while/./g}331x34&33332x20
#2345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 6 2345

Update: Dropped 3 4 strokes above. Then 6 more. Then s/x33/x34/ to fix a bug where only 99 lines were printed by the 3rd version.

- tye

Re^4: Golf Challenge: FizzBuzz
by blazar (Canon) on Mar 03, 2007 at 15:32 UTC
Update: Otherwise, you can still make it shorter:
```die+map{(Fizz)[\$_%3].(Buzz)[\$_%5]||\$_}1..100

Generally printing to STDERR is not considered equivalent to printing to STDOUT.

Update 2: As Sidhekin pointed out to me, the -l switch makes the newline problems go away.

But under common golfing rules -l does count as +3 strokes. Changing

```\$_ for

to

```\$_,\$/for

Re^4: Golf Challenge: FizzBuzz
by vrk (Chaplain) on Mar 02, 2007 at 20:37 UTC
Unfortunately, the last several entries are all missing newlines, which makes the output very hard to read.

Indeed, but there is an easy fix: \$\="\n";. This does add eight characters, though.

Solving the problem without modulo seems fun. If this were Perl 6 or Haskell, one could do it with filtered streams. How about table-driven programming?

--

Create A New User
Node Status?
node history
Node Type: note [id://602950]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2020-08-12 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which rocket would you take to Mars?

Results (65 votes). Check out past polls.

Notices?