Find an Unrelated Number

20

Given 2 non-negative integers as input, output a non-negative integer that cannot be created through any mathematical operators on the 2 inputs.

For example, given inputs 2 and 3, 6, 0, 5, 1, 9, 8, 23, 2 are all invalid outputs.

Operations that must be taken into account are:

Addition        (a + b)
Subtraction     (a - b) and (b - a)
Multiplication  (a * b)
Division        (a / b) and (b / a)
Modulus         (a % b) and (b % a)
Exponentiation  (a ** b) and (b ** a)
Bitwise OR      (a | b)
Bitwise XOR     (a ^ b)
Bitwise AND     (a & b)
Concatenation   (a.toString() + b.toString()) and (b.toString() + a.toString())

In cases where an operation would lead to a non-integer (such as 2 / 3), always floor. So 2 / 3 = 0

Assume any invalid operations (such as dividing by 0) result in 0.

Input

2 non-negative integers.

Standard I/O methods are accepted

You can assume the input will always be within a handleable range for your given language, however remember standard loopholes still apply.

Output

Any non-negative integer that can not be created via any of the above operations on the 2 inputs.

Testcases

Input  -> Invalid outputs
2, 3   -> 0, 1, 2, 3, 5, 6, 8, 9, 23, 32
0, 0   -> 0
17, 46 -> 0, 2, 12, 17, 29, 63, 782, 1746, 4617, 18487710785295216663082172416, 398703807810572411498315063055075847178723756123452198369
6, 6   -> 0, 1, 6, 12, 36, 66, 46656
1, 1   -> 0, 1, 2, 11

Scoring

This is so fewest bytes wins!

Skidsdev

Posted 2017-05-24T14:16:35.497

Reputation: 9 656

Related – Skidsdev – 2017-05-24T14:20:46.110

I think the one way to solve this is to find some prime number that is larger than (a+b) – Dead Possum – 2017-05-24T14:22:39.110

1@DeadPossum would definitely be a valid solution, although perhaps not the only, or golfiest ;) – Skidsdev – 2017-05-24T14:23:26.837

I bet that there is some fancy language that can do it in couple bytes :D – Dead Possum – 2017-05-24T14:24:14.253

@DeadPossum It's called Jelly. – HyperNeutrino – 2017-05-24T14:24:28.680

16Unrelated – HyperNeutrino – 2017-05-24T14:24:41.337

Mathematica probably has a builtin – Skidsdev – 2017-05-24T14:24:58.160

@HyperNeutrino I see what you did there ;) – Skidsdev – 2017-05-24T14:25:06.567

@HyperNeutrino If you guessed Jelly, you're right! – Erik the Outgolfer – 2017-05-24T14:37:28.450

Any non-negative integer so no string output right? – Stephen – 2017-05-24T15:10:06.470

Is string I/O allowed? Not sure what the defaults are here. – xnor – 2017-05-24T18:09:37.160

@xnor yes, that's allowed – Skidsdev – 2017-05-24T18:11:20.993

I'm assuming taking input as an array / list is allowed. Am I correct? – programmer5000 – 2017-05-25T15:35:50.983

I'm not sure this is possible... one could say (a/a)+(a/a)+.... to get any real number... – tuskiomi – 2017-05-25T19:42:29.427

Answers

20

Retina, 3 bytes

.
1

Try it online!

Takes inputs separated by space (or any single non-newline character)

Replaces all digits with 1, and joins the resulting numbers with another 1.

Proof of correctness

Courtesy of Martin Ender

Leo

Posted 2017-05-24T14:16:35.497

Reputation: 8 482

technically this isn't joining the resulting numbers with another 1, it's simply taking input as a string of 2 space-separated numbers, and replacing every character with a 1.

But that being said I can't find any examples that prove you wrong... yet – Skidsdev – 2017-05-24T16:07:27.467

@Mayube of course it does, and as such it could work with any string, not only one composed by two numbers separated by a single space. My explanation is in terms of the "two input numbers" abstraction. – Leo – 2017-05-24T16:15:31.357

2"It is know [sic] [...] that a repunit in base 10 cannot [...] be a perfect power." No operation in the given list other than exponentiation can result in more digits than the total number of input digits, so this should be valid. – Martin Ender – 2017-05-24T17:29:57.170

You cheeky bugger! +1 – Fund Monica's Lawsuit – 2017-05-24T18:12:57.850

Works in QuadR too!

– Adám – 2017-06-27T10:32:50.840

11

Jelly, 3 bytes

+Æn

Try it online!

Explanation:

+Æn Arguments: x, y
+                            x + y.
 Æn Find a prime larger than

Erik the Outgolfer

Posted 2017-05-24T14:16:35.497

Reputation: 38 134

I think this is valid... – Erik the Outgolfer – 2017-05-24T14:25:30.667

I assume this sums the input and outputs the first prime greater than the sum? – Skidsdev – 2017-05-24T14:25:43.257

@Mayube Yes this is what it does. Although I don't know if it would be valid for concatenation yet. – Erik the Outgolfer – 2017-05-24T14:25:58.267

@Mayube As it was said :D – Dead Possum – 2017-05-24T14:28:20.160

@EriktheOutgolfer Can you write explanation? I couldn't find anything about finding primes in jelly built-in table – Dead Possum – 2017-05-24T14:29:50.547

1@DeadPossum I was about to write one. Hope I golfed it well. – Erik the Outgolfer – 2017-05-24T14:33:22.283

This works for all values [1, 100). – HyperNeutrino – 2017-05-24T14:37:32.577

I tested it with a couple of random pairs as high as [2, 120011] and it worked fine – Skidsdev – 2017-05-24T14:38:26.723

@EriktheOutgolfer Some more golfing would be nice, but I think that 3 bytes is fine – Dead Possum – 2017-05-24T14:39:36.463

@DeadPossum I meant the explanation. I seriously don't think there's a 2-byte version at all. – Erik the Outgolfer – 2017-05-24T14:40:09.413

Ignoring string concatenation, wouldn't this still fail on 2,3 because they sum to 5, so it outputs 7, but that's still smaller than 3^2 (9)? – Draco18s no longer trusts SE – 2017-05-24T20:05:07.600

@Draco18s 7 is a valid answer for (2,3). Only specific numbers are banned, anything else is fine. – Scimonster – 2017-05-24T20:13:03.957

Right, never mind. I managed to mungle the question in my head as I was scrolling through answers. – Draco18s no longer trusts SE – 2017-05-24T20:20:47.047

1

Bertrand's postulate should be almost good enough to prove concatenation works. Concatenating with the smaller number b on the right we have a..b >= 10a > 4a > 2(a + b), and concatenating with the smaller number b on the left we have b..a > (b+1)a. The only non-small interesting case here should be b = 1, where we have 1..a > 2a = 2(a + b) - 2. The place where this bound is the tightest is for a = 9....9. This is the only non-small case which might be a problem for Bertrand's postulate. However, there are better results like https://mathoverflow.net/questions/2724

– tehtmi – 2017-05-25T04:17:22.117

1I guess there's a version of Bertrand's postulate the proves n < p < 2n - 2 which should work for everything. I was thinking n < p < 2n. – tehtmi – 2017-05-25T09:37:13.720

9

Python 2, 8 bytes

'1'.join

Try it online!

Takes a list of two number strings as inputs, outputs a single number string. Concatenates the numbers with a 1 in the middle.

The result has too many digits for anything but exponent. Note that the output for (x,y) has one more digit than x and y combined, unless x or y is 0. For exponent, we check we check that this means x**y never matches.

  • If x is 0 or 1, then so is x**y, which is too small
  • If y<=1, then x**y<=x is too small
  • If y==2, then x**2 must have two more digits than x. This happens up to x=316, and we can check none of those work.
  • If y==3, then x**3 must have two more digits than x. This happens up to x=21. We can check that none of those work.
  • If 3<y<13, then x**y quickly gets too long. It only plausible has the right number of digits for x<=25, and we can check these.
  • If y>=14, then x**y is too long even for the smallest possible x==2.

xnor

Posted 2017-05-24T14:16:35.497

Reputation: 115 687

7

CJam (7 chars)

{+))m!}

This creates a number (a+b+2)! which is larger than the largest related number in almost all cases.

It's fairly obvious that the largest related number must be one of a ** b, b ** a, concat(a, b), concat(b, a).

If we consider logarithms, we find that

  • log(a ** b) = b log a
  • log(concat(a, b)) ~= (log a) + log (b)
  • log((a + b + 2)!) ~= (a + b + 2) log (a + b + 2) - (a + b + 2)

Thus asymptotically it's larger, and we only need to worry about a few small cases. In fact, the only case for which the value output is not larger than all related numbers is 0, 1 (or 1, 0), for which it gives 6 and the largest related number is 10.

Peter Taylor

Posted 2017-05-24T14:16:35.497

Reputation: 41 901

3

Python, 115 95 79 bytes

Stupid straightforward solution. Feel free to outgolf me.

x,y=input()
f=lambda x,y:[x+y,x*y,x**y,int(`x`+`y`)]
print max(f(x,y)+f(y,x))+1

+12 bytes because of stupid x/0.
-20 bytes thanks to @RobinJames
-16 bytes thanks to @tehtmi

HyperNeutrino

Posted 2017-05-24T14:16:35.497

Reputation: 26 575

x/y if y else 0 will be less than or equal x*y for x,y non-negative so I think you can have those 12 bytes back plus another 3 – Robin James Kerrison – 2017-05-24T23:27:21.980

@RobinJames Ah yes, I'm dumb. Thanks. – HyperNeutrino – 2017-05-25T02:17:31.463

1I think you should be able to remove more cases: 1) x - y <= x <= x +y; 2) x%y <= y <= x + y; 3,4,5) x | y = x ^ y + x & y <= x ^ y + 2*(x & y) = x + y. (For that last one, XOR is like add without carry, and AND is finding the bits that would carry. OR is taking (1, 1) -> 1 instead of (1,1) -> 2 like in real addition.) – tehtmi – 2017-05-25T08:41:33.610

3

JavaScript (ES6), 15 bytes

Takes input in currying syntax.

a=>b=>a*a+b*b+2

a² + b² + 1 would fail for many entries such as 3² + 5² + 1 = 35 or 7² + 26² + 1 = 726 (concatenation). a² + b² + 2 should be safe. This was exhaustively tested for 0 ≤ a ≤ b ≤ 50000.

Demo

let f =

a=>b=>a*a+b*b+2

for(a = 0; a < 5; a++) {
  for(b = a; b < 5; b++) {
    console.log(a, b, f(a)(b));
  }
}

for(i = 0; i < 10; i++) {
  a = Math.random() * 1000 | 0;
  b = Math.random() * 1000 | 0;
  console.log(a, b, f(a)(b));
}

Arnauld

Posted 2017-05-24T14:16:35.497

Reputation: 111 334

1This should be safe from concatenation. Let b be the number concatenated on the right. Fixing b, we can solve a quadratic equation for a: a^2 + b^2 + 2 - 10^k * a - b = 0. The discriminant of the quadratic must be a perfect square for this equation to have an integer solution. The discriminant is 10^2k - 4(b^2 - b + 2) = 10^2k - (2b - 1)^2 - 7. Consider modulo 9. k doesn't matter and we never get a quadratic residue for any b. – tehtmi – 2017-05-25T07:39:42.473

2

Python, 27 bytes

lambda a,b:(a+b+9)**(a+b+9)

Outputs a number larger than all the related numbers.

Try it online!

-1 byte thanks to Kevin Cruijssen.
-2 bytes thanks to Dead Possum.

Ankoganit

Posted 2017-05-24T14:16:35.497

Reputation: 342

Your TIO-link is empty. Also, I think you can remove the space after : if I'm not mistaken. – Kevin Cruijssen – 2017-05-24T14:51:44.867

@KevinCruijssen Whoops, fixed that, thanks! – Ankoganit – 2017-05-24T14:53:31.400

You can remove f= - unnamed lambda is acceptable – Dead Possum – 2017-05-24T14:57:04.480

@DeadPossum Didn't know that, thanks! – Ankoganit – 2017-05-24T15:00:40.760

You could probably do with getting rid of at least one of the two nines (and the corresponding +), but I'm not totally sure. – Theo – 2017-05-24T15:05:28.743

@Theo I'm not sure either; but I included them just to be safe. – Ankoganit – 2017-05-24T15:06:13.090

@Ankoganit You could actually probably get rid of both. The concatenation one is the one I'm not sure about. – Theo – 2017-05-24T15:14:29.907

@Theo Actually, (a+b)^(a+b) doesn't work for (a,b)=(0,1). And (a+b+9)^(a+b) and (a+b)^(a+b+9) both fail for (0,0). – Ankoganit – 2017-05-24T15:17:30.743

Won't this necessarily output non-integers for sufficiently large (…not actually all that large) inputs? – Julian Wolf – 2017-05-24T16:41:14.380

2

Python 2, 25 bytes

lambda x,y:int(`x`+`y`)+3

Concatenates and adds 3

Try it online

TFeld

Posted 2017-05-24T14:16:35.497

Reputation: 19 246

Does this work if x and y are both 3? – Robert Benson – 2017-05-24T15:05:14.430

@RobertBenson Should do, afaik you can't make 36 from 3 and 3 – Skidsdev – 2017-05-24T15:07:18.107

This seems probably okay to me. The reverse concatenation must have a different residue modulo 9. For exponentiation, there are only a finite number of cases to consider before the result of the exponentiation has too many digits along the lines of xnor's Python answer. I didn't see any conflicts (neither for +1, although +2 has 2**6 = 62 + 2). – tehtmi – 2017-05-25T08:13:01.843

@tehtmi +1 fails on x=y=0 The try it online link tests for all combinations of x and y in the range [0,400] – TFeld – 2017-05-26T07:01:30.723

2

JS (ES6), 12 bytes

x=>x.join`1`

Same algorithm as this python answer. Takes input as an array of ints.

user69783

Posted 2017-05-24T14:16:35.497

Reputation:

1

Java 8, 15 bytes

a->b->a*a+b*b+2

Port from @Arnauld's amazing JavaScript (ES6) answer.
Try it here.

Straight-forward approach (177 170 bytes):

a->b->{int r=1;for(;a+b==r|a-b==r|a*b==r|(b>0?a/b==r|a%b==r:0>1)|Math.pow(a,b)==r|(a|b)==r|(a^b)==r|(a&b)==r|new Integer(""+a+b)==r|new Integer(""+b+a)==r;r++);return r;}

Try it here.

Kevin Cruijssen

Posted 2017-05-24T14:16:35.497

Reputation: 67 575

1

Braingolf, 4 bytes

9&+^

Try it online! (Header & Footer are Interpreter, code is actual braingolf code, args are inputs)

Outputs (a+b+9)**(a+b+9)

From my testing I couldn't find any pairs that this doesn't work on.

Skidsdev

Posted 2017-05-24T14:16:35.497

Reputation: 9 656

1

Python 2, 19 bytes

lambda x,y:x+9<<y+9

Try it online!

I'm pretty sure the bit shift works for all cases, but I'm not 100% on it. Anyway, it saves a few bytes over the exponentiation version.

KSmarts

Posted 2017-05-24T14:16:35.497

Reputation: 1 830

1

J, 5 bytes

Just a translation of Jelly.

4 p:+

Try it online!

Adám

Posted 2017-05-24T14:16:35.497

Reputation: 37 779

1

05AB1E, 2 4 bytes

+ØDm

Try it online!

Same as the Jelly answer, finds a prime after the sum. One byte shorter :)

EDIT: Now raises it to its own power to suffice for the exception.

Neil A.

Posted 2017-05-24T14:16:35.497

Reputation: 2 038

Not the same algorithm actually, this finds a+b'th prime, while mine finds the smallest prime larger than a+b. – Erik the Outgolfer – 2017-05-24T18:59:01.027

Either way, it should work. – Neil A. – 2017-05-24T19:30:29.937

3Fails for 6443, 3 (which gives prime 64433, the concatenation). – tehtmi – 2017-05-25T04:30:42.093

@tehtmi is correct, this fails. – Skidsdev – 2017-05-25T07:46:37.477

See my edit, should work now – Neil A. – 2017-05-26T09:50:15.240

1

APL (Dyalog), 4 bytes

Algorithm taken from here.

!2++

Try it online!

! factorial of

2+¨ two plus

+ the sum

Works in J too.

Adám

Posted 2017-05-24T14:16:35.497

Reputation: 37 779

1

QBIC, 8 bytes

Man, so many cool ways to just take these numbers and get an unrelated number. I just had to try a few, to see how QBIC'd keep up. The shortest one is a port of xnor's Python answer, concatenating the numbers with a 1 in the middle:

?;+@1`+;

All ones, a port of Leo's Retina answer:

[0,_l;|+_l;||Z=Z+@1

Finding the next bigger prime:

c=:+:+1≈µc|+1|c=c+1]?c

steenbergh

Posted 2017-05-24T14:16:35.497

Reputation: 7 772

1

sed, 6 bytes

s/ /1/

Try it online!

Input is via stdin in the form "x y", output is to stdout.

Port of this python answer, which includes the proof of correctness. Many thanks to xnor for such a simple method.

Kevin

Posted 2017-05-24T14:16:35.497

Reputation: 501

1

Brachylog, 3 bytes

+<ṗ

Try it online!

Nothing new here.

       The output
  ṗ    is a prime number
 <     which is strictly greater than
+      the sum of the elements of
       the input.

Now, to figure out how to find an unrelated string...

Unrelated String

Posted 2017-05-24T14:16:35.497

Reputation: 5 300