11
\$\begingroup\$

In this challenge you've to print multiplication tables by input, Here are some examples:

Input: 2 

Output: 
0 2  4 6 8  10  12  14  16  18  20

Input: 20

Output: 20 40 60 80 100 120 140 160 180 200

Rules

  1. The shortest code in bytes wins.

  2. This challenge is a code-golf, It follows code-golf general rules ()

  3. If, just if, your code can't print number, you can use letters, Here is an example:

    Input: B

    Output: B D F H J L N P R T

  4. You can choose to start from 0 or your number (like 20). You can choose if put spaces or don't. The challenge is free, just take an input and print multiplication tables.

  5. Your output must list the first 10 members of the times table for the given number. You may leave out 0*n.

\$\endgroup\$
5
  • 10
    \$\begingroup\$ Your first example has multiples from 0 to 10, the second from 1 to 10. Can we choose either of these or is one of them a typo? Also, does output have to be space-separated or can we use other list-formats? (If only spaces, the number of spaces is a bit random in your example.) \$\endgroup\$ Sep 7, 2016 at 11:55
  • \$\begingroup\$ You can choose to start from 0 or your number (like 20). You can choose if put spaces or don't. The challenge is free, just take an input and print multiplication tables. \$\endgroup\$
    – Rizze
    Sep 7, 2016 at 11:56
  • 4
    \$\begingroup\$ Welcome to PPCG! Nice to see a pretty simply challenge, since we don't have these enough, although in the future I would add some more information. Like: From the test cases it seems we only need to output 10 numbers, but I don't see this specified. Do we need to support negative input? Why are there two spaces between 2 and 4? Why does the first test case have the 0 in it's output (making it 11 output numbers instead of 10). etc. etc. Also, the Sandbox for proposed challenges is a good place to post first to perfect the challenge \$\endgroup\$ Sep 7, 2016 at 12:19
  • \$\begingroup\$ Under rule 3, what should the output for C be? How about Z? \$\endgroup\$
    – Lynn
    Sep 7, 2016 at 15:44
  • 1
    \$\begingroup\$ What exactly is the output, the normal rules tend to allow a function to return its output as a list rather than printing them to STDOUT. \$\endgroup\$ Sep 7, 2016 at 22:04

59 Answers 59

1
2
2
\$\begingroup\$

Groovy (39 bytes for dynamic, 21 for static):

Dynamic (39 bytes)

{a,b,c->(a..b).collect{it*c}}(1,10,2)​
  • a is the starting point of the table
  • b is the ending point of the table.
  • c is the factor

(1, 10, 2) would print:

2
4
6
8
10
12
14
16
18
20

Try it here: https://groovyconsole.appspot.com/edit/5194081252671488


Partial Dynamic (29 bytes)

{n->(1..10).collect{it*n}}(2)

Try it here: {same website, I can't post 3 links}/5080994763767808


Static (21 bytes)

(1..10).collect{it*2}

Try it here: https://groovyconsole.appspot.com/script/5110807876599808


Original Post Mistakes

Edited because someone pointed out I had made mistakes, original version was:

def x(a,b,c){[a..b].each{println it*c}}

Mistakes were:

  • Not including the call to the closure in the byte-count.
  • [a..b] instead of (a..b).
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Without dynamic input's it is: def x(c){[1..10].each{println it*c}} \$\endgroup\$ Sep 7, 2016 at 14:35
  • \$\begingroup\$ I'm afraid if your code expects a value required to work, other then the specified input, that has to be included in the length count. So better go with the hardcoded 1..10 range. By the way, anonymous functions/procs/lambdas/closures are acceptable: {c->(1..10).each{println it*c}} pastebin.com/FWBtSy77 Oh, could you specify your Groovy version? In 2.4.5 your code produces strange output: pastebin.com/6s0yeUx8 \$\endgroup\$
    – manatwork
    Sep 7, 2016 at 15:59
  • \$\begingroup\$ As there are some other solutions that output the language's native list representation, here it is in Groovy: {print((1..10)*.multiply(it))}. \$\endgroup\$
    – manatwork
    Sep 7, 2016 at 16:12
  • \$\begingroup\$ I updated it based on your comments, and I actually did make some mistakes in the initial posting as well. \$\endgroup\$ Sep 7, 2016 at 18:03
  • \$\begingroup\$ Groovy version didn't matter it was the [1..10] instead of using (1..10), which made it a 2D array which made it completely different haha. \$\endgroup\$ Sep 7, 2016 at 18:12
2
\$\begingroup\$

Pyth, 5 Bytes

Try It Online

VhT*N

explanation

V    for unary_range(
hT   Ten+1)
*N   Print (N *Input)

Example

===================== Input ======================
20
==================================================

0
20
40
60
80
100
120
140
160
180
200
\$\endgroup\$
2
\$\begingroup\$

3d, 35 bytes

Code:

>#a…:>×=$v
   ;, !a#—v
     ^ ! '<

Explanation:

#a push 10
…  push every number in the range top..1 (=> 10..1)
:  input number
×  push product of top two of the stack
=  output as number
$  invert top two of the stack
—  pops stack and check if null
if is null:
    #a! print newline
    ,;  pops silently and exits
if not:
    ' ! print a space
... and loops back to ×
\$\endgroup\$
2
\$\begingroup\$

Perl 6, 10 bytes

{$_ X*^11}

Explanation:

{
  # the input
  $_

  # crossed using &infix:<*>
  X[*]

  # a Range from 0 to 11 excluding 11
  # ( short for 「0 ..^ 11」 )
  ^11
}
say {$_ X*^11}(20)
# (0 20 40 60 80 100 120 140 160 180 200)
\$\endgroup\$
2
\$\begingroup\$

Racket 31 bytes

(λ(n)(for/list((i 11))(* n i)))

Saved 7 bytes using suggestion by @StevenH "define(f n)" replaced by "λ(n)".

Testing:

(f 2)
'(0 2 4 6 8 10 12 14 16 18 20)

(f 20)
'(0 20 40 60 80 100 120 140 160 180 200)

(f 7)
'(0 7 14 21 28 35 42 49 56 63 70)

(f 19)
'(0 19 38 57 76 95 114 133 152 171 190)
\$\endgroup\$
1
  • \$\begingroup\$ You can use a lambda λ to save 6 bytes: (λ(n)(for/list((i 11))(* n i))) \$\endgroup\$
    – Steven H.
    Sep 7, 2016 at 20:06
2
\$\begingroup\$

Forth, 31 bytes

Pretty simple answer. Leaves n on the stack.

: f 10 0 DO dup I 1+ * . LOOP ;

Try it online

\$\endgroup\$
2
\$\begingroup\$

Befunge 93 : 24 bytes

&:01+*.20g1+:"9"`#@_20p#  

Fairly straightforward. Uses get and put instructions to keep track of the iterator.

\$\endgroup\$
2
\$\begingroup\$

Dyalog APL, 5 bytes

Begins at 0× or 1× depending on ⎕IO.

⎕×⍳10

input

× times

⍳10 first 10 integers

TryAPL online: with ⎕IO←0 and with ⎕IO←1 .

\$\endgroup\$
2
\$\begingroup\$

Brain-Flak 50 46 bytes

Try it online

<>((()()())({}){}){({}[()]<(({})<>({})<>)>)}{}

Explanation

Switch to the off stack and push 9:

<>((()()())({}){})

Decrement the nine until zero

{({}[()]<...>)}{}

Each time add the input value a copy of the top value

(({})<>({})<>)
\$\endgroup\$
2
\$\begingroup\$

Sesos, 12 bytes

~~

Since this contains unprintables, here is an xxd reversible hexdump:

0000000: d6cb82 06efbe 7e7ed0 ef1fcf                       ......~~....

The SASM that was used to compile the above SBIN is here:

set numin            ;Input consists of linefeed-separated integers from now on.
set numout           ;Output consists of linefeed-separated integers from now on.
get                  ;Get the input.
fwd 1                ;Go to the repeat-count cell.
add 10               ;Repeat 10 times.
jmp                  ;Enter the loop.
 sub 1                ;This is a repeat-n-times loop.
 rwd 1                ;Go to the proper cell.
 jmp                  ;Table generation method.
  sub 1                ;Value copying.
  fwd 2                ;Go to the multiple cell.
  add 1                ;Add the value.
  fwd 1                ;Go to an empty cell.
  add 1                ;Copy the original value, so that we don't lose it.
  rwd 3                ;Enable finishing check.
 jnz                  ;Check if the method is finished.
 fwd 3                ;Where the original value has been copied.
 jmp                  ;Move the value back to its original place.
  sub 1                ;Empty the cell.
  rwd 3                ;Go to the original cell.
  add 1                ;Set it to the original value.
  fwd 3                ;Enable finishing check.
 jnz                  ;Check if the moving is finished.
 rwd 1                ;Go to the multiply cell.
 put                  ;Print its value.
 rwd 1                ;Enable finishing check.
                     ;(implicit jnz) Check if execution should be terminated.

Try it online!

Output is like this:

1
2
3
4
5
6
7
8
9
10

\$\endgroup\$
2
\$\begingroup\$

Scala, 41 35 bytes

Usage scala multiplicationtable.scala <input>

Code

print(1 to 10 map(args(0).toInt*))
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can drop the dots and some parentheses: 1 to 10 map(_*args(0).toInt)map print \$\endgroup\$
    – corvus_192
    Oct 6, 2016 at 19:00
1
\$\begingroup\$

Pyth, 7 bytes

m*dQU11

Input: 2
Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Prints as a list, but can print as seen in the question with a simple modification, making it 9 bytes.

jdm*dQU11

Input: 20
Output: 0 20 40 60 80 100 120 140 160 180 200
\$\endgroup\$
1
\$\begingroup\$

Fourier, 15 bytes

I~N11(i*Noi^~i)

Output for input 20:

020406080100120140160180200

Since unseparated output is allowed.

Try it online!

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

Neoscript, 16 bytes

{n|0:[n]:(10*n)}

Inspired by @manatwork answer

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

LI, 30 bytes

M1iM2iM3iM4iM5iM6iM7iM8iM9iMY9

The lack of a working list implementation does not win me any awards today.

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

Labyrinth, 41 bytes

First time trying to use Labyrinth. I have no idea how to take advantage of grid manipulation, so I ignored it in my answer. There's probably certainly better ways to do this, but I don't know how to do it.

10}
  ?
: :
:::
  :
":"
"
!\{(@
"  }
":+"

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ If it's any consolation, apart from a couple of very specific use cases I also don't know how to take advantage of grid manipulation. ;) \$\endgroup\$ Sep 7, 2016 at 13:19
  • 1
    \$\begingroup\$ One such use case is to start with a long piece of linear code "for free" though: labyrinth.tryitonline.net/… (although I'm sure there must be a way that avoids duplicating 10 times) \$\endgroup\$ Sep 7, 2016 at 13:21
1
\$\begingroup\$

Python 2, 24 bytes

lambda n:range(0,11*n,n)

range() in Python 2 returns a list, so there is no need to convert.

\$\endgroup\$
2
  • \$\begingroup\$ That one uses Python 3. This one uses Python 2. The code is shorter by 6 bytes. Therefore, it is not a duplicate. \$\endgroup\$
    – Oliver Ni
    Sep 10, 2016 at 1:24
  • \$\begingroup\$ @OliverNi Yes, it is, since it uses the same method, and is the same language, just different version. The other one using list is just because Python 3's range is equivalent to Python 2's xrange. \$\endgroup\$ Oct 6, 2016 at 17:02
1
\$\begingroup\$

Japt, 6 bytes

UòA*UU

Test it online!

How it works

        // Implicit: U = input integer, A = 10
Uò      // Create a range from U to
  A*U   //  10 * U,
     U  //  skipping by U each time.
        // Implicit: output last expression
\$\endgroup\$
1
\$\begingroup\$

Bash on OSX, 13

jot 11 0 - $1

The input is taken as a command-line parameter. This will also work on Linux is jot is installed; e.g. sudo apt-get install athena-jot.

\$\endgroup\$
2
  • \$\begingroup\$ Can't you use jot 11 0 -$1? \$\endgroup\$
    – Rizze
    Sep 8, 2016 at 8:22
  • \$\begingroup\$ @Rizze I don't think so - that would could down from 0 to $1 * -1 in 11 steps \$\endgroup\$ Sep 8, 2016 at 20:36
1
\$\begingroup\$

Mathematica, 10 bytes

#Range@10&

Range@10 is the list {1,2,...,10}, which is then multiplied by the input #.

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

PHP, 44 bytes

$a=$argv[1];for($i=0;$i<10*$a;$i+=$a)echo$i;
\$\endgroup\$
3
  • \$\begingroup\$ You can remove 1 byte using echo$iinstead of echo$i;. \$\endgroup\$
    – Rizze
    Sep 8, 2016 at 10:05
  • \$\begingroup\$ And how you run it? Without the semicolon I get only error: pastebin.com/uGbRA6KA \$\endgroup\$
    – manatwork
    Sep 8, 2016 at 10:11
  • \$\begingroup\$ @manatwork true, i didn't even check before editing, my bad \$\endgroup\$
    – Sefa
    Sep 8, 2016 at 10:57
1
\$\begingroup\$

jq, 15 characters

range(0;.*11;.)

Sample run:

bash-4.3$ jq 'range(0;.*11;.)' <<< 20
0
20
40
60
80
100
120
140
160
180
200

On-line test

\$\endgroup\$
5
  • \$\begingroup\$ Can't you use range(0;.*11)? \$\endgroup\$
    – Rizze
    Sep 8, 2016 at 12:51
  • \$\begingroup\$ @Rizze, then step defaults to 1, so for 20 will not output 0, 20, 40, …, 200, but 0, 1, 2, …, 219. \$\endgroup\$
    – manatwork
    Sep 8, 2016 at 13:02
  • \$\begingroup\$ Oh ok, Can't you just delete . or ;? \$\endgroup\$
    – Rizze
    Sep 8, 2016 at 13:03
  • \$\begingroup\$ Nope. . is kind of default variable, something like $_ in Perl and it in Groovy. ; is the parameter separator, like , in most programming language. To use range() with step, you have to pass 3 parameters and none of them can be empty. \$\endgroup\$
    – manatwork
    Sep 8, 2016 at 13:18
  • \$\begingroup\$ Ah ok :) Understood \$\endgroup\$
    – Rizze
    Sep 8, 2016 at 13:24
1
\$\begingroup\$

C, 39 bytes

i;F(n){while(i<11)printf("%d ",n*i++);}

Ideone it!


Input:

10

Output:

0 10 20 30 40 50 60 70 80 90 100
\$\endgroup\$
0
1
\$\begingroup\$

Retina, 63 bytes

Feels a bit long...

$
¶10$*
+`¶(1(1+))
¶$2 $1
^.+
$*
(?<=(1+)¶.*)1
$1
.*¶

(1)+
$#1

Try it online

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

Pyth, 5 bytes

*RQST

If we're allowed to print the table from 0-9, *RQT would work as well. Outputs in list [2, 4, 6, 8,...] form.

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

C#, 62 bytes

void P(int x){for(int i=1;i<=10;i++){Console.WriteLine(x*i);}}
\$\endgroup\$
0
\$\begingroup\$

Python 3:

The current Python 3 answer has several flaws:

  1. the author hasn't been online for a year.
  2. does not count the print, and this answer, and possibly others, requires it built-in.

This answer, not counting the input n=2 and the print() has 26 Bytes, which beats the current best.

Counting everything (which is much easier to compare): 36 bytes.

I suggest this to be the updating answer.

n=2;s=0
while s<11: print(s*n); s+=1

Try it online!

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

brainfuck, 37 bytes

,<++++++++++[->[->+>+<<]>[-<+>]>.<<<]

Try it online!

Same IO as this

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

Vyxal, 3 bytes

₀ɾ*

Try it Online!

10-range-product-by-input

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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