I recently pointed a student doing work experience to an article about dumping a multiplication table to the console. It used a nested for loop and multiplied the step value of each.
This looked like a .NET 2.0 approach. I was wondering, with the use of Linq and extension methods,for example, how many lines of code it would take to achieve the same result.
Is the stackoverflow community up to the challenge?
The challenge: In a console application, write code to generate a table like this example:
01 02 03 04 05 06 07 08 09 02 04 06 08 10 12 14 16 18 03 06 09 12 15 18 21 24 27 04 08 12 16 20 24 28 32 36 05 10 15 20 25 30 35 40 45 06 12 18 24 30 36 42 48 54 07 14 21 28 35 42 49 56 63 08 16 24 32 40 48 56 64 72 09 18 27 36 45 54 63 72 81
As this turned into a language-agnostic code-golf battle, I'll go with the communities decision about which is the best solution for the accepted answer.
There's been alot of talk about the spec and the format that the table should be in, I purposefully added the 00 format but the double new-line was originally only there because I didn't know how to format the text when creating the post!
PHP, 62 chars
Fortran95 - 40 chars (beating perl by 4 chars!)
This solution does print the leading zeros as per the spec.
Ruby - 42 Chars (including one linebreak, interactive command line only)
This method is two lines of input and only works in
irb
(because irb gives us_
), but shortens the previous method by a scant 2 charcters.Ruby - 44 Chars (tied with perl)
Ruby - 46 Chars
Ruby - 47 Chars
And back to a double loop
Ruby - 54 chars!
Using a single loop saves a couple of chars!
Ruby - 56 chars
Another attempt using C#/Linq with GroupJoin:
cat - 252 characters
Assuming that a trailing newline is wanted; otherwise, 251 chars.
* runs *
PHP, 71 chars
Output: