# Ryley's Theorem

S. Ryley proved following theorem in 1825:

Every rational number can be expressed as a sum of three rational cubes.

### Challenge

Given some rational number $$\r \in \mathbb Q \$$ find three rational numbers $$\a,b,c \in \mathbb Q\$$ such that $$r= a^3+b^3+c^3.$$

### Details

Your submission should be able to compute a solution for every input given enough time and memory, that means having for instance two 32-bit int representing a fraction is not sufficient.

### Examples

\begin{align} 30 &= 3982933876681^3 - 636600549515^3 - 3977505554546^3 \\ 52 &= 60702901317^3 + 23961292454^3 - 61922712865^3 \\ \frac{307}{1728} &= \left(\frac12\right)^3 + \left(\frac13\right)^3 + \left(\frac14\right)^3 \\ 0 &= 0^3 + 0^3 + 0^3 \\ 1 &= \left(\frac12\right)^3 + \left(\frac23\right)^3 + \left(\frac56\right)^3\\ 42 &= \left(\frac{1810423}{509232}\right)^3 + \left(\frac{-14952}{10609}\right)^3 + \left(\frac{-2545}{4944}\right)^3 \end{align}

• I had something that kind-of worked in Japt, but it often ran into a "too much recursion" error. Probably because the strategy was "get random numbers, try again until they're a correct answer". – Kamil Drakari Nov 4 '18 at 2:54
• Requiring bignum support unnecessarily excludes a lot of languages, and/or requires a lot of wasted boilerplate to implement them – Sparr Nov 4 '18 at 5:54
• @Sparr This was a deliberate choice, as the output could be quite "large" even for simple inputs, or depending on what method you use, the intermediate values in the calculation could also be very large. So working with arbitrary precision numbers was an important point for this challenge (and probably quite often in other number-theory-challenges too). – flawr Nov 4 '18 at 9:12
• Would it be acceptable to output [p1,p2,p3,q], interpreted as $\left(\frac{p_1}{q}\right)^3+\left(\frac{p_2}{q}\right)^3+\left(\frac{p_3}{q}\right)^3$? – Arnauld Nov 4 '18 at 13:58
• Along a similar vein, do the three rational numbers outputted have to be in simplest form? – Quintec Nov 4 '18 at 14:36

# Pari/GP, 40 bytes

r->[x=27*r^3+1,9*r-x,z=9*r-27*r^2]/(3-z)


Try it online!

The same length, the same formula:

r->d=9*r^2-3*r+1;[x=r+1/3,3*r/d-x,1/d-1]


Try it online!

$$r=\left(\frac{27r^3+1}{27r^2-9r+3}\right)^3+\left(\frac{-27r^3+9r-1}{27r^2-9r+3}\right)^3+\left(\frac{-27r^2+9r}{27r^2-9r+3}\right)^3$$

Check it online!

• -5 bytes because you can change the order of the summands – Black Owl Kai Nov 4 '18 at 8:42
• @BlackOwlKai The numerator of the second summand is $-27r^{\color{Red}3}+9r-1$, not $-27r^{\color{Red}2}+9r-1$. – alephalpha Nov 4 '18 at 12:42

f x=[w|n<-[1..],w<-mapM(\_->[-n,1/n-n..n])"IOU",x==sum((^3)<$>w)]!!0  Try it online! Simple bruteforce solution. It tests all triples of rational numbers of the form $$\left(\frac{a_1}{n},\frac{a_2}{n},\frac{a_3}{n}\right)\qquad\text{with }-n\le\frac{a_i}{n}\le n.$$ • We can always assume that the three rational numbers have the same denominator, since $$\left(\frac{a_1}{n_1},\frac{a_2}{n_2},\frac{a_3}{n_3}\right)=\left(\frac{a_1n_2n_3}{n_1n_2n_3},\frac{a_2n_1n_3}{n_1n_2n_3},\frac{a_3n_1n_2}{n_1n_2n_3}\right).$$ • We can always assume that $$\-n\le\frac{a_i}{n}\le n\$$, since $$\frac{a_i}{n}=\frac{a_iN}{nN}$$ for any arbitrarily large integer $$\N\$$. • What does the "IOU" do? – Solomon Ucko Nov 3 '18 at 21:54 • @SolomonUcko Nothing special, it's as good as any other list of length 3 – Delfad0r Nov 3 '18 at 21:57 • @H.PWiz I couldn't find any consensus on Meta on whether assuming typed input is accepted, but I still found a way to shorten the code without that assumption. Thanks! – Delfad0r Nov 3 '18 at 22:13 • @Delfad0r There is a "consensus" that you do not have to count a possible import, that is only needed to construct the type needed, if you do not explicitly need anything from that import for defining your function. (And you can assume that the correct type is passed to your function, when it is called.) – flawr Nov 3 '18 at 22:14 • Save one byte by using [-n,1/n-n..n] – Christian Sievers Nov 4 '18 at 13:10 # Husk, 14 bytes ḟo=⁰ṁ^3π3×/NİZ  Simple brute force solution. Try it online! ## Explanation Division in Husk uses rational numbers by default and Cartesian products work correctly for infinite lists, making this a very straightforward program. ḟo=⁰ṁ^3π3×/NİZ İZ Integers: [0,1,-1,2,-2,3,-3... N Natural numbers: [1,2,3,4,5... ×/ Mix by division: [0,1,0,-1,1/2,0,2,-1/2,1/3... This list contains n/m for every integer n and natural m. π3 All triples: [[0,0,0],[0,0,1],[1,0,0]... ḟ Find the first one ṁ^3 whose sum of cubes o=⁰ equals the input.  # JavaScript (Node.js), 73 bytes Takes input as (p)(q), where $$\p\$$ and $$\q\$$ are BigInt literals. Returns [[p1,q1],[p2,q2],[p3,q3]] such that $$\\frac{p}{q}=\left(\frac{p_1}{q_1}\right)^3+\left(\frac{p_2}{q_2}\right)^3+\left(\frac{p_3}{q_3}\right)^3\$$. p=>q=>[x=p*(y=p*(p*=9n*q*q)*3n/q)/q+(q*=q*q),p-x,p-=y].map(x=>[x,3n*q-p])  Try it online! # Haskell, 70 bytes In An introduction to the Theory of Numbers (by Hardy and Wright) there is an construction that even includes a rational parameter. For golfing purposes I just set this parameter to 1, and tried reducing as much as possible. This results in the formula $$r \mapsto \left[ {{r^3-648\,r^2+77760\,r+373248}\over{72\,\left(r+72\right)^2 }} , {{12\,\left(r-72\right)\,r}\over{\left(r+72\right)^2}} , -{{r^2 -720\,r+5184}\over{72\,\left(r+72\right)}} \right]$$ f r|t<-r/72,c<-t+1,v<-24*t/c^3,a<-(v*t-1)*c=((a+v*c+c)/2-)<$>[a,v*c,c]

$_=eval;($a,$b)=($_*9,$_**2*27);$c=$b*$_;say for map$_/($b-$a+3),$c+1,-$c+$a-1,-$b+$a

You can save 8 bytes (the leading \$_=eval;) if you know the input is an integer; this part is needed to have the program grok an input of the form 308/1728. Input is read from STDIN. I'm using the formula given by @alephalpha.