S. Ryley于1825年证明了该定理:
每个有理数可以表示为三个有理立方体的总和。
挑战
鉴于一些有理数发现三个有理数,使得
细节
给定足够的时间和内存,您的提交应该能够为每个输入计算一个解决方案,这意味着仅用两个32位int
表示一个分数是不够的。
例子
[p1,p2,p3,q]
解释为?
S. Ryley于1825年证明了该定理:
每个有理数可以表示为三个有理立方体的总和。
鉴于一些有理数发现三个有理数,使得
给定足够的时间和内存,您的提交应该能够为每个输入计算一个解决方案,这意味着仅用两个32位int
表示一个分数是不够的。
[p1,p2,p3,q]
解释为?
Answers:
r->[x=27*r^3+1,9*r-x,z=9*r-27*r^2]/(3-z)
相同的长度,相同的公式:
r->d=9*r^2-3*r+1;[x=r+1/3,3*r/d-x,1/d-1]
该公式在: Richmond,H.(1930)中给出。上的Rational解决方案。爱丁堡数学学会会议论文集,2(2),92-100。
f x=[w|n<-[1..],w<-mapM(\_->[-n,1/n-n..n])"IOU",x==sum((^3)<$>w)]!!0
Simple bruteforce solution. It tests all triples of rational numbers of the form
[-n,1/n-n..n]
ḟo=⁰ṁ^3π3×/NİZ
Simple brute force solution. Try it online!
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.
Takes input as (p)(q)
, where and are BigInt literals.
Returns [[p1,q1],[p2,q2],[p3,q3]]
such that .
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])
Derived from H. W. Richmond (1930), On Rational Solutions of x3 + y3 +z3 = R.
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
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.