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
The shortest code in bytes wins.
This challenge is a code-golf, It follows code-golf general rules (code-golf)
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
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.
Your output must list the first 10 members of the times table for the given number. You may leave out 0*n.
61 Answers 61
MATL, 4 bytes
10:*
Breakdown:
% Implicit input
10: % Create a list from 1 2 ... 10
* % Multiply list by input
% Implicit output
C#, (削除) 105 (削除ここまで) (削除) 96 (削除ここまで) (削除) 67 (削除ここまで) 56 bytes
Now that I know how lambda's work in C#, here is an update to my first answer:
n=>{for(int i=0;i++<10;)System.Console.Write(i*n+" ");};
Saves 11 bytes.
First post, please forgive me for anything I've done wrong. Also, feel free to give me golfing tips, as I haven't really tried it before!
void c(int n){for(int i=0;i++<10;){System.Console.Write(i*n+" ");}}
Ungolfed:
void c(int n)
{
for (int i = 0; i++ < 10 ; )
{
System.Console.Write(i*n+" ");
}
}
Thank you Jonathan Allan, can't add comments yet. And thank you Kevin Cruijssen. I assumed I had to always include the entire program unless the question specified that snippets were allowed. Would I also be able to leave out the System. call to print to console in this case, or are using/imports required then?
-
1\$\begingroup\$ Welcome to PPCG! You can remove the class, only the main function is required by golfing rules :) \$\endgroup\$Jonathan Allan– Jonathan Allan2016年09月07日 13:17:10 +00:00Commented Sep 7, 2016 at 13:17
-
1\$\begingroup\$ @JonathanAllan Not only that, but by those same rules you can also just make a separate method without
Mainaltogether. I.e.void f(int n){for(int i=0;i++<10;){System.Console.Write(i*n+" ");}}And indeed, welcome to PPCG! \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2016年09月07日 13:43:22 +00:00Commented Sep 7, 2016 at 13:43 -
1\$\begingroup\$ codegolf.stackexchange.com/q/173/39436 \$\endgroup\$fede s.– fede s.2016年09月08日 01:34:10 +00:00Commented Sep 8, 2016 at 1:34
Jelly, 3 bytes
×ばつ
Test it at TryItOnline
Or first 256 cases, nicely aligned, also at TryItOnline
How?
×ばつ - main link takes one argument, n
5 - literal 10
R - range [1,2,...,10]
×ばつ - multiply by input (implicit vectorization)
-
\$\begingroup\$ I'd love to learn Jelly but half the commands don't render for me, so it'd be useless :D \$\endgroup\$Beta Decay– Beta Decay2016年09月07日 12:21:05 +00:00Commented Sep 7, 2016 at 12:21
-
\$\begingroup\$ @BetaDecay I can't type most of them and they don't render for me in any text editor or on my cmd line :( - they render fine in Firefox on my Windows 7 machine though. \$\endgroup\$Jonathan Allan– Jonathan Allan2016年09月07日 12:22:29 +00:00Commented Sep 7, 2016 at 12:22
-
\$\begingroup\$ You and Emigma are winning. \$\endgroup\$Rizze– Rizze2016年09月08日 13:26:59 +00:00Commented Sep 8, 2016 at 13:26
-
\$\begingroup\$ @BetaDecay - It could help you to download and (re)install DejaVu Sans Mono font (I can now see almost every character in notepad++ and TIO through Firefox is now using it too and displaying every character still) \$\endgroup\$Jonathan Allan– Jonathan Allan2016年09月08日 15:22:02 +00:00Commented Sep 8, 2016 at 15:22
-
\$\begingroup\$ Exactly what I thought of, +1. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年10月06日 16:58:45 +00:00Commented Oct 6, 2016 at 16:58
Clojure, 70 (削除) 80 (削除ここまで) bytes
This is my first post on CG, I hope the input is OK:
70 bytes
(defn -main[& a](println(map #(* %(read-string(first a)))(range 10))))
80 bytes
(defn -main[& a](let[n(read-string(first a))](println(map #(* % n)(range 10)))))
The program will read a number as a stdin argument and display the result:
Output
lein run 10
(0 10 20 30 40 50 60 70 80 90)
-
5\$\begingroup\$ Welcome! That's a great first answer! \$\endgroup\$mbomb007– mbomb0072016年09月07日 14:28:44 +00:00Commented Sep 7, 2016 at 14:28
05AB1E, 3 bytes
TL*
Explanation
T # literal predefined as 10
L # 1-based range: [1,2,3,4,5,6,7,8,9,10]
* # multiply with input
-
\$\begingroup\$ You and Jonathan Allan are winning \$\endgroup\$Rizze– Rizze2016年09月08日 13:26:50 +00:00Commented Sep 8, 2016 at 13:26
Perl, 19 bytes
Includes +1 for -n
Run with the input on STDIN:
perl -M5.1010 -n table.pl <<< 8
table.pl:
say$_*$`for/$/..10
-
\$\begingroup\$ You probably meant
-n? or did I miss something? \$\endgroup\$Dada– Dada2016年09月07日 20:07:40 +00:00Commented Sep 7, 2016 at 20:07 -
\$\begingroup\$ @Dada: right, conflated it with another version. Fixed \$\endgroup\$Ton Hospel– Ton Hospel2016年09月07日 22:44:20 +00:00Commented Sep 7, 2016 at 22:44
Haskell, 16 bytes
(<$>[1..10]).(*)
Usage example: (<$>[1..10]).(*) $ 4 -> [4,8,12,16,20,24,28,32,36,40].
Pointfree version of: f n = map (n*) [1..10].
-
\$\begingroup\$ What does
<$>do? \$\endgroup\$Cyoce– Cyoce2016年09月09日 01:30:17 +00:00Commented Sep 9, 2016 at 1:30 -
\$\begingroup\$ @Cyoce:
<$>is an infix version offmap(ormapwhen used with a list), i.e. it applied the function given as it's 1st argument to every element of the list.func <$> list=fmap func list=map func list. \$\endgroup\$nimi– nimi2016年09月09日 04:56:25 +00:00Commented Sep 9, 2016 at 4:56
Jellyfish, 8 bytes
p*r11
i
Quite simple: r11 gives the list [0, 1, ..., 9, 10], i reads the input, * multiplies them and p prints the resulting list.
R, 11 bytes
scan()*0:10
30 char.
PHP, 34 bytes
(34 bytes)
for(;$i++<10;)echo$i*$argv[1].' ';
(34 bytes)
for(;++$i%11;)echo$i*$argv[1].' ';
(34 bytes)
while($i++<10)echo$i*$argv[1].' ';
(35 bytes)
for(;$i++<10;)echo' '.$a+=$argv[1];
((削除) 41 (削除ここまで) 40 bytes)
<?=join(' ',range(0,10*$a=$argv[1],$a));
(削除)
<?=join(' ',range($a=$argv[1],10*$a,$a));
(削除ここまで) (44 bytes)
foreach(range(1,10)as$i)echo$i*$argv[1].' ';
-
\$\begingroup\$ The one using
range()with$stepcan be shorter if you start it from 0:<?=join(' ',range(0,10*$a=$argv[1],$a));. \$\endgroup\$manatwork– manatwork2016年09月07日 13:27:18 +00:00Commented Sep 7, 2016 at 13:27 -
\$\begingroup\$ The rule allowing to start from 0 was not set when I did it, but you're right; I update this one. \$\endgroup\$Crypto– Crypto2016年09月07日 13:47:22 +00:00Commented Sep 7, 2016 at 13:47
J, 8 bytes
(i.10)&*
This is the range from 0 to 9 inclusive (i.10) bonded (&) wit the multiplication function (*). This starts at zero.
Test cases
k =: (i.10)&*
k 2
0 2 4 6 8 10 12 14 16 18
k 10
0 10 20 30 40 50 60 70 80 90
k"0 i.10
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81
Zsh, 19 characters
echo {0..${1}0..1ドル}
Sample run:
(This is the interactive way to run it, equivalent with zsh scriptfile.sh 20.)
~ % set -- 20
~ % echo {0..${1}0..1ドル}
0 20 40 60 80 100 120 140 160 180 200
Python 3, (削除) 52 (削除ここまで) (削除) 33 (削除ここまで) 30 bytes
lambda n:list(range(0,11*n,n))
3 bytes saved thanks to @manatwork
Formatting the output is visibly not necessary
-
1\$\begingroup\$ Borrow from my shell answer:
lambda n:" ".join(map(str,range(0,n*11,n)))\$\endgroup\$manatwork– manatwork2016年09月07日 12:10:41 +00:00Commented Sep 7, 2016 at 12:10 -
\$\begingroup\$ @manatwork I use Python 3 \$\endgroup\$TuxCrafting– TuxCrafting2016年09月07日 12:19:18 +00:00Commented Sep 7, 2016 at 12:19
-
1\$\begingroup\$ You use Python 3 but you can save 6 bytes by using Python 2:
lambda n:range(0,11*n,n)\$\endgroup\$Jonathan Allan– Jonathan Allan2016年09月07日 13:03:57 +00:00Commented Sep 7, 2016 at 13:03 -
\$\begingroup\$ Author AFK for a year. new answer 26 bytes. \$\endgroup\$Gulzar– Gulzar2021年05月04日 17:59:54 +00:00Commented May 4, 2021 at 17:59
Mata, (削除) 15 (削除ここまで) 29 Bytes
args i
mata
A=1..10
B=`i'*A
B
Mata is the matrix programming language in the Stata commercial statistics package. Code creates a matrix, multiplies by the input (2 in this case) and the outputs the new matrix
Output
1 2 3 4 5 6 7 8 9 10
+---------------------------------------------------+
1 | 2 4 6 8 10 12 14 16 18 20 |
+---------------------------------------------------+
-
1\$\begingroup\$ How is it taking input? It needs to be reusable too. \$\endgroup\$Jonathan Allan– Jonathan Allan2016年09月07日 13:50:58 +00:00Commented Sep 7, 2016 at 13:50
-
1\$\begingroup\$ OK, have edited to clarify receipt of input \$\endgroup\$f1rstguess– f1rstguess2016年09月07日 14:14:44 +00:00Commented Sep 7, 2016 at 14:14
Pure Bash, 18
echo $[{0..10}*1ドル]
The input is taken as a command-line parameter.
-
\$\begingroup\$ Good & great ^_^ \$\endgroup\$ABcDexter– ABcDexter2016年10月17日 06:11:26 +00:00Commented Oct 17, 2016 at 6:11
Stata, 46 bytes
args i
set obs `i'
gen x=_n
gen y=x*`i'
list y
Output
For i=15
+-----+
| y |
|-----|
1. | 15 |
2. | 30 |
3. | 45 |
4. | 60 |
5. | 75 |
|-----|
6. | 90 |
7. | 105 |
8. | 120 |
9. | 135 |
10.| 150 |
|-----|
11.| 165 |
12.| 180 |
13.| 195 |
14.| 210 |
15.| 225 |
-
\$\begingroup\$ There are a few extra shorthands you can use here: ob for obs, g for gen, and l for list. Also, is it possible to have x be _n*`i' instead of using two variables? I'd never seen args before in STATA. Thanks for showing me something new! \$\endgroup\$bmarks– bmarks2017年02月17日 05:08:51 +00:00Commented Feb 17, 2017 at 5:08
Cheddar, 20 bytes
n->(|>11).map(n&(*))
Yay for functional \o/
I don't think this needs explanation but if you'd like to me add one just ask :)
-
\$\begingroup\$ I should learn Cheddar. And what does
n&(*)do? I'm assuming(*)means the same thing it means I'm haskell, but what does&do in that context? \$\endgroup\$Cyoce– Cyoce2016年09月09日 06:31:27 +00:00Commented Sep 9, 2016 at 6:31 -
Java 7, (削除) 61 (削除ここまで) 57 bytes
void c(int n){for(int i=0;i++<10;)System.out.print(i*n);}
Ungolfed & test cases:
class M{
static void c(int n){
for(int i = 0; i++ < 10; ){
System.out.print(i*n);
}
}
public static void main(String[] a){
c(2);
System.out.println();
c(20);
}
}
Output:
2468101214161820
20406080100120140160180200
-
\$\begingroup\$ Spaces are optional, System.out.print(i*n); would save 4 bytes \$\endgroup\$CameronD17– CameronD172016年09月08日 08:34:36 +00:00Commented Sep 8, 2016 at 8:34
-
\$\begingroup\$ @CameronD17 Ah, that rule has been added after I made this answer, but thanks for mentioning. I've removed it. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2016年09月08日 08:38:29 +00:00Commented Sep 8, 2016 at 8:38
JavaScript (ES6), (削除) 33 (削除ここまで) 31 bytes
f=(x,i=11)=>--i&&f(x,i)+" "+x*i
It's a recursive solution.
T-SQL 61 bytes
Replace n with the number for which table needs to be populated. Demo
SELECT TOP 11 number*n FROM master..spt_values WHERE type='P'
spt_value is a undocumented table in SQL Server, you can read more about this table in
I hope someone will come up with better TSQL solution.
Scala, 24 bytes
(n:Int)=>0 to 10 map(n*)
Explanation:
(n:Int)=> //define an anonymous function
0 to 10 //create a range from 0 to 10
map(n*) //multiply each element by the input
Brachylog, 12 bytes
,10y:[?]z:*a
I need to implement that I * [A, B, C] = [I*A, I*B, I*C]...
Explanation
,10y The range [0, 1, ..., 10]
:[?]z The list [[0, Input], [1, Input], ..., [10, Input]]
:*a The list [0*Input, 1*Input, ..., 10*Input]
brainf***, 84 bytes
,[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++<<<<<<<<<<-]>[.>]
Expects input as a single byte (as BF can only operate on numbers up to 255) and returns results as single bytes. Some values may look like ASCII, but they should not be treated as such; look at the decimal representation of the returned bytes.
-
\$\begingroup\$ You can save 5 bytes by doing >,[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++[<]>-]>[.>] \$\endgroup\$Jeff– Jeff2016年09月07日 12:47:41 +00:00Commented Sep 7, 2016 at 12:47
-
\$\begingroup\$ @Jeff Unfortunately, that breaks any input that's higher than 25 because the byte will overflow, resetting to 0. The overflow
0s get matched by[<], which makes the entire process loop infinitely. \$\endgroup\$Steven H.– Steven H.2016年09月07日 13:19:14 +00:00Commented Sep 7, 2016 at 13:19 -
\$\begingroup\$ Right, but overflowing bytes result in incorrect outputs with your code anyway, don't they? \$\endgroup\$Jeff– Jeff2016年09月07日 13:31:56 +00:00Commented Sep 7, 2016 at 13:31
-
1\$\begingroup\$ @Jeff They're correct outputs, they're just operating in mod 255. \$\endgroup\$Steven H.– Steven H.2016年09月07日 19:10:57 +00:00Commented Sep 7, 2016 at 19:10
JavaScript, 42 bytes
function f(a){for(i=0;i<11;i++)alert(i*a)}
-
\$\begingroup\$ I don't know JS very well, can you move the increment into the test portion of the for? \$\endgroup\$Jonathan Allan– Jonathan Allan2016年09月07日 12:26:22 +00:00Commented Sep 7, 2016 at 12:26
-
\$\begingroup\$ I edited it. It's ok now. \$\endgroup\$Rizze– Rizze2016年09月07日 12:33:18 +00:00Commented Sep 7, 2016 at 12:33
-
\$\begingroup\$ It alerts now with alert(). \$\endgroup\$Rizze– Rizze2016年09月07日 12:34:46 +00:00Commented Sep 7, 2016 at 12:34
-
\$\begingroup\$ It was alerting for me without alert: here - I take that back, need to click "run" not just "try" to reload \$\endgroup\$Jonathan Allan– Jonathan Allan2016年09月07日 12:35:27 +00:00Commented Sep 7, 2016 at 12:35
-
1\$\begingroup\$ @JonathanAllan What? \$\endgroup\$Rizze– Rizze2016年09月07日 12:36:37 +00:00Commented Sep 7, 2016 at 12:36
MATLAB, 12 bytes
@(x)x*[1:10]
Not really much to it. An anonymous function that takes x as input and multiplies it by the vector [1:10]. Displays as ans = 2 4 6 ... Also works in Octave.
PowerShell v2+, 23 bytes
param($n)1..10|%{$_*$n}
Takes input via command-line argument, loops over the range 1 to 10, each loop placing that number *$n on the pipeline. Output via implicit Write-Output at end of program execution results in newline separated values.
PS C:\Tools\Scripts\golfing> .\multiplication-table.ps1 2
2
4
6
8
10
12
14
16
18
20
PS C:\Tools\Scripts\golfing> .\multiplication-table.ps1 20
20
40
60
80
100
120
140
160
180
200
C89, 44 bytes
k;main(n){for(k=n*11;k-=n;)printf("%d ",k);}
Ungolfed:
k;
main(n)
{
for (k=n*11 ; k-=n ;)
printf("%d ", k);
}
Compile and run with (input 4)
gcc -ansi main.c && ./a.out 2 3 4
Output
40 36 32 28 24 20 16 12 8 4
Test it
Pyke, 5 bytes
TSQm*
Or TQm* if allowed to do numbers 0-9 rather than 1-10
Or TL* if we're going non-competitive.
Javascript (ES6), (削除) 34 (削除ここまで) 31 bytes
a=>{for(i=0;i<11;)alert(++i*a)}
(a)=>{for(i=0;i<11;++i)alert(i*a)}
Saved 3 bytes thanks to grizzly.
-
\$\begingroup\$ At the very least you don't need the parentheses around the
a, but I think it's also possible to be creative as to the position of the++. \$\endgroup\$Neil– Neil2016年09月07日 15:24:58 +00:00Commented Sep 7, 2016 at 15:24
Cubix, 24 bytes
;;..I1.N-?W.;;)W@/So..*O
Cubix is a 2-dimensional, stack-based esolang. Cubix is different from other 2D langs in that the source code is wrapped around the outside of a cube.
Test it online! Note: you'll have to copy-paste the code, and there's a 50 ms delay between iterations.
Explanation
The first thing the interpreter does is figure out the smallest cube that the code will fit onto. In this case, the edge-length is 1. Then the code is padded with no-ops . until all six sides are filled. Whitespace is removed before processing, so this code is identical to the above:
; ;
. .
I 1 . N - ? W .
; ; ) W @ / S o
. .
* O
-
\$\begingroup\$ Here's a slightly shorter one using the new stack options
I.0.WrN;-!@vrW>r)*O;o\$\endgroup\$MickyT– MickyT2016年10月13日 23:06:43 +00:00Commented Oct 13, 2016 at 23:06
Cbe? How aboutZ? \$\endgroup\$