Background
Ramanujan's number, \1729ドル\$, is called a taxi-cab number due to the (possibly apocryphal) tale of Hardy boarding a cab to visit Ramanujan in hospital having this number, which seemed bland to him.
It's since known as the most famous of a class of integers known as "taxicab numbers" which are expressible as the sum of two \$n\$th powers (of positive integers) in two (or sometimes \$k\$) different ways.
\1729ドル\$ is the smallest natural number expressible as the sum of 2 cubes in 2 different ways, making it the first \3,2ドル\$ taxicab number (\$n,k\$ being general).
Challenge
Given a number, decide whether it is a \3,2ドル\$ 'secondary taxicab number' - meaning it fulfils the same constraint as \1729ドル\$ (2 unique sums of cubes), but does not have to be the smallest such integer of the \3,2ドル\$ class (that being 1729, of course).
Example cases
$1729ドル = 10^3 + 9^3 = 12^3 + 1^3 \\ 4104 = 15^3 + 9^3 = 16^3 + 2^3 \\ 13832 = 2^3 + 24^3 = 18^3 + 20^3$$
As well as \20683,ドル 32832, 39312...\$
Scoring
This is code-golf, so the shortest answer in each language wins.
Rough Matlab code to find other cases by brute force:
for k = 1729:20000
C = sum(round(mod(real((k-[1:ceil(k^(1/3))].^3).^(1/3)),1)*10000)/10000==1);
if C > 1
D = (mod(C,2)==0)*C/2 + (mod(C,2)==1)*((C+1)/2);
disp([num2str(k),' has ',num2str(D),' solns'])
end
end
-
\$\begingroup\$ Welcome to PPCG! I edited your question a bit to make it a bit more clear. Would you be willing to add some test cases? \$\endgroup\$musicman523– musicman5232017年06月14日 02:59:21 +00:00Commented Jun 14, 2017 at 2:59
-
\$\begingroup\$ Yep, I was struggling because I'm at work and don't have Matlab, but managed to get Octave online to work and found 4104=16^3+4^3=15^3+9^3 \$\endgroup\$DrQuarius– DrQuarius2017年06月14日 03:11:45 +00:00Commented Jun 14, 2017 at 3:11
-
2\$\begingroup\$ A001235 \$\endgroup\$Shaggy– Shaggy2017年06月14日 07:06:37 +00:00Commented Jun 14, 2017 at 7:06
-
1\$\begingroup\$ Do there need to be exactly two ways to write the number, or at least two? \$\endgroup\$John Dvorak– John Dvorak2017年06月14日 08:19:26 +00:00Commented Jun 14, 2017 at 8:19
-
4\$\begingroup\$ someone should write an answer in Taxi bigzaphod.github.io/Taxi \$\endgroup\$SaggingRufus– SaggingRufus2017年06月14日 12:54:27 +00:00Commented Jun 14, 2017 at 12:54
13 Answers 13
Jelly, 9 bytes
Credits to Erik the Outgolfer.
Œċ*3S€ċ>1
This is too slow that it won't even work for 1729 online.
Much faster, 12 bytes
Credits to Dennis.
R*3fRŒċS€ċ>1
-
\$\begingroup\$ I just tested this with "4104" and it passed. :) Haven't found any beyond this yet, but that was lightning fast! \$\endgroup\$DrQuarius– DrQuarius2017年06月14日 03:10:29 +00:00Commented Jun 14, 2017 at 3:10
-
\$\begingroup\$
Ðf8can becomefR. The second8can be removed. \$\endgroup\$Dennis– Dennis2017年06月14日 06:17:17 +00:00Commented Jun 14, 2017 at 6:17 -
\$\begingroup\$ The second 8 can be removed indeed, but the fR swap leads to failure. \$\endgroup\$DrQuarius– DrQuarius2017年06月14日 06:24:55 +00:00Commented Jun 14, 2017 at 6:24
-
-
1\$\begingroup\$ You don't need to care about speed, just do
Œċ*3S€ċ>1. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年06月14日 12:47:57 +00:00Commented Jun 14, 2017 at 12:47
Mathematica, 35 bytes
Count[#^3+#2^3&~Array~{#,#},#,2]>2&
Pure function taking a positive integer and returning True or False.
#^3+#2^3&~Array~{#,#} tabulates all sums of cubes of two integers between 1 and the input. (This would be much faster with a sensible bound on the integers to be cubed, like the cube root of the input; but that would take precious bytes. As it is, the code takes about 30 seconds on the input 13832 and scales at least quadratically in the input.) Count[...,#,2] counts how many times the input appears in this list at nest-level 2; if this number is greater than 2, then the input is a semi-taxicab number (greater than 2, rather than greater than 1, since a^3+b^3 and b^3+a^3 are being counted separately).
05AB1E, 9 bytes
Code (very slow)
L3mãOQO3›
Code (much faster), 12 bytes
tL3mDδ+ ̃QO3›
Uses the 05AB1E encoding. Try it online!
Explanation
t # Square root (not necessary but added for speed)
L # Create a list [1 .. sqrt(input)]
3m # Raise to the power of 3
D # Duplicate
δ+ # 2 dimensional addition
̃ # Deep-flatten the entire list
Q # Check which are equal to the input
O # Sum up to get the number of equalities
3› # Checks whether there are 4 or more equalities. In order for a number
to be a secondary taxicab number, there are at least two distinct
ways to get to that number and 4 ways when you also take reversed
arguments in account.
-
2\$\begingroup\$ Surely you could save a byte by removing the square root. This is code-golf, not fastest-code. \$\endgroup\$Christian Legge– Christian Legge2017年06月14日 11:19:05 +00:00Commented Jun 14, 2017 at 11:19
-
\$\begingroup\$ @Christian I have added a slow version of the code. \$\endgroup\$Adnan– Adnan2017年06月15日 09:36:32 +00:00Commented Jun 15, 2017 at 9:36
Mathematica, (削除) 38 (削除ここまで) 37 bytes
Tr[1^PowersRepresentations[#,2,3]]>1&
-1 byte thanks to @GregMartin
As always, there is a Mathematica builtin to everything.
-
1\$\begingroup\$ Assuming that the OP is ok with more-than-2-representations, then I believe
Tr[1^...]works in place ofLength@. \$\endgroup\$Greg Martin– Greg Martin2017年06月14日 06:23:51 +00:00Commented Jun 14, 2017 at 6:23
APL (dzaima/APL), 15 bytes
{4≤⍵⍧∘.+⍨3*⍨⍳⍵}
Same method as all the other answers. Only works till 4104 due to brute forcing.
more than 20 bytes golfed off with dzaima's help at the APL orchard.
The output appears with a ⊂ next to it, which has been patched in the latest version of the interpreter.
Explanation
{4≤⍵⍧∘.+⍨3*⍨⍳⍵} ⍵ → input
3*⍨⍳⍵ range 1 to n, cubed
∘.+⍨ dot product, addition with itself
⍵⍧ number of times ⍵ appears in the sums
4≤ is 4 less than or equal to that?
JavaScript (ES7), 63 bytes
A relatively fast recursive function which eventually returns a boolean.
f=(n,k,r=0,x=n-k**3)=>x<0?r>3:f(n,-~k,r+=(x**(1/3)+.5|0)**3==x)
Demo
f=(n,k,r=0,x=n-k**3)=>x<0?r>3:f(n,-~k,r+=(x**(1/3)+.5|0)**3==x)
console.log([...Array(40000).keys()].filter(n => f(n)))
Mathematica, 48 bytes
Length@Solve[x^3+y^3-#==0<x<y,{x,y},Integers]>1&
input
[4104]
output
True
-
\$\begingroup\$ Note that
#!=1729&&is no longer necessary now that the spec has been clarified. \$\endgroup\$Greg Martin– Greg Martin2017年06月14日 06:15:56 +00:00Commented Jun 14, 2017 at 6:15 -
\$\begingroup\$ Could you use
Solverather thanReduce? \$\endgroup\$Ian Miller– Ian Miller2017年06月14日 09:51:37 +00:00Commented Jun 14, 2017 at 9:51 -
\$\begingroup\$ of course...1 byte is 1 byte! \$\endgroup\$ZaMoC– ZaMoC2017年06月14日 10:08:45 +00:00Commented Jun 14, 2017 at 10:08
-
\$\begingroup\$ Can save one more byte with
Length@Solve[x^3+y^3-#==0<x<y,{x,y},Integers]>1&which replaces the&&with-and a little bit of rearranging. \$\endgroup\$Ian Miller– Ian Miller2017年06月14日 10:41:53 +00:00Commented Jun 14, 2017 at 10:41
Python, 71 bytes
lambda x:len([i for i in range(x)for j in range(i,x)if i**3+j**3==x])>1
MATL ((削除) 16 (削除ここまで) 15 bytes) ((削除) 13 (削除ここまで) 12 ideally)
.4^:3^2XN!sG=sq
Explanation:
Based on the Jelly solution of 'Leaky Nun', just converted to MATL, probably redundant in some parts and can be improved:
.4^ % rough cube root of input, as maximum potential integer N.
:3^ % create array of all cubes from 1^3 up to N^3.
2XN % do nchoosek on cube array, creating all possible pairs (k=2) to add.
!s % transpose array and add all pairs to find sums.
G= % find all pairs that equal the original input.
sq % if there is more than one solution, then pass the test.
Note: falsy outputs include 0 and -1, while truthy output is 1. Thanks to Luis Mendo for saving an extra byte here replacing "s1>" with "sq".
Ideally ((削除) 13 (削除ここまで) 12 bytes):
:3^2XN!sG=sq
...is enough, but for larger numbers this crashes on tio.run's page.
-
\$\begingroup\$ Original: MATL (19 bytes) =============== XH.34^:3^2XN!sH=s1> \$\endgroup\$DrQuarius– DrQuarius2017年06月14日 07:28:09 +00:00Commented Jun 14, 2017 at 7:28
-
1\$\begingroup\$ If any truthy output is valid, you can replace
1>byq. Also, you haveHinstead ofGin the explanation. The fact that the program crashes for large numbers is usually irrelevant for scoring. If it works given enough time and memory that's acceptable, unless the challenge specifies otherwise \$\endgroup\$Luis Mendo– Luis Mendo2017年06月14日 08:55:25 +00:00Commented Jun 14, 2017 at 8:55 -
1\$\begingroup\$ Thanks Luis! I'm new to "truthy" outputs, but this works nicely now. Edited to amend. Also, thanks for creating such a fun and easy to use esolang! \$\endgroup\$DrQuarius– DrQuarius2017年06月15日 00:42:51 +00:00Commented Jun 15, 2017 at 0:42
Ruby, 52 bytes
->n{r=*1..n;r.product(r).count{|i,j|i**3+j**3==n}>1}
Since this version creates a massive n2 sized array, it fails on all true testcases higher than 1729, here is a modified version that has a smaller array size of about n2/3, which successfully checks at least up to 31392.
PHP, 76 bytes
for(;$i++<$argn**(1/3);)for($n=1;$n<$i;)$n++**3+$i**3!=$argn?:$c++;echo$c>1;
Add++, 32 bytes
D,g,@,3^AR3€Ω^$€+
L,R€gb+A€=b+2=
The Footer in the TIO link is simply a faster version (b+ \$\to\$ BFB])
How it works
D,g,@, ; Define a helper function, g
; It takes an int; STACK = [5]
3^ ; Cube; STACK = [125]
AR ; Argument range; STACK = [125 [1 2 3 4 5]]
3€Ω^ ; Cube each; STACK = [125 [1 8 27 64 125]]
$€+ ; Add the cube to each; STACK = [[126 133 152 189 250]]
L, ; Define the main function
; Takes an int; STACK = [1729]
R ; Range; STACK = [[1 2 3 ... 1728 1729]]
€g ; Run g over each; STACK = [[[2] [9 16] ... ]]
b+ ; Concatenate; STACK = [[2 9 16 ...]]
A€=b+ ; Count argument; STACK = [2]
2= ; Equals 2? STACK = [1]
Explore related questions
See similar questions with these tags.