21
\$\begingroup\$

Write the shortest code to print the following multiplication table:

1×1=1
1×2=2  2×2=4
1×3=3  2×3=6  3×3=9
1×4=4  2×4=8  3×4=12 4×4=16
1×5=5  2×5=10 3×5=15 4×5=20 5×5=25
1×6=6  2×6=12 3×6=18 4×6=24 5×6=30 6×6=36
1×7=7  2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49
1×8=8  2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9  2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
\$\endgroup\$
8
  • 6
    \$\begingroup\$ Is anyone really going to do anything besides 2 for loops? Where's the challenging (interesting) part? \$\endgroup\$ Commented Apr 15, 2013 at 23:56
  • 3
    \$\begingroup\$ I don't use for. Ok, I use while. \$\endgroup\$ Commented Apr 16, 2013 at 13:31
  • 3
    \$\begingroup\$ Are trailing spaces important? \$\endgroup\$ Commented May 13, 2013 at 19:06
  • 1
    \$\begingroup\$ @jdstankosky My answer uses the inverted upper triangle of a scaled identity matrix :). \$\endgroup\$ Commented Sep 20, 2016 at 16:15
  • 1
    \$\begingroup\$ @jdstankosky perhaps you may find my answer a bit more interesting - no loops involved \$\endgroup\$ Commented Jul 22, 2017 at 21:03

72 Answers 72

1 2
3
0
\$\begingroup\$

Racket 94 bytes

(for((n(range 1 10)))(for((i(range 1(add1 n))))(printf "~ax~a=~a  "i  n(* i n)))(displayln""))

Ungolfed:

(define (f)
  (for ((n (range 1 10)))
    (for ((i (range 1 (add1 n))))
      (printf "~ax~a=~a  " i  n (* i n)))
    (displayln ""))
  )

Testing:

(f)

Output:

1x1=1  
1x2=2  2x2=4  
1x3=3  2x3=6  3x3=9  
1x4=4  2x4=8  3x4=12  4x4=16  
1x5=5  2x5=10  3x5=15  4x5=20  5x5=25  
1x6=6  2x6=12  3x6=18  4x6=24  5x6=30  6x6=36  
1x7=7  2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49  
1x8=8  2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64  
1x9=9  2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81  
\$\endgroup\$
0
\$\begingroup\$

tcl, 93

time {incr i;set j 0;while {[incr j]<=$i} {puts -nonewline $j\x$i=[expr $j*$i]\ };puts ""} 10

demo

\$\endgroup\$
1
0
\$\begingroup\$

///, 262 bytes

/;/=4//:/x\/\///-/ 6:,/=2//'/ 5:&/=1//%/ 4:#/ 3:"/
1:!/  2x/1x1&"2,!2;"3=3!3=6 #3=9"4;!4=8 #4&2%4&6"5=5!5&0#5&5%5,0'5,5"6=6!6&2#6&8%6,4'6=30-6=36"7=7!7&4#7,1%7,8'7=35-7;2 7x7;9"8=8!8&6#8,4%8=32'8;0-8;8 7x8=56 8x8=64"9=9!9&8#9,7%9=36'9;5-9=54 7x9=63 8x9=72 9x9=81

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Haskell, 84 82 bytes

putStr$init$unlines[unwords[s i++'×':s j++'=':s(i*j)|i<-[1..j]]|j<-[1..9]]
s=show

-2 thanks to @Laikoni!

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

Perl 5, 51 bytes

for$/(1..9){printf$_."x$/=%-3d",$_*$/for 1..$/;say}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

uBASIC, 87 bytes

Anonymous function that takes no input and outputs to the console.

0ForY=1To9:ForX=1To9:z=x*y:Ifx<=yThen?x;"x";y;"=";z;" ";:Ifz<10Then?" ";
2NextX:?:NextY

Try it online!

\$\endgroup\$
0
\$\begingroup\$

MY-BASIC, 105 bytes

Anonymous function that takes no input and prints a multiplication table to the console.

Str(...) conversions are costly in this solution.

For Y=1 To 9
For X=1 To 9
If x<=y Then Print Left(Str(x)+"x"+Str(y)+"="+Str(x*y)+"  ",7)
Next
Print;
Next

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3.6, 70 68 bytes

r=range(1,10)
for b in r:print(*[f"{a}x{b}={a*b:<2}"for a in r[:b]])

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I had not seen Daniel's Answer before submitting this, but credit to him for the original implementation of this idea. \$\endgroup\$
    – Gareth
    Commented Jul 9, 2017 at 21:09
  • 1
    \$\begingroup\$ 68 bytes \$\endgroup\$
    – ovs
    Commented Jan 31, 2018 at 19:31
0
\$\begingroup\$

Yabasic, 101 bytes

Anonymous function that takes no input and prints a multiplication table to the console.

For y=1To 9
For x=1To 9
If(x<=y)Then
?Left$(Str$(x)+"x"+Str$(y)+"="+Str$(x*y)+"  ",7);
Fi
Next
?
Next

-3 bytes for the use of Fi over EndIf

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 70 characters

for(s=i='';i++<9;s+='\n')for(j=0;j++<i;s+=j+'×'+i+'='+j*i+' ');alert(s)

Respecting alignment, 83 characters:

for(s=i='';i++<9;s+='\n')for(j=0;j++<i;s+=j+'×'+i+'='+j*i+(j*i>9?' ':'  '));alert(s)

\$\endgroup\$
0
\$\begingroup\$

Stax, 18 bytes, (17 chars, UTF-8)

9mc{b"`n×` =`*"mJ

Run and debug it

can't be compressed due to the unicode requirement. a 16 may be possible if I can find a way to compress it. 14 if using x is allowed.

\$\endgroup\$
0
\$\begingroup\$

Uiua, 38 bytes

≡⬚@ (&p/$"_ _"▽:≡$"_×_=_")⊞⊃⊃⊙∘×≤.+1⇡9

Try it!

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.