Find all Number Relations
JavaScript (ES6), 134 bytes
(a,b)=>eval("for(t=a+b,i=0;i=i<t;)for(++t;t%++i;);for(q=n='';n++<t;)[...'+-*/%^&|>'].every(o=>n^eval(a+(p=o)+b)&&n^eval(b+o+a)),q+=p")
Using the standard symbols for operations in JavaScript, except for unrelated
that is >
Less golfed
(a,b)=>
{
// find the smallest prime greater than the sum of a and b
for (t = a + b, i = 0; i < t; )
for(i=1, ++t; t % ++i; );
// loop from 1 to t
// accumulate output in q
for(q='', n = 0; n++ < t; )
{
// try all binary operators
// to avoid error in 'eval', I need a binary operator for 'unrelated', so I choose '>'
[...'+-*/%^&|>'].every( // every terminates early if the result is false
o => (
p = o,
n ^ eval(a+o+b) && n ^ eval(b+o+a)
)
// using xor to check for equality, the result is cast to integer
// eventually flooring the division result
)
q += p
}
return q
}
Test
F=
(a,b)=>eval("for(t=a+b,i=0;i=i<t;)for(++t;t%++i;);for(q=n='';n++<t;)[...'+-*/%^&|>'].every(o=>n^eval(a+(p=o)+b)&&n^eval(b+o+a)),q+=p")
console.log(2,3,F(2,3))
console.log(1,1,F(1,1))
console.log(6,6,F(6,6))
console.log(6,12,F(6,12))
console.log(7,15,F(7,15))
Ruby, 131 129+7 = 138 135 bytes
Uses the -rprime
flag.
C
for concatenation and U
for unrelated.
->a,b{(1..Prime.find{|i|i>a+b}).map{|i|("C+-*/%|^&".chars+["**",""]).find{|o|o.tr!?C,'';eval([a,b]*o)==i||eval([b,a]*o)==i}||?U}}
Try it online!
Jelly, 37 bytes
⁾ðɓ;@€”⁹;€“+ạ×d|^&”j”,¤v€⁸F
+Æni@€¢%8
Try it online! (footer formats the output from the native list of numbers to print a representation using the operation symbols from the question)
How?
Ignores checking of string concatenation and exponentiation as they are unnecessary.
Creates two (similar) links of Jelly code and evaluates them to evaluate to find the values of applying each of the 9 dyadic operations with the arguments in both orders. Flattens this into a single list and finds the index at which each value in the range [1,p]
is first found, or 0. Takes the result modulo 8 to collapse the two values for each of reversed subtraction, reversed div
and reversed mod
to a single values.
⁾ðɓ;@€”⁹;€“+_×d|^&”j”,¤v€⁸F - Link 1: evaluate all operations: number a, number b
⁾ðɓ - literal "ðɓ"
”⁹ - literal '⁹'
;@€ - concatenate for €ach -> ["⁹ð","⁹ɓ"]
¤ - nilad followed by link(s) as a nilad:
“+_×d|^&” - literal "+_×d|^&"
”, - literal ','
j - join -> "+,_,×,d,|,^,&"
;€ - concatenate for €ach -> ["⁹ð+,_,×,d,|,^,&","⁹ɓ+,_,×,d,|,^,&"]
⁸ - link's left argument, a
v€ - evaluate €ach with argument a:
- 1 ⁹ð+,_,×,d,|,^,&
- ⁹ - right argument, b
- ð - dyadic chain separation
- , , , , , , - pair results (yields a list like [[[[[[a+b,aạb],a×b],adb],a|b],a^b],a&b])
- + - addition
- _ - subtraction
- × - multiplication
- d - divmod (returns a pair of values)
- | - bitwise-or
- ^ - bitwise-xor
- & - bitwise-and
-
- 2 ⁹ɓ+,_,×,d,|,^,& - just like above except:
- ɓ - dyadic chain separation
- - ...with argument reversal
-
F - flatten the two into one list
+Æni@€¢%8 - Main link: number a, number b
+ - addition -> a+b
Æn - next prime
¢ - call last link (1) as a dyad -> Link(1)(a, b) = [b+a,b-a,b*a,b/a,b%a,b|a,b^a,b&a,a+b,a-b,a*b,a/b,a%b,a|b,a^b,a&b]
i@€ - first index for €ach (e.g. if a=8, b=3 and n=5 the first index of 5 is 10, a-b)
%8 - modulo 8 (... 10%8 = 2, aligning with b-a. Same goes for / and %)