What do you get when you multiply 6 by 9? (42)

62

6

I love math. But I can't find a single calculator that can multiply correctly. They seem to get everything right except 6*9 (It's the question to life, the universe, and everything! How could they get that wrong?!). So I want you all to write a function for me that can multiply 2 numbers correctly (and 6*9 equals 42 instead of 54. 9*6 equals 54 still).

Oh, and I'm going to have to build the source in Minecraft so... fewest bytes win!

Recap

  • Take 2 numbers as input (type doesn't matter, but only 2 items will be passed, and order must be consistent. So streams, and arrays are ok as long as they preserve the order they where passed in. I.e., a map won't work because it doesn't preserve the order)
  • Output multiple of both numbers except if they are 6 and 9, then output 42 (order matters!)
    • PS. I never was really good with counting, so I think only integers from 0 to 99 are real numbers (type used doesn't matter)
  • Fewest bytes per language wins!

Leaderboard:

var QUESTION_ID=124242,OVERRIDE_USER=61474;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:+r.match(SCORE_REG)[0],language:r.match(LANG_REG)[0].replace(/<\/?[^>]*>/g,"").trim(),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=/\d+((?=$)|(?= Bytes))/i,OVERRIDE_REG=/^Override\s*header:\s*/i;LANG_REG=/^[^,(\n\r]+/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/Sites/codegolf/all.css?v=617d0685f6f3"> <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>

Tezra

Posted 2017-06-02T20:56:54.833

Reputation: 1 371

May the input be a list of numbers? – Jonathan Allan – 2017-06-02T22:14:41.553

@MartinEnder Added rules to address that – Tezra – 2017-06-03T04:09:25.240

1@JonathanAllan Yes, added rules for that. – Tezra – 2017-06-03T04:09:49.563

74Coincidentally the question id has 42 twice in it. – Erik the Outgolfer – 2017-06-03T06:00:18.550

2if only integers up to 99 are real numbers, then e.g. 9 * 11 is a valid prompt but 10 * 10 is not, right? – Please stop being evil – 2017-06-03T08:23:34.537

@thedarkwanderer. 0 to 99 is the valid input, 0 to 9999 is the valid output – Tezra – 2017-06-03T13:45:16.377

14@EriktheOutgolfer …and the 12 stands for what to subtract if input is 6 and 9. – Adám – 2017-06-03T23:43:26.960

1@Adám Just the absolutely perfect question id for this question. – Erik the Outgolfer – 2017-06-04T07:16:01.110

11@EriktheOutgolfer Even if you read the ID backwards. – T. Sar – 2017-06-05T12:57:47.867

142 is 54 in base 13. Note that 6 and 9 are valid in base 13 One answer has that, surprised not more – Michael Durrant – 2017-06-05T13:24:33.813

When I saw the title, I expected it to be about finding bases where the equation is true but it turns out that already exists.

– Engineer Toast – 2017-06-05T16:03:50.217

Answers

68

Mathematica, 15 bytes

Byte count assumes Windows ANSI encoding (CP-1252).

6±9=42
±n__:=1n

Defines a binary operator ± which solves the problem. We simply define 6±9=42 as a special case which takes precedence and then add a fallback definition which makes ± equal to multiplication. The latter uses a fairly interesting golfing trick. The reason this works is actually quite elaborate and we need to look into sequences. A sequence is similar to what's known as a splat in other languages. It's basically a "list" without any wrapper around it. E.g. f[1, Sequence[2, 3, 4], 5] is really just f[1, 2, 3, 4, 5]. The other important concept is that all operators are just syntactic sugar. In particular, ± can be used as a unary or binary operator and represents the head PlusMinus. So ±x is PlusMinus[x] and a±b is PlusMinus[a,b].

Now we have the definition ±n__. This is shorthand for defining PlusMinus[n__]. But n__ represents an arbitrary sequence of arguments. So this actually adds a definition for binary (and n-ary) usagess of PlusMinus as well. The value of this definition is 1n. How does this multiply the arguments? Well, 1n uses Mathematica's implicit multiplication by juxtaposition so it's equivalent to 1*n. But * is also just shorthand for Times[1,n]. Now, n is sequence of arguments. So if we invoke a±b then this will actually become Times[1,a,b]. And that's just a*b.

I think it's quite neat how this syntax abuse lets us define a binary operator using unary syntax. We could now even do PlusMinus[2,3,4] to compute 24 (which can also be written as ±##&[2,3,4] or 2±Sequence[3,4] but it's just getting crazy at that point).

Martin Ender

Posted 2017-06-02T20:56:54.833

Reputation: 184 808

I particularly like how this is basically a patch for my 'calculator' :3 For fun, does this work for * too? :3 – Tezra – 2017-06-03T04:14:18.973

3@Tezra well, * is a built-in operator, so you'd have to Unprotect it to add further definitions, but Unprotect[Times];6*9=42 should work (can't test right now though). – Martin Ender – 2017-06-03T05:24:15.947

1Doing this to the * operator is just so evil.... I love it! >:3 – Tezra – 2017-06-03T15:51:12.037

+1 for this crazy but intersting abuse of sequence and times. It took me a minute or two to fully understand this whole code. Very cool. – Julien Kluge – 2017-06-03T22:12:27.657

1How can I upvote when there are exactly 42 others?!? Here's my "future +1" to be awarded after someone else breaks it! :-) – The Vee – 2017-06-05T15:11:00.190

@Tezra thanks for the checkmark, but this is not the shortest answer so it shouldn't be accepted. If you don't want to pin the shortest answer to the top for whatever reason it is better not to accept an answer at all. – Martin Ender – 2017-06-05T15:13:07.793

1@MartinEnder Aww; But it's the shortest Mathematica one, and my favorite so far. :3 – Tezra – 2017-06-05T15:17:03.177

The trick with ±n__ getting parsed as PlusMinus[n__] and accepting infix notation further on is my favourite Mathematica hack of all time. I don't have much but I think there'll be enough for a little bounty! – The Vee – 2017-06-05T16:14:05.237

@TheVee thank you, that's very kind. :) – Martin Ender – 2017-06-05T17:45:38.810

58

Haskell, 14 bytes

6&9=42
a&b=a*b

Try it online!

Anders Kaseorg

Posted 2017-06-02T20:56:54.833

Reputation: 29 242

25

C, 32 31 29 28 bytes

-2 thanks to Digital Trauma
-1 thanks to musicman523

#define f(a,b)a^6|b^9?a*b:42

Pretty simple. Declares a macro function f that takes two arguments, a and b.
If a is 6 and b is 9, return 42. Otherwise return a x b.

Try it online!

MD XF

Posted 2017-06-02T20:56:54.833

Reputation: 11 605

2Use ^ instead of == and adjust the logic a bit: #define f(a,b)a^6||b^9?a*b:42 - saves 2 bytes. – Digital Trauma – 2017-06-02T21:51:00.363

@DigitalTrauma Cheers :D – MD XF – 2017-06-02T21:54:34.213

1I think you can use | instead of || to save another byte, since it still has lower precedence than ^ – musicman523 – 2017-06-02T22:07:57.143

@musicman523 Thanks! Editing. – MD XF – 2017-06-02T22:09:31.150

1You should update your shortC version to take these changes as well – musicman523 – 2017-06-02T22:21:29.263

17

JavaScript (ES6), 20 bytes

x=>y=>x-6|y-9?x*y:42

Explanation:

Iff x==6 and y==9, x-6|y-9 will be 0 (falsy), and 42 will be the result.

Snippet:

f=

x=>y=>x-6|y-9?x*y:42

console.log(f(6)(9));
console.log(f(9)(6));

Rick Hitchcock

Posted 2017-06-02T20:56:54.833

Reputation: 2 461

4Very nicely done; wish I'd thought of it. +1 – Shaggy – 2017-06-02T21:55:26.747

14

Python 2, 30 29 bytes

Thanks to Jonathan Allan for saving a byte!

lambda x,y:x*[y,7][6==x==y-3]

Try it online!

Adnan

Posted 2017-06-02T20:56:54.833

Reputation: 41 965

2Save a byte using the fact that 6*7 is 42: lambda x,y:x*[y,7][6==x==y-3] – Jonathan Allan – 2017-06-02T22:18:45.640

@JonathanAllan Ohh, that's neat! Thanks! :) – Adnan – 2017-06-03T08:13:02.733

This solution also works in Python 3 – AMK – 2017-08-22T20:18:57.177

Exactly what I got! Can't seem to find any way to golf it further. – FlipTack – 2017-10-24T11:17:30.360

12

05AB1E, 15 11 9 bytes

-4 bytes thanks to @Emigna

-2 bytes thanks to @Adnan

P¹69SQi42

Try it online!

How it works

P          # multiply input
 ¹         # push first number
  69       # the number 69
    S      # split per character
     Q     # equality for both inputs
       i42 # if so, print 42
           # otherwise print product

Neil A.

Posted 2017-06-02T20:56:54.833

Reputation: 2 038

You could save 4 bytes with ‚D96SQi42ëP – Emigna – 2017-06-02T21:23:44.460

@Emigna huh... Why the , at the beginning? – Neil A. – 2017-06-02T21:26:27.280

Pairing the 2 inputs to compare only once as a list. – Emigna – 2017-06-02T21:45:18.417

I suppose 6Qs9Q* would have worked as well for the same byte count. – Emigna – 2017-06-02T21:46:39.300

Changing the input format saves 2 bytes: P¹69SQi42

– Adnan – 2017-06-03T08:33:21.650

@Adnan: Didn't see the rules changed. Thanks! – Neil A. – 2017-06-03T09:35:49.017

10

Java (OpenJDK 8), 24 22 bytes

-2 bytes thanks to @OlivierGrégoire

a->b->a==6&b==9?42:a*b

Try it online!

Bashful Beluga

Posted 2017-06-02T20:56:54.833

Reputation: 413

3Welcome to PPCG! I don't know much about Java, but could you remove the System.out.println() call and just let the function return the result? – ETHproductions – 2017-06-02T23:04:33.937

True, I thought I had to explicitly print the result. Thanks for saving me 17 bytes! – Bashful Beluga – 2017-06-02T23:14:45.060

as suggested here, using ^ will save you several bytes

– phuclv – 2017-06-04T01:23:22.850

2@LưuVĩnhPhúc not in Java, because I'd have to write (a^6|b^9)==0 since there is no implicit "different than 0" comparison. The resulting code snippet would be 27 bytes long. Anyways, thanks for the suggestion, and please tell me if I got your tip wrong. – Bashful Beluga – 2017-06-04T02:59:56.240

@CoffeeandClonazepam I'm not great at java, but doesn't every number other than 0 evaluate to true? We don't require an explicit cast to boolean, as long as the return value can be casted to the correct boolean output. – Rɪᴋᴇʀ – 2017-06-04T04:02:20.687

1

@Riker nope, it does not work like that in java. E.g. the snippet int a = 5; if (a) do_some_stuff(); else do_other_stuff(); gives a Type mismatch: cannot convert from int to boolean compilation error. They must be made explicitly with boolean values; refer to SO and ORACLE.

– Bashful Beluga – 2017-06-04T04:10:00.533

@Riker Try it online! Main.java:4: error: incompatible types: int cannot be converted to boolean

– Bashful Beluga – 2017-06-04T04:15:52.790

@CoffeeandClonazepam ah, okay. – Rɪᴋᴇʀ – 2017-06-04T13:45:41.860

3You can use currying to spare one byte, and you can get rid of the semicolon as it's not part of the lambda to spare another byte: a->b->a==6&b==9?42:a*b. – Olivier Grégoire – 2017-06-04T16:14:37.980

@OlivierGrégoire Good point, thank you! – Bashful Beluga – 2017-06-04T17:23:40.387

1Just a note why the 0 is not false. Java is type safe so 0 is an integer not a boolean and unsafe typecasting is not allowed so you can't use falsy values – Martin Barker – 2017-06-06T14:52:25.780

@MartinBarker, thanks for the explanation! – Bashful Beluga – 2017-06-06T14:55:36.613

@MartinBarker to show a concrete example, if (0) { } else { } won't compile in Java. That's why 0 isn't a valid falsy type. – Olivier Grégoire – 2017-06-09T08:19:46.580

6

Ruby, 24 bytes

->a,b{a==6&&b==9?42:a*b}

dkudriavtsev

Posted 2017-06-02T20:56:54.833

Reputation: 5 781

a^6|b^9<1 might work as boolean. Hard to test on my smartphone. – Eric Duminil – 2017-06-02T22:53:10.177

1@EricDuminil Unfortunately that expression is parsed as (((a^6)|b)^9), i.e. a.^(6).|(b).^(9), so it won't work correctly. a-6|b-9==0 would work, but that's no shorter. – Ventero – 2017-06-03T01:05:21.297

@Ventero: I didn't think about that. Thanks. a,b==6,9 would be nice, but it also doesn't work. – Eric Duminil – 2017-06-03T08:38:27.810

6

Brain-Flak, 158 154 148 140 138 126 bytes

(({}<>)(((([()()()]<>)){})<({}{}({}))>{(()()()){}(<{}>)}{}))([()]{()(<{}>)}{})(({<{}>{}((<>))}{}){}<{}>{<({}[()])><>({})<>}{})

Try it online!

Explanation

This code is pretty simple. We make copies of the top two items on the stack, we subtract 6 from one and 9 from the other. We then take the not of the two values. We and those values, multiply the result by 12. Multiply the inputs and subtract the two results.

Post Rock Garf Hunter

Posted 2017-06-02T20:56:54.833

Reputation: 55 382

2You might want to capitalize not and and (or backtick them), reading your description rather tripped me up. – MD XF – 2017-06-02T21:39:25.573

6

Jelly, 8 7 bytes

Vf96SạP

Input is as an array of two integers: first the right operand, then the left one.

Try it online!

How it works

Vf96SạP  Main link. Argument: [b, a]

V        Cast [b, a] to string, then eval the resulting string.
         For [b, a] = [9, 6], this yields 96.
 f96     Filter with 96, yielding [96] if V returned 96, [] otherwise.
    S    Take the sum, yielding either 96 or 0.
      P  Compute the product of [b, a], yielding ba = ab.
     ạ   Compute the absolute difference of the results to both sides.
         When the sum is 0, this simply yields the product.
         However, when [b, a] = [9, 6], this yields 96 - 54 = 42.

Dennis

Posted 2017-06-02T20:56:54.833

Reputation: 196 637

This has like -1 degree of freedom. How do these coincidences even occur to you? – lirtosiast – 2018-11-29T08:46:41.270

6

Factorio, 661 bytes, 6 combinators with 9 connections

There is one constant combinator set to output A and B. Change these to set the input.

Blueprint string (0.15.18):

0eNrNVm2O2jAQvcv8rEKFvSHLRuqPtrfYCkUhGWAkYkfOGDVCOUBv0bP1JLWTLQuB3U0QbfcPYvzxZt68eYr3sNxaLA0phngPlGlVQfxtDxWtVbr1a1yXCDEQYwEBqLTwUY4Z5WgmmS6WpFLWBpoASOX4HWLRBG8C+EScKr6MIJtFAKiYmLCrpw3qRNliicaleK2SAEpduata+fQObiI+zgKo/R+XIyeDWbcrA18IG71NlrhJd+RuuytPmInby1ucyq+uyFScnPHakWHrVg4VdScmnz2fPzQhjnxQlKlpS4zhk7ugLZd2BCTu0NS8IbXusMvalWgVJyuji4SUA4OYjcWmS606nm31wv8YzI+7SS66axbusHxh1zeITGaJ21C4w41XtyeHHCXH9D+o8eVUjYd3qoY47bc86rWPo158/yze2iCqPtxsmHx3r9ry3E6ylU9cTUv0aITDygwPZaaGeFMgUzbM99NBg/aMegPnV+gxRg6oLtFNZFsjfLhiJB+huZn1B87O7Crr/0Pnfz11vug5/9ePn+/E+2Hf++4beNHV8uzgRWWica6ejnDKiraM5oWXwhtC2CcVDo+FxfAWDfwc3Y9jLv4288cj5qG8IXU3Ie2zKj56xgXgZrNqOURhKGfR/GE6nzfNb7OMaxo=

The output is signal Z and is to be taken from the top and bottom deciders.

Screenshot

latias1290

Posted 2017-06-02T20:56:54.833

Reputation: 91

waaa... first time i ever see a factorio answer in ppcg xD – V. Courtois – 2017-09-12T21:52:42.260

2Not golfy enough. – jimmy23013 – 2018-04-01T14:04:42.253

5

MATL, 11 bytes

[BE]=?42}Gp

Input is an array with the two numbers.

Try it online!

Explanation

[BE]    % Push array [6, 9]
=       % Implicit input: array of two numbers. Compare with [6, 9] element-wise
?       % If the two entries are true
  42    %   Push 42
}       % Else
  G     %   Push input
  p     %   Product of array
        % Implicit end. Implicit display

Luis Mendo

Posted 2017-06-02T20:56:54.833

Reputation: 87 464

5

R, 33 bytes

function(a,b)`if`(a-6|b-9,a*b,42)

Returns a function.

Try it online!

Giuseppe

Posted 2017-06-02T20:56:54.833

Reputation: 21 077

5

Factorio, 581 bytes, 3 combinators with 4 connections

Blueprint string (0.16.36):

0eNqllNtu4jAQht9lLldmldNCFWkvto/RCkUhGWAkYkfOGDVCefeOnV1Km7ACemPJ9vibf04+webgsLWkGfITUGV0B/nrCTra6fLgz7hvEXIgxgYU6LLxO2/HpeZFZZoN6ZKNhUEB6RrfII+HtQLUTEw44sKmL7RrNmjF4AyqsaIa7SVHQWs6eWq0dy+46OcvBT3ki1hc1GSxGi8T5XWwNYdig/vySPJYXvxFFnJXB0znT7dkOy4mYR3JspOTs6DRYoFHtD3vSe98XP/CFZ9xtsqe0mW29KdNW9qgOYffgjCOW3eHk+eR3fai1WkuttY0BWlhQM7W4TC61mPAIYzYLxbry6yS7FKxJFs54rANFdhZRP3VMBnWQk08ZvZ+ChpExqSCyX9bYVLCRfxRwbmabenAaK+03rX0/RnT5z7VJbroQnUH7HkGlq7OsDFtc8WYzWJ8WxbTs4rSEu8bZKpuGoXopkn4gH5vGEKiO/SMO5vbtCgDEjTCjwcm5AWGO4ZgknX16Tq7OhRfHiZXypU91PTRd6ZYdIjo8PnmF3+1AvmfuuBq+bRKYmnWKM2G4R1hAPnz

The bottom left constant combinator should be set to output A and B as input. The output is signal Z from the bottom right arithmetic combinator.

enter image description here

Top left: 2147483640 A, 2147483637 B
Top right: If everything = 2147483646 output B, input count
Bottom left: (input) A, (input) B
Bottom right: A * B -> Z

jimmy23013

Posted 2017-06-02T20:56:54.833

Reputation: 34 042

4

GW-BASIC, 55 bytes

1INPUT A:INPUT B
2IF A=6THEN IF B=9THEN ?"42":END
3?A*B

Output:

output

The first machine at pcjs has IBM BASIC, which is practically the same thing. To test this, head over there, hit Run on the machine, Press Enter-Enter and type BASICA to get into BASIC mode. Then enter the source code (it will automatically prettyprint for you), type RUN, input two integers, and done!

MD XF

Posted 2017-06-02T20:56:54.833

Reputation: 11 605

3Are you sure the bytecount is correct? GW-BASIC uses an encoding in which some words, like INPUT, are encoded in fewer bytes than the characters that make them up would suggest. The count therefore seems high to me. – None – 2017-06-02T21:33:19.137

@ais523 Well, I ran it through wc, and I got 55... Copy-pasted into my emulator and it had the expected behavior. – MD XF – 2017-06-02T21:34:19.110

3Right, my point is that you're probably scoring your submission higher than it needs to be. Get GW-BASIC to save the file, and then look at the size of the resulting file on disk; it should be smaller. – None – 2017-06-02T21:35:25.380

@ais523 Saved as OUT.BAS: https://i.stack.imgur.com/32eH1.png Bytecount is the middle value.

– MD XF – 2017-06-02T21:38:26.243

OK, I wasn't expecting that, but I guess it's a wash in this situation. (Or perhaps there's more than one save format?) – None – 2017-06-02T21:45:55.213

@ais523 Well, if there was more than one instruction to save, then maybe there would be. But literally the only way to save is SAVE "FILENAME". ¯\(ツ) – MD XF – 2017-06-02T21:47:02.100

Turns out that there is in fact more than one way to save, but SAVE is the one that uses the binary format; the other is LIST , filename. So 55 is probably unbeatable. – None – 2017-06-02T21:49:21.513

@ais523 Would that one have a shorter bytecount? – MD XF – 2017-06-02T21:49:41.233

Doesn't really matter how big is the file. What matters is the image in memory. @ais523 is right in that old BASICs used to store programs in tokens rather than as simple text. – ysap – 2017-06-04T15:22:19.110

4

Check, 34 33 bytes

.:+&>#v
#>42#v#9-!\>6-!*?
 d* ##p

Check is my new esolang. It uses a combination of 2D and 1D semantics.

Input is two numbers passed through command line arguments.

Explanation

The stack starts with the command line arguments on it. Let's call the arguments a and b.

The first part, .:+&, essentially duplicates the stack, leaving it as a, b, a, b. > pushes 0 to the stack (it is part of a numeric literal completed by 9).

# switches to 2D semantics, and v redirects the IP downwards. The IP immediately runs into a #, which switches back to 1D semantics again.

9-! checks whether b is equal to 9 (by subtracting 9 and taking the logical NOT). \>6-! then checks whether a is equal to 6. The stack now contains a, b, 1, 1 if and only if b==9 and a==6. Multiplying with * takes the logical AND of these two values, giving a, b, 1 if the inputs were 6 and 9, and a, b, 0 otherwise.

After this, the IP runs into a ?. This will switch to 2D mode if the top stack value is nonzero, and otherwise will continue in 1D mode.

If the top stack value was 1, this means that the other stack values are 6 and 9, so we push 42 to the stack with >42 and then move down to the second # on the last line.

If the top stack value was 0, then execution moves down to the next line. d removes the 0 (as ? does not do so), and then we multiply the two inputs with *. The ## switches in and out of 2D mode, doing nothing.

The branches have now joined again. The stack either contains 6, 9, 1, 42, or a*b. p prints the top stack value and then the program ends, discarding the rest of the stack.

Esolanging Fruit

Posted 2017-06-02T20:56:54.833

Reputation: 13 542

This looks like a nifty language! – Not a tree – 2017-06-03T13:12:10.550

3

Python 3, 36 33 bytes

lambda x,y:42if x==6==y-3else x*y

Try it online!

Martmists

Posted 2017-06-02T20:56:54.833

Reputation: 429

1Alternate version, same size: lambda x:x[0]*x[1]if x!=(6,9)else 42. The only difference is the input type. – musicman523 – 2017-06-02T21:46:31.003

Nevermind - your edited version is shorter :) – musicman523 – 2017-06-02T21:58:23.603

The code you posted doesn't actually work, so I switched it to the working version on TIO that you linked to. – ETHproductions – 2017-06-02T22:49:31.320

It works for me: In [1]: f=lambda x,y:42if 6==x==y-3else x*y In [2]: f(6,9) Out[2]: 42 In [3]: f(9,6) Out[3]: 54 @ETHproductions – Martmists – 2017-06-02T22:56:05.517

@Martmists You were missing a space then, because your code was f=lambda x,y:42if6==x==y-3else x*y – ETHproductions – 2017-06-02T22:58:13.063

3

Japt, 13 11 12 bytes

¥6&V¥9?42:N×

Try it online

  • 2 1 bytes saved thanks to obarakon.

Shaggy

Posted 2017-06-02T20:56:54.833

Reputation: 24 623

1N¬¥69?42:N× for 11 bytes – Oliver – 2017-06-03T01:54:08.520

Nice one, @obarakon. – Shaggy – 2017-06-03T12:44:27.253

3

JavaScript (ES6), 25 bytes

x=>y=>[x*y,42][x==6&y==9]

Shaggy

Posted 2017-06-02T20:56:54.833

Reputation: 24 623

3

Standard ML (MLton), 22 20 bytes

Saved 2 bytes thanks to @Laikoni!

fun$6 $9=42| $x y=x*y

Try it online!

This is the kind of thing SML is meant for, which is why it beats shortC and Python.

The old version looked much nicer. :P

musicman523

Posted 2017-06-02T20:56:54.833

Reputation: 4 472

20 bytes: Try it online!

– Laikoni – 2017-06-23T13:29:11.080

@Laikoni Wow, I had no idea you could use $ as an identifier! Why doesn't this compile if you remove the space between | and $? – musicman523 – 2017-06-23T14:14:44.043

SML distinguishes alphanumeric and symbolic identifiers, which can be quite handy for golfing. |$ is parsed as a single symbolic identifier, so everything breaks. I plan to write a tips question for SML soon and add an answer about those two types of identifiers. – Laikoni – 2017-06-23T15:21:37.273

3

APL (Dyalog), 10 bytes

×-12×6 9≡,

Try it online!

× the product (of the arguments)

- minus

12× twelve times

6 9≡ whether (6,9) is identical to

, the concatenation (of the arguments)

Adám

Posted 2017-06-02T20:56:54.833

Reputation: 37 779

Oh wow I just saw this and my J answer is exactly the same as this :/ except one byte longer – Conor O'Brien – 2017-06-05T00:36:55.813

@ConorO'Brien Makes sense. J and tacit APL are mostly equivalent, save for J's multi-char primitives (and needing Cap for a final atop). – Adám – 2017-06-05T00:39:05.243

3

R, 41 I think I don't know how to count bytes I am new :D

function(a,b){

if(a==6&b==9){42} else {a*b}

}

I define a funtion whose arguments are a and b in this order. If a equals to 6 and b equals to 9 it returns 42, otherwise, a times b

R17

Posted 2017-06-02T20:56:54.833

Reputation: 31

Welcome to the site! :) – James – 2017-06-06T16:07:27.547

Welcome. Take into account that newlines and spaces count too. In your case, if you remove newlines and spaces (which you can) it's only 41. – Masclins – 2017-06-12T14:27:04.130

1You can cut two bytes by using ifelse(a==6&b==9,42,a*b) – Masclins – 2017-06-12T14:33:32.633

You can cut the whole thing down to 33 bytes as function(a,b)\if`(a-6|b-9,a*b,42)`. – rturnbull – 2017-06-12T14:45:02.220

This is only 41 bytes after you remove the unneeded whitespace, unil then, it's 47 bytes. – Pavel – 2017-07-11T01:24:32.900

3

SPL, 356 bytes

a.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]Ajax:Listen to your heart!Puck:Listen to your heart!Are you as big as the sum of a big big big cat and a cat?If so, am I as big as the sum of a big big cat and a big cat?If so, you are as big as the product of I and the sum of I and a cat.If not, you are as big as the product of you and I.Open your heart

With newlines and spaces:

a.                       *Title*
Ajax,.                   *Declare variable Ajax*
Puck,.                   *Declare variable Puck*
Act I:.
Scene I:.
[Enter Ajax and Puck]
Ajax: Listen to your heart!                  *Set Puck's value to user input*
Puck: Listen to your heart!                  *Set Ajax's value to user input*
      Are you as big as the sum of a big 
       big big cat and a cat?                *Is Ajax=9?* 
      If so, am I as big as the sum of a 
       big big cat and a big cat?            *Is Puck=6?* 
      If so, you are as big as the product 
       of I and the sum of I and a cat.      *If so, set Ajax=42* 
      If not, you are as big as the product 
       of you and I.                         *If not set Ajax=(Ajax)(Puck)*
      Open your heart                        *Print Ajax's value*

William Taylor

Posted 2017-06-02T20:56:54.833

Reputation: 31

2

shortC, 23 bytes

Df(a,b)a==6&b==9?42:a*b

Try it online!

MD XF

Posted 2017-06-02T20:56:54.833

Reputation: 11 605

I think you can save 2 bytes: Try it online!

– Conor O'Brien – 2017-09-12T22:10:46.293

2

Pyth, 12 bytes

-*FQ*12q(6 9

Try it online

Explanation

 -*FQ*12q(6 9
  *FQ             Take the product
        q(6 9)Q   Check if the (implicit) input is (6, 9)
 -   *12          If so, subtract 12

user48543

Posted 2017-06-02T20:56:54.833

Reputation:

Clever solution. I tried it with ternary statements in 15: AQ?&q6Gq9G42*GH – Tornado547 – 2017-12-14T02:54:31.790

2

S.I.L.O.S, 81 67 bytes

readIO
J=i
readIO
a=(J-6)^2+(i-9)^2
a/a
a-1
a*12
x=J*i+a
printInt x

Try it online!

In some sense addition functions as an interesting NAND gate in SILOS.

-14 bytes thanks to @Leaky Nun

Essentially we create a number "a" which is 0 (falsy) iff j is 6 and i=9, then we divide it by itself subtract one and multiply it by 12 in order to add to our product.

If "a" was 1 after subtracting one and multiplying, it becomes a no-op, however in the case where a is 0, 0/0 silently throws an error (which is auto-magically caught) a becomes 0, and then becomes -1 and we end up subtracting 12 from our product.

Rohan Jhunjhunwala

Posted 2017-06-02T20:56:54.833

Reputation: 2 569

69 bytes – Leaky Nun – 2017-06-03T03:02:05.543

67 bytes – Leaky Nun – 2017-06-03T03:06:31.490

@LeakyNun ooh, that's clever. – Rohan Jhunjhunwala – 2017-06-03T11:09:39.073

Actually, 0/0 becomes 0. – Leaky Nun – 2017-06-03T12:43:53.620

@LeakyNun I meant to say becomes 0, and then decremented. Fixing. – Rohan Jhunjhunwala – 2017-06-03T13:14:12.027

2

Octave, 23 bytes

@(x,y)x*y-12*~(x-6|y-9)

Try it online!

Explanation

~ is logical NOT, and | is logical OR. So, in pseudocode, this computes

x*y - 12*NOT(x-6 OR y-9)

Luis Mendo

Posted 2017-06-02T20:56:54.833

Reputation: 87 464

2

Jelly, 10 bytes

⁼6,9ȧ42ȯ⁸P

A monadic link taking a list of the two numbers.

Try it online!

How?

⁼6,9ȧ42ȯ⁸P - Link: list of numbers [a,b]
 6,9       - 6 paired with 9, [6,9]
⁼          - equals? (non-vectorising) (1 or 0)
     42    - literal answer, 42
    ȧ      - logical and               (42 or 0)
        ⁸  - link's left argument, [a,b]
       ȯ   - logical or                (42 or [a,b])
         P - product                   (42 or a*b)

Jonathan Allan

Posted 2017-06-02T20:56:54.833

Reputation: 67 804

You could've just used ?, like I did. ;) – Erik the Outgolfer – 2017-06-03T05:52:34.280

Ah because , is special in that it is part of the literal regex pattern, so 6,9 is parsed as a single token and the quick $ can combine it with . Did you reason that, or just try it and notice that it worked? – Jonathan Allan – 2017-06-03T19:53:07.133

1I reasoned that. – Erik the Outgolfer – 2017-06-04T06:52:06.890

2

Retina, 36 bytes

^6 9$
6 7
\d+
$*
1(?=1* (1+))|.
$1
1

Try it online! Standard unary multiplication, just alters the input to handle the special case.

Neil

Posted 2017-06-02T20:56:54.833

Reputation: 95 035

2

PHP, 35 bytes

<?=([$a,$b]=$_GET)==[6,9]?42:$a*$b;

user63956

Posted 2017-06-02T20:56:54.833

Reputation: 1 571

1Sadly this fails for more than just 6 * 9 e.g. 7 * 9 = 49. – Christoph – 2017-06-04T09:50:11.357

couldn't get the above to work, this works <?=($a=$_GET)==[6,9]?42:$a[0]*$a[1]; – inarilo – 2017-06-05T12:51:54.890

1

@inarilo It requires PHP 7.1: http://sandbox.onlinephpfunctions.com/code/e62c70dfdf3fe0927b3d0a18124429c7a49385e7.

– user63956 – 2017-06-05T12:56:11.197

@inarilo $b!=9|$a!=6 could have solved the problem ... but at what cost! – Titus – 2017-06-12T16:13:28.720

2

Convex, 16 14 13 bytes

_6 9¶=\:*42¶=

Try it online!

GamrCorps

Posted 2017-06-02T20:56:54.833

Reputation: 7 058

2

Jelly, 9 bytes

42P⁼6,9$?

Try it online!

Takes list of numbers as input.

Erik the Outgolfer

Posted 2017-06-02T20:56:54.833

Reputation: 38 134

2

C#, 144 bytes

class I{I(int x)=>v=x;int v;public static int operator*(I a,I b)=>a.v==6&&b.v==9?42:a.v*b.v;public static implicit operator I(int x)=>new I(x);}

Run in C# Pad

Usage: (I)6*9

Explanation

class I {
    // Constructor saves the int value in field v
    I(int x) => v = x;
    int v;

    // Redefine the multiplication operator
    public static int operator*(I a, I b) => 
        a.v==6 && b.v==9 ? // If the values are 6 and 9,
            42 : // return 42, else
            a.v * b.v; // return the normal multiplication

    // Add implicit conversion from int to I, so `I x = 5;` is valid
    public static implicit operator I(int x) => 
        new I(x);
}

Usage:

(I)6*9; // Convert 6 to I. To be able to apply the operator, 9 will
        // be implicitly converted as well.

It's not the shortest possible approach within the challenges terms, but I figured it interesting enough to post.


C#, 25 bytes, but not as fun

(x,y)=>x==6&&y==9?42:x*y;

Run in C# Pad

Arthur Rump

Posted 2017-06-02T20:56:54.833

Reputation: 81

Why not use a lambda? You could probably implement one in a small fraction of the bytes as this solution. – None – 2017-06-05T00:13:28.843

This is code golf not a business solution. Can you at least try to golf this solution? – TheLethalCoder – 2017-06-05T08:39:15.843

2

J, 11 bytes

*-12*6 9-:,

Try it online!

Surprisingly elegant.

*-12*6 9-:,
     6 9-:,     see if 6 9 matches the paired input
  12*           multiply that by 12
 -              and subtract this from
*               the product of the inputs

Conor O'Brien

Posted 2017-06-02T20:56:54.833

Reputation: 36 228

2

Prolog, 45 Bytes

m(6,9,42).
m(X,Y,Z):-(X=\=6;Y=\=9),Z is X*Y.

s3attar

Posted 2017-06-02T20:56:54.833

Reputation: 31

Welcome to the site! Always good to see someone golfing in Prolog :). I have edited this answer to include code formatting. – Post Rock Garf Hunter – 2017-06-05T16:15:14.647

@WheatWizard Thank you :). Yes I've noticed it's a very rare occurrence :D – s3attar – 2017-06-07T00:40:55.700

2

x86_64 machine language for Linux, 20 19 18 bytes

00:       83 fe 09                cmp    $0x9,%esi
03:       75 09                   jne    0xe
05:       31 c0                   xor    %eax,%eax
07:       83 ff 06                cmp    $0x6,%edi
0a:       b0 2a                   mov    $0x2a,%al
0c:       74 03                   je     0x11
0e:       96                      xchg   %esi,%eax
0f:       f7 ef                   imul   %edi
11:       c3                      retq

If you want to Try it online! for youself, compile and run the following C program

#include<stdio.h>
#include<math.h>

int h(int a,int b){return a-6|b-9?a*b:42;}
const char hh[]="\x83\xfe\tu\t1\xc0\x83\xff\6\xb0*t\3\x96\xf7\xef\xc3";
int main(){
  for( int f=-10; f<10; f++ ) {
    for( int g = -10; g<10; g++ ) {
      printf( "%d %d %d %d\n", f, g, h(f, g), ((int(*)(int,int))hh)(f,g) );
    }
  }
}

ceilingcat

Posted 2017-06-02T20:56:54.833

Reputation: 5 503

2

Python3, 59 bytes

-1 bytes thanks to Stephen

a=input();print(42if a=='6 9'else eval(a.replace(' ','*')))

Try it online!

motatoes

Posted 2017-06-02T20:56:54.833

Reputation: 121

@StephenS thanks – motatoes – 2017-06-06T15:25:38.143

2

><>, 16 bytes

:{:}*}9=$6=*c*-n

Try it online!

Pretty simple. Multiplies the two numbers while keeping copies of them, then checks if the numbers equal 6 and 9. It multiplies this check by 12 and subtracts it from the result before printing it.

Jo King

Posted 2017-06-02T20:56:54.833

Reputation: 38 234

2

Brachylog, 8 bytes

c69∧42|×

Try it online!

A predicate which takes a pair of numbers as its input variable and outputs their correct product through its output variable.

            If the input
c           concatenated
 69         is 69,
   ∧42      then output 42,
      |     otherwise
       ×    output the product of all numbers in the input.

I have no clue why [0,69]c69 fails, but I am very glad that it does! (Running 69~cᶠw confirms that [6,9] is the only two-element list that concatenates to 69.)

Unrelated String

Posted 2017-06-02T20:56:54.833

Reputation: 5 300

1

JavaScript, 23 22 bytes

x=>y=>x==6&y==9?42:x*y

-1 byte thanks to Shaggy

f=
x=>y=>x==6&y==9?42:x*y

console.log(f(6)(9))
console.log(f(9)(6))

Stephen

Posted 2017-06-02T20:56:54.833

Reputation: 12 293

1Save a byte by using a single &. – Shaggy – 2017-06-03T10:27:53.033

1

Ohm, 11 bytes

*┼6E┘9E&?42

Try it online!

*            Implicit inputs. Multiply
 ┼6E         First input == 6?
    ┘9E      Second input == 9?
       &     Logical and
        ?42  If true push (and output) 42
             Else implicit output the product

FrodCube

Posted 2017-06-02T20:56:54.833

Reputation: 539

1

Bash, 24

echo $[$1$2-69?$1*$2:42]

Saves a couple of bytes by just doing one test - checking if the string concatenation of the inputs is 69.

Try it online.

Digital Trauma

Posted 2017-06-02T20:56:54.833

Reputation: 64 644

1

Batch, 30 bytes

@cmd/cset/a%1*%2-12*!(%1%2-69)

Subtracts 12 if the input numbers were 6 and 9.

Neil

Posted 2017-06-02T20:56:54.833

Reputation: 95 035

1

dc, 22 bytes

[42 1]sX[k6=X]sZd9=Z*p

Try it online!

Maxim Mikhaylov

Posted 2017-06-02T20:56:54.833

Reputation: 571

1

GNU sed, 98 75 + 1 = 99 76 bytes

+1 byte for -r flag. Takes input as two unary numbers separated by a newline. -23 bytes thanks to Ephphatha.

y/1/0/
N
s/^(0{6}\n1{7})11$/\1/
h
s/0+//
x
s/1+//
:
s/0//;TX
G;b
:X
s/\n//g

Try it online! (Delete the # in the "Footer" section if you don't want to count 1s by hand.)

Explanation

Almost a third of this code (line 3 below) is devoted to converting 6×9 to 6×7. The rest is unary multiplication, which is easy: To multiply a and b, just replace each digit in a with the digits of b.

y/1/0/    # Replace 1s on this line with 0s
N         # Append next line to this line
s/^(0{6}\n1{7})11$/\1/  # If the inputs are 6 and 9,, replace the 9 with 7
h         # Copy to hold space
s/0+//    # Delete 0s (1st argument)
x         # Swap pattern and hold space
s/1+//    # Delete 1s (2nd argument)
:
  s/0//;TX  # Delete a 0; if there weren't any, branch to :X
  G;b       # Append a copy of the hold space (1s) to pattern space; branch to :
:X
s/\n//g   # Remove all newlines

Jordan

Posted 2017-06-02T20:56:54.833

Reputation: 5 001

"and 69 equals 42 instead of 54. 96 equals 54 still" - you can save some bytes by only handling the 6\n9 order - s/^(0{6}\n)11(1{7})$/\1\2/ – Ephphatha – 2017-06-04T11:37:21.237

s/^(0{6}\n1{7})11$/\1/ should work as well – Ephphatha – 2017-06-05T05:27:40.517

1

GolfScript, 18 bytes

."6 9"={;42}{~*}if

Try it online!

Strict input format: numbers on a single line separated by a single space, nothing else.

Erik the Outgolfer

Posted 2017-06-02T20:56:54.833

Reputation: 38 134

1

F#, 25 bytes

function 6,9->42|x,y->x*y

Try it!

Asik

Posted 2017-06-02T20:56:54.833

Reputation: 161

@WheatWizard May be of help: https://tio.run/#fs-mono

– Beta Decay – 2017-06-05T12:16:30.813

@WheatWizard I know, I just thiught you didn't have a method of running F# programs – Beta Decay – 2017-06-05T13:44:00.003

1@WheatWizard I added a link where you can run the code. The code contains more stuff (binding the function to an identifier, calling it, printing the result) so you can see what's going on. – Asik – 2017-06-05T14:34:31.593

You can get rid of the () around x,y and 6,9 it is not necessary. – Post Rock Garf Hunter – 2017-06-05T14:39:27.603

1

Noether, 24 bytes

{I~a6=I~b9=&}{42P}{ab*P}

Try it here!

Explanation:

{           }            - If
 I                       - Push user input
  ~a                     - Store top item of stack in variable a
    6                    - Push 6
     =                   - Pop top two items and return 1 if they are equal
      I                  - Push user input
       ~b                - Store top item of stack in variable b
         9               - Push 9
          =              - Pop top two items and return 1 if they are equal
           &             - Pop top two items and bitwise AND
             {   }       - If true then
              42         - Push 42
                P        - Print top item
                  {    } - Else
                   a     - Push variable a onto the stack
                    b    - Push variable b onto the stack
                     *   - Pop top two items and multiply
                      P  - Print top item

Beta Decay

Posted 2017-06-02T20:56:54.833

Reputation: 21 478

What do I put in input to make this work? – Tezra – 2017-06-18T15:15:42.987

@Tezra Hold on I'll edit the link – Beta Decay – 2017-06-18T16:01:48.697

@Tezra See the new link :D – Beta Decay – 2017-06-18T16:04:02.043

1

PowerShell, 42 bytes

Appropriately, but not very impressively, a 42 byte scriptblock function which is a basic if/else made from array indexing with a little number-to-string type coercing happening:

$f={param($a,$b)(($a*$b),42)["$a$b"-eq69]}

e.g.

PS C:\> & $f 6 9
42

PS C:\> & $f 9 6
54

Previous approach, 44 bytes:

$f={(($a=$args-join"*"),42)[$a-eq'6*9']|iex}

TessellatingHeckler

Posted 2017-06-02T20:56:54.833

Reputation: 2 412

1

Prolog

multiplication(6, 9, 42) :- !.
multiplication(X, Y, Z) :- Z is X * Y.

Ugly Prolog, 33 bytes

m(6,9,42):-!.
m(X,Y,Z):-Z is X*Y.

Test it here

pluraliseseverythings

Posted 2017-06-02T20:56:54.833

Reputation: 21

It looks like you have a good deal of extra whitespace here. You could save some bytes by removing almost all of the whitespace in this answer. – Post Rock Garf Hunter – 2017-06-06T14:55:02.113

Oh but I like white spaces and bytes. It wouldn't be Prolog if it was optimised under any metric and not pretty. – pluraliseseverythings – 2017-06-06T15:02:38.200

5This is a [tag:code-golf] competition. The goal is to minimize bytes. – Post Rock Garf Hunter – 2017-06-06T15:05:18.093

You need to take input of a sort. How does this work? Does the second line define a function? – Rɪᴋᴇʀ – 2017-06-06T15:11:11.317

OkIAddedANewVersion! – pluraliseseverythings – 2017-06-06T15:11:25.583

The input is of the type

:- m(5,6,Z) – pluraliseseverythings – 2017-06-06T15:15:54.633

In the answer, it helps to explain the code (Second line is a rule, first line overrides that rule, call function via m(#,#,Z)) Also, welcome to code-golf. I have verified that your answer is correct. ^^ Sorry you have to make your Prolog ugly, but in code-golf, if it looks like code, someone else will beat you. =P – Tezra – 2017-06-06T16:34:41.183

1

TI-Basic, 16 15 13 bytes

prod(Ans)-12prod(Ans={6,9

Value is returned as from any function.

Timtech

Posted 2017-06-02T20:56:54.833

Reputation: 12 038

Input :XY-12(X=6 and Y=9 is a byte shorter. – lirtosiast – 2017-07-11T01:05:47.260

@lirtosiast Thanks! – Timtech – 2017-07-11T01:07:22.163

prod(Ans)-12prod(Ans={6,9 using input in Ans for 13 bytes – pizzapants184 – 2017-12-12T16:45:56.970

@pizzapants184 Smart! That's probably the optimal solution. – Timtech – 2017-12-14T00:38:52.590

1

q/kdb+, 19 17 bytes

Solution:

{(prd x;42)x~6 9}

Example:

q){(prd x;42)x~6 9}6 9
42
q){(prd x;42)x~6 9}6 10
60
q){(prd x;42)x~6 9}9 6
54

Explanation:

If the input is the exact list (6;9) return 42 else multiply together:

{(prd x;42)x~6 9} / the solution
{               } / lambda function
           x~6 9  / is input the list (6;9) (0b or 1b)
 (     ;  )       / two item list 
  prd x           / if false (index 0) then multiply together
        42        / if true (index 1) then return 42

streetster

Posted 2017-06-02T20:56:54.833

Reputation: 3 635

1

Excel, 28 bytes

Takes inputs in A1 and B1

=IF(AND(A1=6,B1=9),42,A1*B1)

Wernisch

Posted 2017-06-02T20:56:54.833

Reputation: 2 534

1

Commentator, 223 bytes

//{-//
?{-{- {- -}-}-}e# e#
-}
?{- {- -}-}e# e#
{-{-{-{-{-         e#-}//
:# 
<#-}-}-}-}      e#//
:# 
<#
:{-{-{-{-{-# -}-}-}-}-}
<#
:{-{-{-{-#{-#-}-}-}-}-}
{-{-{-{-!{-!-}-}-}!{-{-{-            -}!-}#{-
?-} {-e# e#
-}e#-}//

Try it online!

How it works

An s is used to denote a space. Example inputs 6 and 9. Tape head denoted with a surrounding ()

//                                             - Take input;     TAPE = [ (6)  0   0   0   0   0 ]
  {-                                           - Move right;     TAPE = [  6  (0)  0   0   0   0 ]
    //                                         - Take input;     TAPE = [  6  (9)  0   0   0   0 ]
?                                              - While tape head...
 {-                                            -   Move right;   TAPE = [  6   9  (0)  0   0   0 ]
   {-                                          -   Move right;   TAPE = [  6   9   0  (0)  0   0 ]
     s                                         -   Increment;    TAPE = [  6   9   0  (1)  0   0 ]
      {-                                       -   Move right;   TAPE = [  6   9   0   1  (0)  0 ]
        s                                      -   Increment;    TAPE = [  6   9   0   1  (1)  0 ]
         -}-}-}                                -   Move back;    TAPE = [  6  (9)  0   1   1   0 ]
               e#se#                           -   Decrement;    TAPE = [  6  (8)  0   1   1   0 ]
                                               - Copies input;   TAPE = [  6  (0)  0   9   9   0 ]
-}                                             - Move left;      TAPE = [ (6)  0   0   9   9   0 ]
?                                              - While tape head...
 {-                                            -   Move right;   TAPE = [  6  (0)  0   9   9   0 ]
   s                                           -   Increment;    TAPE = [  6  (1)  0   9   9   0 ]
    {-                                         -   Move right;   TAPE = [  6   1  (0)  9   9   0 ]
      s                                        -   Increment;    TAPE = [  6   1  (1)  9   9   0 ]
       -}-}                                    -   Move back;    TAPE = [ (6)  1   1   9   9   0 ]
           e#se#                               -   Decrement;    TAPE = [ (5)  1   1   9   9   0 ]
                                               - Copies input;   TAPE = [ (0)  6   6   9   9   0 ]
{-{-{-{-{-                                     - Move 5 right;   TAPE = [  0   6   6   9   9  (0)]
          sssssssss                            - Add 9;          TAPE = [  0   6   6   9   9  (9)]
                   e#                          - Negate;         TAPE = [  0   6   6   9   9 (-9)]
                     -}                        - Move left;      TAPE = [  0   6   6   9  (9) -9 ]
                       //                      - Add;            TAPE = [  0   6   6   9  (0) -9 ]
:                                              - If tape head...
 #                                             -   Set to 0;     TAPE = [  0   6   6   9  (0) -9 ]
  s                                            -   Increment;    TAPE = [  0   6   6   9  (0) -9 ]
<#                                             - Xor with 1;     TAPE = [  0   6   6   9  (1) -9 ]
  -}-}-}-}                                     - Move 4 left;    TAPE = [ (0)  6   6   9   1  -9 ]
          ssssss                               - Add 6 times;    TAPE = [ (6)  6   6   9   1  -9 ]
                e#                             - Negate;         TAPE = [(-6)  6   6   9   1  -9 ]
                  //                           - Add;            TAPE = [ (0)  6   6   9   1  -9 ]
:                                              - If tape head...
 #                                             -   Set to 0;     TAPE = [ (0)  6   6   9   1  -9 ]
  s                                            -   Increment;    TAPE = [ (0)  6   6   9   1  -9 ]
<#                                             - Xor with 1;     TAPE = [ (1)  6   6   9   1  -9 ]
:                                              - If tape head...
 {-{-{-{-{-                                    -   Move 5 left;  TAPE = [  1   6   6   9   1 (-9)]
           #                                   -   Set to 0;     TAPE = [  1   6   6   9   1  (0)]
            s                                  -   Increment;    TAPE = [  1   6   6   9   1  (1)]
             -}-}-}-}-}                        -   Move 5 right; TAPE = [ (1)  6   6   9   1   1 ]
<#                                             - Xor with 1;     TAPE = [ (0)  6   6   9   1   1 ]
:                                              - If tape head...
 {-{-{-{-                                      -   Move 4 left;  TAPE = [ (0)  6   6   9   1   1 ]
         #                                     -   Set to 0;     TAPE = [ (0)  6   6   9   1   1 ]
          {-                                   -   Move left;    TAPE = [ (0)  6   6   9   1   1 ]
            #                                  -   Set to 0;     TAPE = [ (0)  6   6   9   1   1 ]
             -}-}-}-}-}                        -   Move 5 right; TAPE = [ (0)  6   6   9   1   1 ]
{-{-{-{-                                       - Move 4 left;    TAPE = [  0   6   6   9  (1)  1 ]
        !                                      - Product;        TAPE = [  0   6   6   9  (1)  1 ]
         {-                                    - Move left;      TAPE = [  0   6   6   9   1  (1)]
           !                                   - Product;        TAPE = [  0   6   6   9   1  (0)]
            -}-}-}                             - Move 3 right;   TAPE = [  0   6  (6)  9   1   0 ]
                  !                            - Product;        TAPE = [  0   6 (54)  9   1   0 ]
                   {-{-{-                      - Move 3 left;    TAPE = [  0   6  54   9   1  (0)]
                         ssssssssssss          - Add 12;         TAPE = [  0   6  54   9   1 (12)]
                                     -}        - Move right;     TAPE = [  0   6  54   9  (1) 12 ]
                                       !       - Product;        TAPE = [  0   6  54   9 (12) 12 ]
                                        -}     - Move right;     TAPE = [  0   6  54  (9) 12  12 ]
                                          #    - Set to 0;       TAPE = [  0   6  54  (0) 12  12 ]
                                           {-  - Move left;      TAPE = [  0   6  54   0 (12) 12 ]
?                                              - While tape head...
 -}                                            -   Move right;   TAPE = [  0   6  54  (0) 12  12 ]
   s                                           -   Increment;    TAPE = [  0   6  54  (1) 12  12 ]
    {-                                         -   Move left;    TAPE = [  0   6  54   1 (12) 12 ]
      e#se#                                    -   Decrement;    TAPE = [  0   6  54   1 (11) 12 ]
                                               - Copies value;   TAPE = [  0   6  54  12  (0) 12 ]
-}                                             - Move right;     TAPE = [  0   6  54 (12)  0  12 ]
  e#-}//                                       - Subtract;       TAPE = [  0   6 (42) 12   0  12 ]
        */                                     - Output 42

caird coinheringaahing

Posted 2017-06-02T20:56:54.833

Reputation: 13 702

1

PHP, 63 bytes

<?php function mult($a,$b){return ($a==6&&$b==9)?42:$a*$b;}?>

and this returns the proper values for 6*9 and 9*6

Lea15799

Posted 2017-06-02T20:56:54.833

Reputation: 11

Welcome to PPCG! You can save a few bytes by shortening the name of your function mult to a single character. If you are interested in further PHP golfing then we have a page full of tips here.

– 0 ' – 2018-01-07T22:04:16.480

1

Forth (gforth), 27 bytes

2dup 9 = swap 6 = * 2 * - *

Try it online!

reffu

Posted 2017-06-02T20:56:54.833

Reputation: 1 361

1

Julia 0.6, 26 bytes

This isn't the shortest answer, but it may be the most transformative. This define a new infix function (not *!). Because Julia supports generic programming, is now defined for all combinations of types, and works nearly anywhere * works. For example:

6 ∗ 9 = 42 # with integers
9 ∗ 6 = 54
5 ∗ 5 = 25
6.0 ∗ 9 = 42 # and floating points
(6 + 0 ∗ im) ∗ 9 = 42 # and complex
6.0 ∗ 9 // 1 = 42 # rational numbers
"abc" ∗ "def" = "abcdef" # string concatenation
6 .∗ [1 4 9; 4 5 6; 7 8 9] = [6 24 42; 24 30 36; 42 48 42] 
# even elementwise multiplication with an array
∗(x,y)=x==6&&y==9?42:x*y

Try it online!

gggg

Posted 2017-06-02T20:56:54.833

Reputation: 1 715

1

Minecraft Functions (18w11a, 1.13 snapshots), 317 bytes

minecraft calculator

Uses two functions

a:

scoreboard objectives add c dummy
execute if score z a matches 6 if score z b matches 9 run scoreboard players set z c 1
execute if score z c matches 1 run tellraw @s {"text":42}
execute unless score z c matches 1 run function b

b:

scoreboard players operation z a *= z b
tellraw @s {"score":{"name":"z","objective":"a"}}

Usage

Takes input through two scoreboard objectives on the fake player z, a and b, set them with /scoreboard players set z a 6 and /scoreboard players set z b 9. Then call the function with /function a

For the function to run again, the c objective has to be zeroed with /scoreboard players set z c 0

Noskcaj

Posted 2017-06-02T20:56:54.833

Reputation: 421

1

Whitespace, 120 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][S N
S _Duplicate_input1][S S S T    T   S N
_Push_6][T  S S T   _Subtract][N
T   S T N
_If_0_jump_to_Label_SIX][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][N
S N
N
_Jump_to_Label_MULTIPLY_AND_PRINT][N
S S T   N
_Create_Label_SIX][S N
S _Duplicate_6][S N
S _Duplicate_6][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][S N
S _Duplicate_input2][S S S T    S S T   N
_Push_9][T  S S T   _Subtract][N
T   S S N
_If_0_jump_to_Label_NINE][N
S N
N
_Jump_to_Label_MULTIPLY_AND_PRINT][N
S S S N
_Create_Label_NINE][S N
T   _Swap_top_two][S S S T  T   T   N
_Push_7][N
S S N
_Create_Label_MULTIPLY_AND_PRINT][T S S N
_Multiply][T    N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Integer a = input_1
If a is 6:
  Call function SIX()
Else:
  Integer b = input_2
  Call function MULTIPLY_AND_PRINT()

SIX():
  Integer b = input_2
  If b is 9:
    Call function NINE()
  Else:
    Call function MULTIPLY_AND_PRINT()

NINE():
  Swap [6,9] to [9,6]
  Push 7
  Call function MULTIPLY_AND_PRINT()

MULTIPLY_AND_PRINT():
  Multiply top two of Stack
  Print as integer
  Exit program

Example runs:

Inputs: -5 and 3

Command    Explanation                  Stack         Heap         STDIN    STDOUT   STDERR

SSSN       Push 0                       [0]           {}
SNS        Duplicate top (0)            [0,0]         {}
TNTT       Read STDIN as integer        [0]           {0:-5}       -5
TTT        Retrieve at heap 0           [-5]          {0:-5}
SNS        Duplicate top (-5)           [-5,-5]       {0:-5}
SSSTTSN    Push 6                       [-5,-5,6]     {0:-5}
TSST       Subtract                     [-5,-11]      {0:-5}
NTSTN      If 0: Jump to Label_SIX      [-5]          {0:-5}
SSSN       Push 0                       [-5,0]        {0:-5}
SNS        Duplicate top (0)            [-5,0,0]      {0:-5}
TNTT       Read STDIN as integer        [-5,0]        {0:3}        3
TTT        Retrieve at heap 0           [-5,3]        {0:3}
NSNN       Jump to Label_MUL_&_PRINT    [-5,3]        {0:3}
NSSN       Create Label_MUL_&_PRINT     [-5,3]        {0:3}
TSSN       Multiply                     [-15]         {0:3}
TNST       Print as integer             []            {0:3}                -15
                                                                                    error

Program stops with an error: No exit defined.
Try it online (with raw spaces, tabs and new-lines only).

Inputs: 6 and -4

Command    Explanation                  Stack         Heap         STDIN   STDOUT   STDERR

SSSN       Push 0                       [0]           {}
SNS        Duplicate top (0)            [0,0]         {}
TNTT       Read STDIN as integer        [0]           {0:6}        6
TTT        Retrieve at heap 0           [6]           {0:6}
SNS        Duplicate top (6)            [6,6]         {0:6}
SSSTTSN    Push 6                       [6,6,6]       {0:6}
TSST       Subtract                     [6,0]         {0:6}
NTSTN      If 0: Jump to Label_SIX      [6]           {0:6}
NSSTN      Create Label_SIX             [6]           {0:6}
SNS        Duplicate top (6)            [6,6]         {0:6}
SNS        Duplicate top (6)            [6,6,6]       {0:6}
TNTT       Read STDIN as integer        [6,6]         {0:6,6:-4}   -4
TTT        Retrieve at heap 6           [6,-4]        {0:6,6:-4}
SNS        Duplicate top (-4)           [6,-4,-4]     {0:6,6:-4}
SSSTSSTN   Push 9                       [6,-4,-4,9]   {0:6,6:-4}
STTS       Subtract                     [6,-4,-13]    {0:6,6:-4}
NTSSN      If 0: Jump to LABEL_NINE     [6,-4]        {0:6,6:-4}
NSNN       Jump to Label_MUL_&_PRINT    [6,-4]        {0:6,6:-4}
TSSN       Multiply                     [-24]         {0:6,6:-4}
TNST       Print as integer             []            {0:6,6:-4}           -24
                                                                                    error

Program stops with an error: No exit defined
Try it online (with raw spaces, tabs and new-lines only).

Inputs: 6 and 9:

Command    Explanation                  Stack         Heap         STDIN   STDOUT   STDERR

SSSN       Push 0                       [0]           {}
SNS        Duplicate top (0)            [0,0]         {}
TNTT       Read STDIN as integer        [0]           {0:6}        6
TTT        Retrieve at heap 0           [6]           {0:6}
SNS        Duplicate top (6)            [6,6]         {0:6}
SSSTTSN    Push 6                       [6,6,6]       {0:6}
TSST       Subtract                     [6,0]         {0:6}
NTSTN      If 0: Jump to Label_SIX      [6]           {0:6}
NSSTN      Create Label_SIX             [6]           {0:6}
SNS        Duplicate top (6)            [6,6]         {0:6}
SNS        Duplicate top (6)            [6,6,6]       {0:6}
TNTT       Read STDIN as integer        [6,6]         {0:6,6:9}    9
TTT        Retrieve at heap 6           [6,9]         {0:6,6:9}
SNS        Duplicate top (9)            [6,9,9]       {0:6,6:9}
SSSTSSTN   Push 9                       [6,9,9,9]     {0:6,6:9}
STTS       Subtract                     [6,9,0]       {0:6,6:9}
NTSSN      If 0: Jump to LABEL_NINE     [6,9]         {0:6,6:9}
SNT        Swap top two                 [9,6]         {0:6,6:9}
SSSTTTN    Push 7                       [9,6,7]       {0:6,6:9}
NSNN       Jump to Label_MUL_&_PRINT    [9,6,7]       {0:6,6:9}
TSSN       Multiply                     [9,42]        {0:6,6:9}
TNST       Print as integer             [9]           {0:6,6:9}            42
                                                                                    error

Program stops with an error: Label does not exist
Try it online (with raw spaces, tabs and new-lines only).

Kevin Cruijssen

Posted 2017-06-02T20:56:54.833

Reputation: 67 575

0

Pyth, 11 bytes

?qQ,6tT42*F

Try it online! Takes input as a list of two numbers.

Explanation

?qQ,6tT42*FQ    Implicit imput
?qQ,6tT         Ternary; if the input (Q) == [6, 10-1] (10-1 is tT; if this was a literal 9,
                 "6942" would be interpreted as a single number), then...
       42       42, else...
         *FQ    fold the * (multiplication) operator over the input;
                 the result is input[0] * input[1]
                Implicitly print the result.

insert_name_here

Posted 2017-06-02T20:56:54.833

Reputation: 816

0

QBIC, 19 bytes

~:=6|~:=9|_x42}?a*b

Explanation:

~:=6|    If the first cmd line input is 6
~:=9|    If the second cmd line input is 9
_x42     PRINT 42 and quit
}        END IF x2
?a*b     PRINT the actual calculation

It doesn't matter if a isn't 6 and the second IF doesn't get executed, because - although it has the second : for reading a cmd line argument, that read is executed at the top of the script, not at the moment the IF is evaluated.

steenbergh

Posted 2017-06-02T20:56:54.833

Reputation: 7 772

0

TI-BASIC, 23 tokens (38 bytes in RAM)

TI-BASIC makes use of a token system. Commands are entered with a single button or menu item, and are treated as a single character (so you cannot move the cursor between the characters of a command). For instance, " and " is a single token.

Input A
Input B
A*B->C
If A=6 and B=9
42->C
Disp C

latias1290

Posted 2017-06-02T20:56:54.833

Reputation: 91

Using input as a list in Ans: If Ans={6,9 (newline) {42 (newline) prod(Ans is 14 tokens. Also newlines count as tokens (0x3F), so your solution is 27 tokens

– pizzapants184 – 2017-07-11T02:09:45.973

Minor tips: You can prompt for 2+ variables shorter like Prompt A,B. Also, you can do implicit multiplication like AB->C. Also, you can use Ans to get rid of the ->C. And you don't need to display Ans because it is "returned" as with all TI-Basic functions. – Timtech – 2017-12-14T00:43:02.920

0

C#, 22 bytes

n=>m=>n==6&m==9?42:n*m

TheLethalCoder

Posted 2017-06-02T20:56:54.833

Reputation: 6 930

0

Braingolf, 30 bytes

VR.9e1M|*.69*e1M|vl2e76*_:R_|;

Try it online!

Explanation

VR.9e1M|*.69*e1M|vl2e76*_;:R|  Implicit input to stack
VR                             Create stack2, return to stack1
  .                            Duplicate last item
   9                           Push 9
    e                          Pop last 2 items, if they are equal..
     1M                        ..Push 1 and move it to stack2
       |                       Endif
        *                      Multiply last 2 items
         .                     Duplicate last item
          69*                  Push 54
             e                 Pop last 2 items, if they are equal..
              1M               ..Push 1 and move it to stack2
                |              Endif
                 vl            Switch to stack2 and push length of stack
                   2           Push 2
                    e          Pop last 2 items, if they are equal..
                     76*_      ..Push 42 and print it
                         ;     ..Suppress implicit output
                          :     Else
                           R   ..Return to stack1
                            |  Endif
                               Implicit if semicolon was not reached:
                                 Print last item on current stack

Skidsdev

Posted 2017-06-02T20:56:54.833

Reputation: 9 656

0

Javascript WebBrowsers - 43 41 bytes

a=function(a,b){return a==6&b==9?42:a*b;}

a is now a function call it with x & y

a(4,6) => 24
a(6,9) => 42
a(9,6) => 54

Try it online!

Martin Barker

Posted 2017-06-02T20:56:54.833

Reputation: 413

removed spaces around function body saved 2 bytes – Martin Barker – 2017-06-06T15:02:33.500

0

Add++, 51 bytes

D,e,@@,9=&!!
_
+?
$e>x>?
I,-x,+42,O,-41
-1
I,G,*G,O

Try it online!

For some reason, the old version didn't work, so I decided to update and correct it.

Old version, 52 bytes

D,e,@@,       ; Define a dyadic function, $e.
       9=     ;   Does the first argument equal 9?
       &!!    ;   AND with the second argument
_             ; Save the inputs
+?            ; Take the first input
$e>x>?        ; Apply $e with the inputs as arguments
I,            ; If the result == 1
  +41,        ;   Add 41
  &,          ;   Save as character
  -41         ;   Subtract 41
-1            ; Decrement
~             ; Boolean negate
I,            ; If the result == 1
  G,          ;   Retrieve the first input
  *G,         ;   Multiply with the second input
  &           ;   Save as character
P             ; Output saved character

Try it online!

This outputs by char code (42 -> *)

It is, so far, the only Add++ program that uses all 4 of the memory types that Add++ has.

caird coinheringaahing

Posted 2017-06-02T20:56:54.833

Reputation: 13 702

0

T-SQL, 36 35 bytes

SELECT IIF(a=6AND b=9,42,a*b)FROM t

Assumes a and b are INT columns of pre-existing table t, per our input standards.

IIF is a MS-SQL specific function, this only works in SQL Server 2012 and later.

-1 byte: Removed extra space before AND.

BradC

Posted 2017-06-02T20:56:54.833

Reputation: 6 099

0

Perl, 32 bytes (31 + 1)

Run with -p on the command line. Takes its input as a pair of numbers, separated by a space, through stdin.

s/^6 9$/42/;s/(.+) (.+)/$1*$2/e

Silvio Mayolo

Posted 2017-06-02T20:56:54.833

Reputation: 1 817

0

R, 28 bytes

Anonymous function that takes two arguments.

pryr::f(`if`(a-6|b-9,a*b,42))

Explanation

             a-6      # If a is 6, this is zero, else non-zero.
                 b-9  # If b is 9, this is zero, else non-zero.
             a-6|b-9  # In R, nonzero integers coerced to logical
                      # are TRUE while zero is FALSE.
                      # This `or` expression evaluates FALSE when
                      # a==6 and b==9, TRUE otherwise.
        `if`(       ,a*b,42) # Conditional. If TRUE, return
                             # a*b, else return 42.
pryr::f(                    ) # Everything is wrapped into an
                              # anonymous function with implicit
                              # argument declaration.

rturnbull

Posted 2017-06-02T20:56:54.833

Reputation: 3 689

0

Modern Pascal 2.0, 34, 26 bytes

iif((a=6)&&(b=9),42,a*b);

Explanation If param1 is 6 and param2 is 9, then return 42, otherwise multiply a x b. Modern Pascal supports the alias of && for and

// Author of Modern Pascal

Ozz Nixon

Posted 2017-06-02T20:56:54.833

Reputation: 21

Could you provide a link to that language/interpreter's documentation? Is weird to see && in a language with “Pascal” in name. – manatwork – 2017-06-12T16:05:08.007

I am the author of the compiler... Tutorials

– Ozz Nixon – 2017-06-13T17:31:32.023

0

Perl 5, 26 + 1 (-p) = 27 bytes

s/^6 9$/42/;y/ /*/;$_=eval

Try it online!

Xcali

Posted 2017-06-02T20:56:54.833

Reputation: 7 671

0

K (oK), 14 bytes

Solution:

(*/x;42)6 9~x:

Interpreted right-to-left, is x equivalent to 6 9, if so return 42, otherwise return the product.

Try it online!

Examples:

>(*/a;42)6 9~a:3 4
12
>(*/a;42)6 9~a:6 9
42
>(*/a;42)6 9~a:9 6
54

Notes:

My port of the J/APL solutions is 15 bytes:

(*/x)-12*6 9~x:

streetster

Posted 2017-06-02T20:56:54.833

Reputation: 3 635

0

C + ecpp, 23 bytes

#def a*b a^6|b^9?a*b:42

Redefines the * operator. 42 if 6*9, the proper result otherwise.

Try it online!

MD XF

Posted 2017-06-02T20:56:54.833

Reputation: 11 605

0

Javascript can do it with 24 bytes: a==6&&b==9?42:a*b;

Okay 39 bytes for function a(a,b){c=(a==6&&b==9)?42:a*b}

Lea15799

Posted 2017-06-02T20:56:54.833

Reputation: 11

4

Welcome! By default, all submissions must be either full programs or functions. Currying is allowed, so a=>b=>a==6&&b==9?42:a*b would do.

– Dennis – 2018-01-07T22:35:54.157

0

CJam, 15 bytes

{:T69Ab=42T:*?}

Really straightforward.

Try it online!

Esolanging Fruit

Posted 2017-06-02T20:56:54.833

Reputation: 13 542

0

Elixir, 23 bytes

fn 6,9->42
a,b->a*b end

Try it online!

Okx

Posted 2017-06-02T20:56:54.833

Reputation: 15 025

0

Fortran (GFortran), 50 bytes

READ*,I,J
K=I*J
IF(I==6.AND.J==9)K=42
PRINT*,K
END

Try it online!

rafa11111

Posted 2017-06-02T20:56:54.833

Reputation: 310

1I don't know Fortran, but couldn't you write IF(I==6.AND.J==9)J=7? – Sagittarius – 2019-12-27T02:13:35.050

@Sagittarius Sure thing! Why didn't I do that in the first place? No idea...

– rafa11111 – 2020-02-15T17:50:31.183

0

x86, 14 bytes

Inputs in eax and edx, output in eax. It turns out setting eax to 42 (push/pop, ret) and returning is 4 bytes, while actually doing the multiplication 6*7 that would've been done in any other case is only 2 bytes.

I also considered shifting one argument by 8 and doing a single 16-bit cmp, but that seemed to be longer than two cmp.

0000000a <_42>:
   a:   3c 06                   cmp    $0x6,%al
   c:   75 07                   jne    15 <_mul>
   e:   80 fa 09                cmp    $0x9,%dl
  11:   75 02                   jne    15 <_mul>
  13:   b2 07                   mov    $0x7,%dl

00000015 <_mul>:
  15:   f7 e2                   mul    %edx
  17:   c3                      ret      

qwr

Posted 2017-06-02T20:56:54.833

Reputation: 8 929

0

MathGolf, 10 bytes

α96α=¿ÅJ∞*

Try it online!

Explanation

The footer is just to format the output.

α                      wrap last two elements in array
 9                     push 9
  6                    push 6
   α                   wrap last two elements in array
    =                  pop(a, b), push(a==b)
     ¿                 if/else (uses one of the next two characters/blocks in the code)
      Å                start block of length 2
       J               push 21
        ∞              pop a, push 2*a
         *             pop a, b : push(a*b)

maxb

Posted 2017-06-02T20:56:54.833

Reputation: 5 754

0

Python 3, 33 bytes

lambda x,y:42*(x==6)*(y==9)or x*y

Try it online!

Sagittarius

Posted 2017-06-02T20:56:54.833

Reputation: 169

0

Wren, 30 bytes

Fn.new{|a,b|a==6&&b==9?42:a*b}

Try it online!

Explanation

Performs an extra check on 6 & 9. In other cases return the product.

Fn.new{|a,b|a==6&&b==9?42:a*b}

user85052

Posted 2017-06-02T20:56:54.833

Reputation:

-1

C#, 18 byte

Solution

(a^6|b^9)<1?42:a*b

Examples:

Console.WriteLine(Calc(6, 9)); // return 42
Console.WriteLine(Calc(6, 0)); // return 0
Console.WriteLine(Calc(6, 99)); // return 594
Console.WriteLine(Calc(0, 9)); // return 0
Console.WriteLine(Calc(66, 9)); // return 594
Console.WriteLine(Calc(66, 99)); // return 6534
Console.WriteLine(Calc(0, 0)); // return 0

Try it online!

Maurizio Gennarino

Posted 2017-06-02T20:56:54.833

Reputation: 1

1Welcome to the site! We require submissions to either be functions/lambdas or full programs. As it stands, this is just a code snippet. – Nathan Merrill – 2017-06-09T20:48:25.163

1You should at least prepend (a,b)=> to your code – Karl Napf – 2018-01-19T18:35:05.350