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

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\$
0
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\$
1
  • \$\begingroup\$ You can also use a closure {$0*$1} and use it like this let res = {$0 * $1} (2,4) //8 \$\endgroup\$ Commented Feb 19, 2017 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
  • 1
    \$\begingroup\$ Suggest replacing i>>1 with i/2 :) \$\endgroup\$
    – ceilingcat
    Commented Jul 27, 2017 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\$
1
  • \$\begingroup\$ I take pride in being last place on this challenge. \$\endgroup\$ Commented Sep 13, 2017 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\$
1
\$\begingroup\$

C, 56 bytes

main(int i,char**a){printf("%d",atoi(*++a)*atoi(*++a));}

compile

> gcc source.c

usage

> a 8 7

or

> ./a.out 8 7

explanation

main(int i,char**a) {  /* Begin program and accept command-line arguments.    */
   printf("%d",        /* First argument prints the (final) decimal result.   */
         atoi(*++a)    /* Returns int of program's 2nd command-line argument. */
         *             /* Multiplies the values of the 1st & 2nd arguments.   */
         atoi(*++a)    /* Returns int of program's 1st command-line argument. */
   );
}
\$\endgroup\$
6
  • \$\begingroup\$ Request: Don't put the byte count far away from the language and use a code block for the explanation. \$\endgroup\$ Commented Feb 22, 2017 at 22:23
  • \$\begingroup\$ @CalculatorFeline Why? \$\endgroup\$
    – veganaiZe
    Commented Feb 22, 2017 at 22:24
  • \$\begingroup\$ Well, I guess the byte count formatting is okay, but the explanation looks really weird. \$\endgroup\$ Commented Feb 22, 2017 at 22:25
  • \$\begingroup\$ That's my style (for the moment)! If you don't like it then please offer me something more than really weird -- Thanks for the feedback. \$\endgroup\$
    – veganaiZe
    Commented Feb 22, 2017 at 22:29
  • 2
    \$\begingroup\$ Your formatting is all over the place on my phone. Also, I'm sorry, but I just don't get why you'd include an explanation at all if you're not going to make it readable. \$\endgroup\$
    – Dennis
    Commented Feb 22, 2017 at 23:07
1
\$\begingroup\$

Jellyfish, 6 bytes

P*i
 i

Try it online!

P denotes print, then * multiplies the two numbers below and to the right. At the start of execution, the two is are replaced with values from STDIN.

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

Lean Mean Bean Machine, 12 bytes

OO
ii
 /
*
u
\$\endgroup\$
1
\$\begingroup\$

Whitespace, 4 bytes

	  

In some other languages, you could quote that as "\t \n". Expects two numbers on the stack and leaves the result on the stack.

As a complete program reading from standard input and writing to standard output, it would be 36 bytes:

   
 
 	
					   
 
 	
						  
	
 	

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ I know it's been quite a while, but you can golf 1 byte in the full program. Instead of Push_0, Duplicate, STDIN_as_integer, Retrieve for the second input, you can use Duplicate, Duplicate, STDIN_as_integer, Retrieve. Duplicate (SNS) is one byte shorter than Push_0 (SSSN). It will store the second input on the heap at the address of the first input (so if the first input is 3, it will store the second input (i.e. 5) in the heap at {3:5} instead of {0:5}). \$\endgroup\$ Commented Mar 13, 2018 at 8:57
  • \$\begingroup\$ Ignore my comment above. Apparently you cannot use an negative integer as heap-address, so it would fail if the first STDIN is negative. \$\endgroup\$ Commented Mar 13, 2018 at 10:31
  • \$\begingroup\$ @KevinCruijssen Interesting try, though. And I wouldn't be surprised if at least one existing Whitespace interpreter does allow negative heap addresses... \$\endgroup\$
    – aschepler
    Commented Mar 13, 2018 at 13:04
1
\$\begingroup\$

Perl 5, 7 bytes

6 bytes of code + 1 for -p flag

$_*=<>

Try it online!

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

Lua, 32 bytes

i=io.read
print(i("*n")*i("*n"))

Try it online!

Program reads 2 numbers from STDIN and prints the result to STDOUT.


"But shouldn't you use io.write?"

Well according to the documentation,

[...] write uses the current output file, whereas print always uses the standard output. Finally, print automatically applies tostring to its arguments, so it can also show tables, functions, and nil.

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

Go, 84 bytes

package main
import."fmt"
func main(){a,b:=0,0;Scanf("%d%d",&a,&b);Printf("%d",a*b)}

Try it online!

Worth mentioning:

  • We import the fmt package to the global namespace by using import.. That way, we can refer to Scanf and Printf as global functions.

  • a and b are assigned to 0, as that is shorter than doing var a,b int.

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

Go, 28 bytes

func(a,b int)int{return a*b}

Try it online!

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

Ruby, 10 bytes

:*.to_proc

Try it online!

I really thought this might be shorter...

\$\endgroup\$
2
  • \$\begingroup\$ ->a,b{a*b} works too lol \$\endgroup\$
    – anna328p
    Commented Oct 7, 2017 at 20:11
  • \$\begingroup\$ Yeah, I thought I'd try something different. :P \$\endgroup\$ Commented Oct 8, 2017 at 3:09
1
\$\begingroup\$

C#, 27 bytes

delegate(a,b){return a*b;};
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! You can make this shorter with (a,b)=>a*b, or use currying to get a=>b=>a*b. However, someone has already posted this solution. \$\endgroup\$
    – Pavel
    Commented Nov 2, 2017 at 1:57
1
\$\begingroup\$

Aceto, 6 bytes

riri*p

ri grabs input and converts to integer * multiplies them p prints it Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Also works in CJam, interestingly. \$\endgroup\$ Commented Mar 11, 2018 at 23:35
1
\$\begingroup\$

QBIC, 13 bytes

::[a|p=p+b}?p

Adapted my code from 'add two numbers'. This starts a loop and adds 'y' to itself 'x' times.

But seriously, a 6-byte solution is ::?a*b. : gets a cmd line parameter and class it 'a', the next : does the same for 'b', 'cause 'a' is already taken. * multiplies, and ? prints the result. This is virtually identical to this answer, only the operator is different.


Since some time now, QBIC can in-line the 'get var from cmd line'-command, and the above would be ?:*:, at 4 bytes.

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

Forked, 5 bytes

$$*%&

Try it online!

  • $$ - read two integers
  • * - multiply top two stack values
  • % - print top of stack as integer
  • & - "terminate", prevent IP from wrapping
\$\endgroup\$
1
\$\begingroup\$

FRACTRAN, 22 bytes

78/55 5/3 1/5 11/2 5/7

Take 7^a*11^b as input, return 11^b*13^(a*b).

Try it online!

This is a golfed version of the wikipedia example.

I'm not sure whether or not extra bytes should be added for input encoding and output decoding.

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

Brainfuck, 76 bytes

Not the shortest but my own solution, written a long time ago.

Golfed

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

Explanation

x = x*y
(x)(y)(temp0)(xtemp)(ycount)
Own work! Convoluted solution

,               x
> +++           y=3 (or input)

>[-]>[-]>[-]    set cells to 0
<<<< [->>>+<<<] transfer x to xtemp
> [->+>>+<<<]   transfer y to temp0 and ycount
> [-<+>]        transfer temp0 to y
>>
[               while ycount != 0
 -               ycount decrement
 < [-<+<<+>>>]   transfer xtemp to x and temp0
 < [->+<]        transfer temp0 to xtemp
 >> 
]

<<<< .          x (might overflow)
\$\endgroup\$
1
\$\begingroup\$

Flobnar, 4 bytes

&
*@

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ :| this is too golfy \$\endgroup\$
    – ASCII-only
    Commented Aug 13, 2018 at 7:38
1
\$\begingroup\$

Commodore BASIC v2 (C64, VIC-20 and some PET models), 27 tokenized BASIC bytes

This version uses the limited DEF FN keyword, See description here

 0 DEFFNA(B)=A*B:INPUTA,B:PRINTFNA(B)

Commodore BASIC v2, 14 tokenized BASIC bytes

 0 INPUTA,B:PRINTA*B

Both methods are likely compatible with all 8 BIT BASIC variants on Commodore home computers, but are not tested.

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

Bourne-shell-based langs all look pretty similar:

Zsh, 11 bytes

<<<$[$1*$2]

Bash, 13 bytes

echo $[$1*$2]

Dash/POSIX sh, 15 bytes

echo $(($1*$2))
\$\endgroup\$
1
  • \$\begingroup\$ $(( )) syntax also works for ksh93. The "fish" shell does its own thing, e.g. math "$1*$2" \$\endgroup\$
    – roblogic
    Commented Apr 1, 2019 at 23:23
1
\$\begingroup\$

GFortran, 51 27 bytes

Fulfils the rules, I suppose integer overflow error is to be expected.

-24 thanks to Ørjan Johansen

Try It Online!

read(*,*)i,j
print*,i*j
end
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Seems you have some redundant stuff. (Unlike ab, ij default to integer.) \$\endgroup\$ Commented Mar 27, 2019 at 17:26
  • \$\begingroup\$ Thanks! It's been about 20 years since I wrote any real Fortran :P \$\endgroup\$
    – roblogic
    Commented Mar 27, 2019 at 23:37
1
\$\begingroup\$

3+ bytes in various methods of representing lambda calculus, taking numeral input as Church numerals.

19 Bytes, Untyped Lambda Calculus, using named arguments:

λx.λy.λz.x(y(z))

This is the most verbose version, . represents a lambda abstraction and parentheses represent an application

9 or 12 Bytes, Lambda Calculus, using De Brujin Indices:

λλλ3(2 1)

This uses De Brujin Indices instead of variable names, and function application is right-associative and implicit.

Replacing lambda symbols with backslashes (a pretty common form when Unicode is not available) yields us only 6 bytes

It's small, but we can go smaller. Using John Tromp's Binary Lambda Calculus representation, we get the binary representation 0000000111100111010, or separated into individual parts:

3 bytes, Untyped Lambda Calculus, using BLC representation

00 00 00 01 1110 01 110 10 xxxxx

Where the 5 xs are any padding bits.

Explanation

These implement the simple mult function for Church Numerals in the untyped lambda calculus.

In untyped lambda calculus, EVERYTHING is a function, including numbers. All these functions take a maximum of a single argument. So how do you represent arbitrary numbers? Church encodings!

In Church encoding, numbers are represented by repeated recursion. All numbers n are represented by a function taking an argument f, and returning a function taking an argument x, which returns f(x) called n times. For example:

0: lambda f: lambda x: x
1 = lambda f: lambda x: f(x)
2 = lambda f: lambda x: f(f(x))
3 = lambda f: lambda x: f(f(f(x)))

Etc etc.

All inputs and outputs in most lambda calculus systems are in the form of church numerals.

The multiplication function takes 3 arguments and returns the first argument called on the second called on the third:

mult = lambda m: lambda n: lambda x: m(n(x))

This in lambda calculus simply multiplies the numbers m and n together.

Calling mult(2)(3) using our previous definitions of 2 and 3 results in a function:

lambda f: lambda x: f(f(f(f(f(f(x))))))

Or 6, the result of our multiplication. All the "golfs" are really just more concise ways of representing untyped lambda calculus.

\$\endgroup\$
0

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.