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

135 Answers 135

1
\$\begingroup\$

A0A0, 28 bytes

I0A1V0O0
I0V0M0
G-2G-1G-1G-1

Adapted from my answer to Add two numbers.

Each number is first put into two separate operands (V0 instructions) on two lines via the I0 instructions on each line and then later combined together for adding. After taking input, we jump to the top. The A1 adds the V0 O0 onto the next line, to get the following code: V2 M0 V1 O0. I've labelled the operands with V1 and V2 to represent the different operands. During execution these contain the actual inputs. From there it's simple.

V2 M0 V1 O0
V2          ; operand, put into the S0 next to it
   M0       ; multiplies its value to the operand next to it (V1)
      V1    ; operand, put into the O0 next to it
         O0 ; outputs its value as a number

The line of jumps at the bottom just goes back to the correct line each time.

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

Factor, 1 byte

*

Try it online!

\$\endgroup\$
1
\$\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
    Apr 6 at 20:19
  • \$\begingroup\$ Edited the image to clarify \$\endgroup\$
    – Seggan
    Apr 6 at 20:26
  • \$\begingroup\$ I'm thinking MasterPiet acts as if the whole PNG is surrounded by black codels \$\endgroup\$
    – Seggan
    Apr 6 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
    Apr 7 at 21:34
  • \$\begingroup\$ @DLosc Apology accepted :) \$\endgroup\$
    – Seggan
    Apr 7 at 21:39
1
\$\begingroup\$

Piet + ascii-piet, 7 bytes (5×2=10 codels)

TAJsJjj

Try Piet online!

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

Z80 machine code, 5 4 bytes with djnz

In Machine Language:

81 10 fd c9

In Assembly:

;8-bit Multiply
;Multiplier in B, multiplicand in C, result in A.
Multiply:
    loop:
        add a, c
        djnz loop
    ret

What is djnz?

djnz decrements the B register (in this case the multiplier), and jumps a certain number of bytes forward or back (in this case, 2 bytes back; essentially jumping to the beginning of the loop), but only if, after being decremented, the B register isn't 0.

Some limitations

This subroutine can only perform 8-bit multiplication, so the result will overflow if it exceeds 0xff.

I believe I have no way of making it do 16-bit multiplication without increasing the byte count.

Without djnz

Here's the code before I added djnz:

In Machine Language:

81 05 20 fc c9

In Assembly:

;8-bit Multiply
;Multiplier in B, multiplicand in C, result in A.
Multiply:
    loop:
        add a, c
        inc b
        jr nz, loop
    ret

Testing

I used this snippet of code to test my submission:

ld c,4 
ld b,10 
call Multiply 
ld ($1000),a 
\$\endgroup\$
0
\$\begingroup\$

Groovy, 10 bytes

{x,y->x*y}

This is an unnamed closure.

Try it here!

\$\endgroup\$
0
\$\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\$
0
\$\begingroup\$

Ruby, 10 bytes

->a,b{a*b}

Answer is two characters too short without this text apparently.

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

ForceLang, 36 bytes

def n io.readnum()
io.write n.mult n

Another fun use of def.

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

Rust, 14

I haven't seen Rust quiet often so let's use it.

|a:u8,b:u8|a*b

This is a lambda expression that takes to 8 bit ints and multiplies them.

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

Batch, 57 bytes

set "a1=%1" && set "a2=%2" && set x=%a1%*%a2% && echo %x%

&&: execute another command.

First command: Set %a1% as the first argument

Second command: Set %a2% as the second argument

Third command: Set %x% as %a1% * %a2%.

Fourth command: Type in the value of %x%.

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

SmileBASIC, 13 bytes

INPUT A,B?A*B

Input should be given as two numbers, separated by a comma.

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

Powershell, 17 Bytes

param($a,$b)$a*$b

takes 2 numbers, returns them multiplied out.

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

Forth, 1 bytes

*

* pops off the top 2 items from the stack, multiplies them and pushes the result.

\$\endgroup\$
1
  • \$\begingroup\$ You can just use *, since it's a word/function. I added it to this polyglot answer \$\endgroup\$
    – mbomb007
    Feb 17, 2017 at 19:07
0
\$\begingroup\$

Maple, 3 bytes

`*`

Usage:

`*`(a,b);

Returns:

a*b

Using this notation, is it possible to supply any number of arguments to the multiplication operator and it will return the product of the sequence.

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

Röda 0.11, 11 bytes

f&a,b{a*=b}

Try it online!

It modifies its first argument (which must be a variable).

Röda 0.12, 7 bytes (non-competing)

{[_*_]}

Try it online!

This kind of syntax was not supported at the time the challenge was posted. It's an anonymous function that reads to numbers from the stream

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

><>, 3 + 3 = 6 bytes

*n;

Invoke as fish.py -c '*n;' -v 2 -8, which is why I only count the flag once.

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

AHK, 15 bytes

1*=%2%
Send,%1%

1 and 2 are, by default, the first two arguments passed in to a program. Sometimes you have to escape them with percent signs so it doesn't confuse the variable 1 with the number one.

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

HEX, 69 bytes non-competing

GBL;
Listen("1");
Listen("2");
Breed("1" * "2");
Scuttle("1");
Write;

I have no idea when HEX was created, but I've listed this as non-competing as the language currently has no publicly-available functional interpreter.

\$\endgroup\$
3
  • \$\begingroup\$ ... so how can it be tested? \$\endgroup\$ May 16, 2017 at 8:16
  • \$\begingroup\$ @StewieGriffin Write your own interpreter ;) \$\endgroup\$
    – Mayube
    May 16, 2017 at 8:16
  • \$\begingroup\$ On a more serious note, I'm working on finishing my HEX interpreter, and while it supports GBL, Listen, Breed, Scuttle and Write, so would work for this program, it's still missing most of HEX's features, so it's not public yet. \$\endgroup\$
    – Mayube
    May 16, 2017 at 8:19
0
\$\begingroup\$

Braingolf, 1 byte [non-competing]

*

Try it online!

Implicit input, * multiplies the last 2 number on the stack and pushes the result, implicit output.

Here's a slightly more interesting one:

Braingolf, 9 bytes [non-competing]

<2->[.]&+

Try it online!

Implicit input of x and y. Subtracts 2 from x, (because [] is a do-while and we already have one y), then duplicates y x times, finally, sums the entire stack. Implicit output.

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

Triangular, 6 bytes

$.$%*<

Try it online!

Formats into this triangle;

  $
 . $
% * <

Without directionals/no-ops, this looks like: $$*%

  • $ - read input as integer
  • * - multiply
  • % - print as integer
\$\endgroup\$
0
\$\begingroup\$

Decimal, 12 bytes

81D81D43D301

Reads two integers (separated by a space or newline), multiplies them, and prints them to STDOUT.

Ungolfed:

81D  ; builtin 1 - read INT to stack
81D  ; builtin 1 - read INT to stack
43D  ; math, multiply (postfix *)
301  ; print from stack to standard output

Try it online!

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

Casio Basic, 6 bytes

a*b

No surprises there. 3 bytes for the program, and 3 bytes to add a,b as parameters.

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

Carrot, 5 bytes

$^F*$

Takes the input separated by newlines.

Explanation:

$ //Set the string stack to the first line of the input
^ //Change to operations mode
F //Convert to float stack
* //Multiply the stack by
$ //The second line of the input
  //Implicitly output the result
\$\endgroup\$
0
\$\begingroup\$

C, 48 bytes

main(a,b){scanf("%d%d",&a,&b);printf("%d",a*b);}

New to code golf, any suggestions are welcome.

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

cQuents, 6 bytes

#|1:AB

I really need to implement a byte that simply shorthands #|1: because every "perform this operation once" challenge uses that as the beginning.

Try it online!

Explanation

#|1      Add a 1 to the end of the user's input, it will be n
   :     Mode : (sequence 1: given n, output the nth item in the sequence)
    AB   Each item in the sequence is the first input times the second input
\$\endgroup\$
0
\$\begingroup\$

Tcl, 10 bytes

expr $a*$b

Try it online!


If one states

namespace path tcl::mathop

before then * acts like a procedure:

Tcl, 7 bytes

* $a $b

Try it online!

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

QC 4 bytes

QQGN

Requires a newer version which must be compiled manually.

Q Read number from stdin and push to stack
Q Same as above
G Multiply two numbers from stack
N Pop and print number from stack
\$\endgroup\$
0
0
\$\begingroup\$

[C64, BasicV2], 11 bytes

1rEA,B:?A*B

The input can be given like:

0dA45,45
\$\endgroup\$
2
  • \$\begingroup\$ 0 INPUTA,B:?A*B Note that Commodore BASIC keyword abbreviations doesn't save BASIC bytes, only typing \$\endgroup\$ Jan 16, 2018 at 10:30
  • \$\begingroup\$ @ShaunBebbers As far I know, it is opposite: Basic programs are stored in a tokenized form in the memory, thus even if you write the whole PRINT command, it will be stored as ?. It shows as PRINT only if you LIST it. But it is not very important, because the task is to minimize the code size, not the basic byte size. \$\endgroup\$
    – peterh
    Jan 16, 2018 at 10:42
0
\$\begingroup\$

Batch File, 23 bytes

@set/ak=%1*%2
@echo %k%

Batch doesn't allow you to print the result of an operation; Thus, we are required to create a temporary variable k which contains the result of the multiplication between argument 1 and 2.

The @'s in front of the commands prevent the current path from being printed before each line on the command prompt.

\$\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.