22
\$\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\$
  • 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 Jan 9 '17 at 8:13
  • 16
    \$\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\$ – Mego Jan 9 '17 at 8:42
  • 32
    \$\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\$ – Martin Ender Jan 9 '17 at 9:19
  • 16
    \$\begingroup\$ Uo next: Subtract two numbers! \$\endgroup\$ – steenbergh Jan 9 '17 at 12:57
  • 7
    \$\begingroup\$ @wat Leaving no barrel-bottom unscraped, eh? \$\endgroup\$ – Gareth Jan 10 '17 at 12:39

120 Answers 120

3
\$\begingroup\$

C# - 11 bytes

(a,b)=>a*b;

Anonymous lambda.

\$\endgroup\$
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\$
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\$
  • 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\$ – Erik the Outgolfer Feb 4 '17 at 8:12
  • \$\begingroup\$ Upvoted it to make up for whoever downvoted \$\endgroup\$ – Metoniem Feb 21 '17 at 9:21
  • 3
    \$\begingroup\$ @Metoniem I think that upvoting just because you feel sorry isn't recommended though. \$\endgroup\$ – Erik the Outgolfer Feb 21 '17 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\$
  • 1
    \$\begingroup\$ × was added before this challenge was posted (at 13:37 UTC on 7 Jan 2016), so this answer is actually competing \$\endgroup\$ – ETHproductions Jan 10 '17 at 0:54
  • 5
    \$\begingroup\$ Did I say 2016? Gosh, I really need to get used to this whole "2017" thing :P \$\endgroup\$ – ETHproductions Jan 10 '17 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\$
  • \$\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 Jan 10 '17 at 19:14
2
\$\begingroup\$

Math++, 3 bytes

?*?

(Why is there a 30 character minimum?)

\$\endgroup\$
  • 5
    \$\begingroup\$ To avoid spam... \$\endgroup\$ – dkudriavtsev Jan 16 '17 at 5:36
  • 1
    \$\begingroup\$ @Mendeleev: I bet almost all existing spam existing has more than 30 chars, so the limitation is pointless and ineffective . \$\endgroup\$ – sergiol Aug 1 '17 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\$

Haskell, 3 bytes

(*)

Call it as a function: (*) a b

or as an operator: a * b .

\$\endgroup\$
  • 4
    \$\begingroup\$ Our standard is to require (*) so that the expression evaluates to a function and can be stored. \$\endgroup\$ – xnor Jan 9 '17 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 Jan 10 '17 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 Jan 10 '17 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 Jan 11 '17 at 21:22
  • \$\begingroup\$ Proton Polyglot :P \$\endgroup\$ – HyperNeutrino Oct 6 '17 at 20:49
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\$
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\$
  • \$\begingroup\$ Cool. Voted up. Very interesting answer \$\endgroup\$ – Muhammad Salman Apr 14 '18 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\$ – Martin Ender Apr 17 '18 at 11:40
  • \$\begingroup\$ @MartinEnder : Oh yeah, nice idea thanks \$\endgroup\$ – user79855 Apr 17 '18 at 14:08
2
+200
\$\begingroup\$

BitCycle, 33 bytes

 >>\/v
?+ \/
? +~!/
 A + =
  ~BC^

Try it online!

This takes input via command line args, with the -U flag to convert it into signed unary.

This only uses one pair of collectors in the main loop by representing the first number with unary 0s and the other number with 1s, allowing us to store both in the same collector.

Explanation:

No fancy gifs sorry. This also assumes you have at least a passing familiarity with BitCycle.

?+       Get both numbers as signed unary from the ? sources
? +      Sends 0s upwards (0s signify that the number is negative), and 1s downwards
  >>\/v  Given either 0, 1 or 2 zeroes
  + \/
   + !/  Push a 0 (signifying negative) if there is exactly one 0
 +      Push the first number as unary 1s to collector B
  +     Push the second number as unary 0s to collector A
 B +    Then push the first number into collector A
  ~AC   Then push both into collector C
     /    If the first bit out of collector C is 0
   + =    Turn the = into { and discard the first bit
   AC^    All the 0s go back to collector A, and the 1s go up
  +~!     Duplicate and print all the 1s   
   +      Push the duplicates back into A
  ~A

This loops until it runs out of 0s in the collector, then the = turns into a } and discards the rest of the 1 bits.

\$\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\$
  • 1
    \$\begingroup\$ Why the downvote? This is a perfectly valid submission. \$\endgroup\$ – Erik the Outgolfer Jan 9 '17 at 17:57
  • \$\begingroup\$ int.__mul__ would also be valid, but that's already posted. \$\endgroup\$ – Yytsi Jan 10 '17 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\$ – Erik the Outgolfer Jan 10 '17 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\$

Pushy, 2 bytes

*#

Try it online!

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

\$\endgroup\$
  • \$\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 Feb 21 '17 at 9:23
1
\$\begingroup\$

dc, 3

?*p

Try it online (wrapped in a shell script to run all given tests).

Note dc thinks underscores (_) are -ve signs.

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

Befunge-93, 5 bytes

&&*.@

Try it online!

&& gets two integers and pushes them to the stack; * multiplies them (duh); . prints the numeric value of the top of the stack, and @ ends the program.

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

Emacs Lisp, 3 bytes

This is just a function.

(*)

Usage:

(* num1 num2)
\$\endgroup\$
1
\$\begingroup\$

Microscript, 4 bytes

isi*

Microscript II, also 4 bytes

FsF*
\$\endgroup\$
1
\$\begingroup\$

Noodel, noncompeting 1 byte

Did not have a full complete version of Noodel until after this challenge, and did not even have multiplication with a single character until recently.

×

Try it:)

How it works

  # Input is implicitly pushed onto the stack with the first element (a) pushed first and the second last (b) making it the top.
× # Pops off two items producing => (b * a) and pushes on the result.
  # The top of the stack is popped off at the end of the program and pushed to stdout.

<div id="noodel" code="×" input="2,-4" cols="5" rows="5"></div>

<script src="https://tkellehe.github.io/noodel/noodel-latest.js"></script>
<script src="https://tkellehe.github.io/noodel/ppcg.min.js"></script>

\$\endgroup\$
  • \$\begingroup\$ @FlipTack, thank you. That should have fixed it. \$\endgroup\$ – tkellehe Jan 22 '17 at 14:13
1
\$\begingroup\$

Swift 3, 40 bytes

func f(_ a:Int,_ b:Int)->Int{return a*b}

Takes two parameter, return the two Ints multiplied. Called like this:

f(2, 4)
\$\endgroup\$
  • \$\begingroup\$ You can also use a closure {$0*$1} and use it like this let res = {$0 * $1} (2,4) //8 \$\endgroup\$ – Rodrigo Ruiz Murguía Feb 19 '17 at 0:41
1
\$\begingroup\$

C (gcc), 98 bytes

m(i,j){i=(i&j)<0?m(O(~i,1),-j):i?O(m(i>>1,O(j,j)),i&1?j:0):0;}O(i,j){i^=j,j&=i^j;i=j?O(i,j<<1):i;}

I don't think I have much of a chance against the other entries.

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Suggest replacing i>>1 with i/2 :) \$\endgroup\$ – ceilingcat Jul 27 '17 at 23:12
1
\$\begingroup\$

Taxi, 442 bytes

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:w 1 l 1 r.Pickup a passenger going to Multiplication Station.Pickup a passenger going to Multiplication Station.Go to Multiplication Station:n 1 r 2 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 r 1 l.Pickup a passenger going to Post Office.Go to Post Office:e 1 l 1 r.

Formatted with line breaks for legibility:

Go to Post Office:w 1 l 1 r 1 l.
Pickup a passenger going to The Babelfishery.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:w 1 l 1 r.
Pickup a passenger going to Multiplication Station.
Pickup a passenger going to Multiplication Station.
Go to Multiplication Station:n 1 r 2 l.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:s 1 r 1 l.
Pickup a passenger going to Post Office.
Go to Post Office:e 1 l 1 r.
\$\endgroup\$
  • \$\begingroup\$ I take pride in being last place on this challenge. \$\endgroup\$ – Engineer Toast Sep 13 '17 at 16:39
1
\$\begingroup\$

ZX80 BASIC ~39 bytes

 1 INPUT A
 2 INPUT B
 3 PRINT A;"X";B;"=";A*B

This program is so simple that it will very likely work on all variants on 8-bit BASIC. However, on the ZX80 (4K ROM) you are limited to integer maths with a signed 16-bit range; so if your answer is >32767 then it will error and you will not see a result.

To remedy this, install a new ROM onto your Sinclair ZX80 to allow 24-bit floating point maths, or upgrade to a Sinclair ZX81.

Save bytes by refactoring line 3 to:

 3 PRINT A*B
\$\endgroup\$
1
\$\begingroup\$

Alice, 7 6 bytes

*/
o@i

Try it online!

Follows the same pattern as my solution for addition.

\$\endgroup\$

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.