30
\$\begingroup\$

Input: Two decimal integers. These can be given to the code in standard input, as arguments to the program or function, or as a list.

Output: Their product, as a decimal integer. For example, the input 5 16 would lead to the output 80.

Restrictions: No standard loopholes please. This is , answer in lowest amount of bytes wins.

Notes: Layout stolen from my earlier challenge, Add two numbers.

Test cases:

1 2   -> 2
4 5   -> 20
7 9   -> 63
-2 8  -> -16
8 -9  -> -72
-8 -9 -> 72
0 8   -> 0
0 -8  -> 0
8 0   -> 0
-8 0  -> 0
0 0   -> 0

Or as CSV:

a,b,c
1,2,2
4,5,20
7,9,63
-2,8,-16
8,-9,-72
-8,-9,72
0,8,0
0,-8,0
8,0,0
-8,0,0
0,0,0

Leaderboard

var QUESTION_ID=106182,OVERRIDE_USER=8478;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
23
  • 4
    \$\begingroup\$ @FlipTack That's assuming addition and multiplication are as easy in any language, which I don't know if it's actually true. \$\endgroup\$
    – Fatalize
    Commented Jan 9, 2017 at 8:13
  • 19
    \$\begingroup\$ I don't think it's fair to allow the "add two numbers" challenge but close this one. Even though it's very trivial in most programming languages, it's still a valid challenge. If this is too broad, then the "add two numbers" challenge must also be too broad. \$\endgroup\$
    – user45941
    Commented Jan 9, 2017 at 8:42
  • 35
    \$\begingroup\$ Anyone is free to downvote trivial challenges if they don't like them, but this is a perfectly valid and on-topic challenge and it's nowhere near "too broad" (if anything, you might call a trivial challenge too narrow). I'm reopening this. That said, if anyone feels that trivial challenges insult their intelligence, I encourage them to seek out languages that make the task less trivial. \$\endgroup\$ Commented Jan 9, 2017 at 9:19
  • 16
    \$\begingroup\$ Uo next: Subtract two numbers! \$\endgroup\$
    – steenbergh
    Commented Jan 9, 2017 at 12:57
  • 7
    \$\begingroup\$ @wat Leaving no barrel-bottom unscraped, eh? \$\endgroup\$
    – Gareth
    Commented Jan 10, 2017 at 12:39

144 Answers 144

3
\$\begingroup\$

Clojure, 1 byte

*

:P As a bonus this works on any number of arguments:

[(*)
 (* 2)
 (* 2 3)
 (* 2 3 4)
 (* 2 3 4 5)] => [1 2 6 24 120]

Interestingly you can easily get its source code:

(source *)
(defn *
  "Returns the product of nums. (*) returns 1. Does not auto-promote
  longs, will throw on overflow. See also: *'"
  {:inline (nary-inline 'multiply 'unchecked_multiply)
   :inline-arities >1?
   :added "1.2"}
  ([] 1)
  ([x] (cast Number x))
  ([x y] (. clojure.lang.Numbers (multiply x y)))
  ([x y & more]
     (reduce1 * (* x y) more)))
\$\endgroup\$
3
\$\begingroup\$

C# - 11 bytes

(a,b)=>a*b;

Anonymous lambda.

\$\endgroup\$
3
\$\begingroup\$

Haskell, 3 bytes

(*)

Call it as a function: (*) a b

or as an operator: a * b .

\$\endgroup\$
5
  • 4
    \$\begingroup\$ Our standard is to require (*) so that the expression evaluates to a function and can be stored. \$\endgroup\$
    – xnor
    Commented Jan 9, 2017 at 16:59
  • \$\begingroup\$ @xnor I'm not sure, but I think if there is a built-in command, then it seems that just writing that down is enough. (At least when looking at other challenges/submissions.) \$\endgroup\$
    – flawr
    Commented Jan 10, 2017 at 13:23
  • \$\begingroup\$ @flawr It's difficult to score this, to assign it to a new name, you need to add parenthesis, but it can be called as a*b without. \$\endgroup\$
    – corvus_192
    Commented Jan 10, 2017 at 13:32
  • 1
    \$\begingroup\$ According to the definition I gave on Meta, which seems well-received at the moment, this sort of function definition must be an expression that evaluates to a value of function type. * is not an expression in Haskell. (*) is. \$\endgroup\$
    – user62131
    Commented Jan 11, 2017 at 21:22
  • \$\begingroup\$ Proton Polyglot :P \$\endgroup\$
    – hyperneutrino
    Commented Oct 6, 2017 at 20:49
3
\$\begingroup\$

LOLCODE, 95 bytes

HAI 1.3
I HAS A J
GIMMEH J
I HAS A Q
GIMMEH Q
I HAS A T
T R PRODUKT OF J AN Q
VISIBLE T
KTHXBYE

Try it online!

\$\endgroup\$
3
\$\begingroup\$

In Floop, 70 bytes, pos. only

r+s[r-]r[-n?o?s+]r+s-[@[-r-n+]r[s-o-[r-]r[-@;]]]r[n[-r@+r-@+]r[-s+]]s+

I wrote this in three parts:

  • Outer part: r+s[r-]r[-n?o?s+]r+s-[A]r[B]s+ (based off of my add-two-numbers program)
  • A: @[-r-n+]r[s-o-[r-]r[-@;]] (copies @1 to n, 1 at a time)
  • B: n[-r@+r-@+]r[-s+] (copies n to @1 and adds it to @1, 1 at a time)

This forms a loop, until o- is 0 after A runs. Then, @0 is printed.

Try It Online!

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

Labyrinth, 5 bytes

??*!@

Try it online!

Input format can be almost anything (this simply multiplies the first two decimal integers it finds in the input).

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

05AB1E, 1 byte

*

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ @downvoter: This is a built-in answer, and the convention is not to upvote them too much. But you should not downvote a perfectly valid answer either. \$\endgroup\$ Commented Feb 4, 2017 at 8:12
  • \$\begingroup\$ Upvoted it to make up for whoever downvoted \$\endgroup\$
    – Metoniem
    Commented Feb 21, 2017 at 9:21
  • 3
    \$\begingroup\$ @Metoniem I think that upvoting just because you feel sorry isn't recommended though. \$\endgroup\$ Commented Feb 21, 2017 at 11:40
2
\$\begingroup\$

FOG, 3 bytes

^^*

Takes input twice and multiplies. I can also just use * and call it a function, but for the sake of uniqueness I decided not to.

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

Japt, 1 byte

×

Here is a non-1 byte solution

U*V

Run it here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ × was added before this challenge was posted (at 13:37 UTC on 7 Jan 2016), so this answer is actually competing \$\endgroup\$ Commented Jan 10, 2017 at 0:54
  • 5
    \$\begingroup\$ Did I say 2016? Gosh, I really need to get used to this whole "2017" thing :P \$\endgroup\$ Commented Jan 10, 2017 at 2:21
2
\$\begingroup\$

C#, 112 63 bytes

Saved 49 bytes, thanks to milk

a=>b=>{int r=0,i=0,k=0<b?1:-1;for(;i!=b;i+=k)r+=a;return r*k;};

I've already posted an answer using the multiplication operator, but here I do it with a loop.

\$\endgroup\$
1
  • \$\begingroup\$ It's only 63 bytes if you don't use System.Math: a=>b=>{int r=0,i=0,k=0<b?1:-1;for(;i!=b;i+=k)r+=a;return r*k;}; \$\endgroup\$
    – milk
    Commented Jan 10, 2017 at 19:14
2
\$\begingroup\$

Math++, 3 bytes

?*?

(Why is there a 30 character minimum?)

\$\endgroup\$
2
  • 5
    \$\begingroup\$ To avoid spam... \$\endgroup\$
    – anna328p
    Commented Jan 16, 2017 at 5:36
  • 2
    \$\begingroup\$ @Mendeleev: I bet almost all existing spam existing has more than 30 chars, so the limitation is pointless and ineffective . \$\endgroup\$
    – sergiol
    Commented Aug 1, 2017 at 22:27
2
\$\begingroup\$

Loader, 124 bytes

~A:set B =@IN
~A:set A =@IN
B:~C:decr B
~C:set C =A
C:incr D
C:decr C
set G =0
B:incr G
C:incr G
G:load m
~B:print D
~B:exit

Run from m.ldr, or alternatively replace the m in the third line from the end with the name of the file.

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

SimpleTemplate, 59 bytes

Since this language doesn't have math (yet), it requires a loop and an increment, to generate the result.

{@setx 0}{@for_ from1 toargv.0}{@incbyargv.1 x}{@/}{@echox}

Ungolfed, with whitespace:

{@set prod 0}
{@for _ from 1 to argv.0}
    {@inc by argv.1 prod}
{@/}
{@echo prod}

This answer was written to run on the commit 2166e6bdac44064ec5594d511528d1469ea3feef from 2017-01-07.

On the latest version (commit 9857e4277536555d0b06e8ef9c00ba0c7f23cf6d), one could do like this (50 bytes):

{@for_ from1 toargv.0}{@incbyargv.1 x}{@/}{@echox}

This will show a warning saying that the index 'x' doesn't exist.


With a new update (commit 552216290ec0d8cb9893e08d89601c4d67fcc3d1), the code can be written as (21 bytes):

{@set*_ argv}{@echo_}
\$\endgroup\$
2
\$\begingroup\$

CJam, 3 bytes

{*}

Try it online!

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

Cubix, 6 bytes

OI|u*@

This is a slightly different algorithm than the "Add Two Numbers" Cubix solution. It would have been trivial to simply use that solution with a * instead of a + but the flow here is slightly different.

On a cube:

  O
I | u *
  @

rather than using the control flow \ or / and then duplicating I to read in the inputs, I opted to use | to pass over the I twice instead, which forced me to use u to get to the output commands.

The commands are:

  • I : read in input
  • | : reflect IP left-right
  • I : read in input
  • * : multiply
  • u : do a right-hand u-turn (i.e., two right turns)
  • O : output as a number. the IP is now facing North, and has a right turn to make, which takes it to
  • * : multiply the top two numbers (essentially a no-op)
  • @ : terminate program.

Similarly to the addition solution, the cube's symmetry allows the following solution.

  @
I | U *
  O

Additionally, replacing | with < or T will work as well.

Try it online!

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

x86_64 machine language (Linux), 5 4 bytes

0:       97                      xchg   %edi,%eax
1:       f7 ee                   imul   %esi
3:       c3                      retq

To Try it online!, compile and run the following C program.

#include<stdio.h>

int f(int a,int b){return a*b;}
const char g[]="\x97\xf7\xee\xc3";

int main(){
  for( int i = 0; i < 10; i++ ) {
    for( int j = 0; j < 10; j++ ) {
      printf( "%d %d %d %d\n", i, j, f(i,j), ((int(*)())g)(i,j) );
    }
  }
}
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 39 36 bytes

-3 bytes thanks to @MartinEnder

f=(a,b)=>b?b>0?a+f(a,b-1):-f(a,-b):0

Try it online!

This is me having fun , it won't win any competitions but it uses recursion and doesn't use the * operator.

Explanation :

f = (a,b) => {
    if (b == 0) {
        return 0;
    }
    if (b > 0) {
        return(a + f(a , b-1))
    }
    else {
        return(-f(a , -b))
    }
};
\$\endgroup\$
4
  • \$\begingroup\$ Cool. Voted up. Very interesting answer \$\endgroup\$ Commented Apr 14, 2018 at 13:20
  • \$\begingroup\$ You can save three bytes by dropping ==0 and swapping the true and false branches of the outer ternary: tio.run/##Zc7RCoIwGAXg@55il265@P8JhYL6LJtpFOIko9df/… \$\endgroup\$ Commented Apr 17, 2018 at 11:40
  • \$\begingroup\$ @MartinEnder : Oh yeah, nice idea thanks \$\endgroup\$
    – user79855
    Commented Apr 17, 2018 at 14:08
  • \$\begingroup\$ also 36 bytes: no addition \$\endgroup\$
    – c--
    Commented Jul 24, 2022 at 4:44
2
\$\begingroup\$

√ å ı ¥ ® Ï Ø ¿, 4 bytes

II*o

Almost identical to my answer for the Add two numbers question.

Explanation

I    › Take input from the command line, evaluate and push to stack
 I   › Take another input
  *  › Times the two values together and push to stack
   o › Output the first value on the stack
\$\endgroup\$
2
\$\begingroup\$

Intcode, 22 bytes

3,0,3,1,2,0,1,2,4,2,99

Fairly basic. IO boilerplate takes up most of the bytecount, given that the language has multiplication as a builtin (2,0,1,2 would be a valid answer, if snippets were allowed)

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

International Phonetic Esoteric Language, 4 bytes (WIP language) (OLD)

ɪɪθo

No TIO interpreter yet, but is runnable by cloning the repository above, and calling python3 main.py "code here".

ɪɪθo
ɪ    ; push int
 ɪ   ; push int
  θ  ; pop, pop, push a * b
   o ; pop, print
\$\endgroup\$
2
\$\begingroup\$

Brainetry, 257 bytes

Golfed version. A more interesting submission follows.

a b c d e f
a b c
a b c d e f
a b
a b c d e f g h
a b c d e
a b c
a b c d e f g h
a b c d e
a b c
a b c d
a b c
a b c d
a b
a b
a b c d e f g h i

a b c d e f g h
a b c d e
a b
a b
a b c d
a b c
a b c
a b c d e f g h i
a
a b c d e f g h i

a b
a b c d e f g

To try this online, follow this repl.it link, paste the code in the btry/replit.btry file and press the green "Run" button.

Ungolfed version:

This is probably my first Brainetry
program that makes
non trivial use of some of
my new
experimental operators that I introduced, expanding the base
brainfuck implementation this is built
upon. We will
see if this made any difference or not.
This program works in a
rather simple way.
First I read inputs.
Then I have
to nest two loops.
One loop,
two loops.
The two loops will be nested, which is nice.

One loop will have me decrement one of
the user inputs, then I
move left,
(not right)
and successively decrement the
cell I'm at.
I do this
and keep accumulating in another cell. If I loop
with
enough dedication, the final result will be stored someplace.

Legend says,
the correct place is the second cell.
\$\endgroup\$
2
  • 2
    \$\begingroup\$ lol, the ungolfed program is self-explanatory. \$\endgroup\$
    – user92069
    Commented Jun 14, 2020 at 6:27
  • \$\begingroup\$ @Memberfor3months yes it is :) \$\endgroup\$
    – RGS
    Commented Jun 14, 2020 at 9:07
2
\$\begingroup\$

Foam, 10 bytes

;# ;# * .#

Try it online!

I'm gonna be using this lang for the next few weeks

;# returns an integer from input.

* is...multiplication.

.# is output.

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

LOLCODE, 77 bytes

HAI 1
I HAS A a
I HAS A b
GIMMEH a
GIMMEH b
VISIBLE PRODUKT OF a AN b
KTHXBYE

I has a bettr answar cuz I used PRODUKT OF a AN b in da VISIBLE direktly instaed of in an variable !!1!

Try it Online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Gramaer misteak on porpose !1!! \$\endgroup\$
    – CreaZyp154
    Commented Feb 2, 2022 at 8:31
2
\$\begingroup\$

Piet, 10 9 codels

Piet program

Original image

The 2 white codels on the side can be replaced with any color. Most straightforward solution, literally "read 2 numbers, multiply, output", I really don't know why this answer is more complicated.

Update: saved 1 codel by switching from a 2x5 to a 3x3 grid

\$\endgroup\$
5
  • \$\begingroup\$ @DLosc Did you forget the 2 white pixels at the bottom? Works fine with gabriellesc.github.io/piet \$\endgroup\$
    – Seggan
    Commented Apr 6, 2022 at 20:19
  • \$\begingroup\$ Edited the image to clarify \$\endgroup\$
    – Seggan
    Commented Apr 6, 2022 at 20:26
  • \$\begingroup\$ I'm thinking MasterPiet acts as if the whole PNG is surrounded by black codels \$\endgroup\$
    – Seggan
    Commented Apr 6, 2022 at 22:21
  • \$\begingroup\$ I apologize, I was wrong about white blocks. The Esolangs article doesn't describe them properly, and I hadn't read the actual spec in a while. My guess now is that perhaps Npiet treats white blocks correctly except that it doesn't do the "exit if infinite loop" part. \$\endgroup\$
    – DLosc
    Commented Apr 7, 2022 at 21:34
  • \$\begingroup\$ @DLosc Apology accepted :) \$\endgroup\$
    – Seggan
    Commented Apr 7, 2022 at 21:39
2
\$\begingroup\$

rusty_deque, 2 bytes

Two variants: one for the left of the deque, one for the right of the deque.

*! # left
*~ # right
\$\endgroup\$
1
\$\begingroup\$

Python 2, 14 bytes

lambda a,b:a*b

Try it online!

I feel like this is too much shorter than from operator import*;mul.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Why the downvote? This is a perfectly valid submission. \$\endgroup\$ Commented Jan 9, 2017 at 17:57
  • \$\begingroup\$ int.__mul__ would also be valid, but that's already posted. \$\endgroup\$
    – Yytsi
    Commented Jan 10, 2017 at 6:29
  • \$\begingroup\$ @TuukkaX Duplicate answers are allowed, but I don't want to dupe an answer I have seen in the past purposefully. \$\endgroup\$ Commented Jan 10, 2017 at 9:43
1
\$\begingroup\$

Pyth, 4 bytes

M*GH

Defines a function named g, can call it in this way - gn1 n2, where n1 and n2 are the numbers.

Try it here!

Previous solutions using 5 bytes.

*.).Q and *hQeQ.

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

OCaml, 4 bytes

( *)

The space after the first parenthesis is needed because (* starts a comment in OCaml.

Try it online!

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

Scala, 2 bytes

_*

This is a curried lambda / anonymous function. To use it, assign it to a variable:

val f:(Int=>Int=>Int)=_*
f(7)(9)                   //returns 63

How it works:

In Scala, the underscore can be used as a shorthand for the arguments.

_*_, for example, is syntactic sugar for (a,b)=>a*b.

Removing the second underscore is treating the method * of the first argument as a function, results in a curried function that multiplies its arguments.

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

Pushy, 2 bytes

*#

Try it online!

As you might have guessed, * is the multiplication operator, and # prints the result.

\$\endgroup\$
1
  • \$\begingroup\$ You might be able to remove the the last byte? I don't think output means it has to be printend :o \$\endgroup\$
    – Metoniem
    Commented Feb 21, 2017 at 9:23

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.