13
\$\begingroup\$

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 nth 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 fulfills 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 , 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
\$\endgroup\$
  • \$\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 Jun 14 '17 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 Jun 14 '17 at 3:11
  • 2
    \$\begingroup\$ A001235 \$\endgroup\$ – Shaggy Jun 14 '17 at 7:06
  • 1
    \$\begingroup\$ Do there need to be exactly two ways to write the number, or at least two? \$\endgroup\$ – John Dvorak Jun 14 '17 at 8:19
  • 2
    \$\begingroup\$ someone should write an answer in Taxi bigzaphod.github.io/Taxi \$\endgroup\$ – SaggingRufus Jun 14 '17 at 12:54

10 Answers 10

4
\$\begingroup\$

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.
\$\endgroup\$
  • 2
    \$\begingroup\$ Surely you could save a byte by removing the square root. This is code-golf, not fastest-code. \$\endgroup\$ – scatter Jun 14 '17 at 11:19
  • \$\begingroup\$ @Christian I have added a slow version of the code. \$\endgroup\$ – Adnan Jun 15 '17 at 9:36
6
\$\begingroup\$

Jelly, 9 bytes

Credits to Erik the Outgolfer.

Œċ*3S€ċ>1

Try it online!

This is too slow that it won't even work for 1729 online.

Much faster, 12 bytes

Credits to Dennis.

R*3fRŒċS€ċ>1

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I just tested this with "4104" and it passed. :) Haven't found any beyond this yet, but that was lightning fast! \$\endgroup\$ – DrQuarius Jun 14 '17 at 3:10
  • \$\begingroup\$ Ðf⁸ can become fR. The second can be removed. \$\endgroup\$ – Dennis Jun 14 '17 at 6:17
  • \$\begingroup\$ The second ⁸ can be removed indeed, but the fR swap leads to failure. \$\endgroup\$ – DrQuarius Jun 14 '17 at 6:24
  • \$\begingroup\$ By the way, this is code-golf so we don't care about speed ;) but you can still include the fast version in the TIO link. \$\endgroup\$ – user41805 Jun 14 '17 at 8:23
  • 1
    \$\begingroup\$ You don't need to care about speed, just do Œċ*3S€ċ>1. \$\endgroup\$ – Erik the Outgolfer Jun 14 '17 at 12:47
5
\$\begingroup\$

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).

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 38 37 bytes

Tr[1^PowersRepresentations[#,2,3]]>1&

-1 byte thanks to @GregMartin

As always, there is a Mathematica builtin to everything.

\$\endgroup\$
  • 1
    \$\begingroup\$ Assuming that the OP is ok with more-than-2-representations, then I believe Tr[1^...] works in place of Length@. \$\endgroup\$ – Greg Martin Jun 14 '17 at 6:23
2
\$\begingroup\$

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)))

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 48 bytes

Length@Solve[x^3+y^3-#==0<x<y,{x,y},Integers]>1&

input

[4104]

output

True

\$\endgroup\$
  • \$\begingroup\$ Note that #!=1729&& is no longer necessary now that the spec has been clarified. \$\endgroup\$ – Greg Martin Jun 14 '17 at 6:15
  • \$\begingroup\$ Could you use Solve rather than Reduce? \$\endgroup\$ – Ian Miller Jun 14 '17 at 9:51
  • \$\begingroup\$ of course...1 byte is 1 byte! \$\endgroup\$ – J42161217 Jun 14 '17 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 Jun 14 '17 at 10:41
1
\$\begingroup\$

Python, 71 bytes

Try it online

lambda x:len([i for i in range(x)for j in range(i,x)if i**3+j**3==x])>1
\$\endgroup\$
1
\$\begingroup\$

MATL (16 15 bytes) (13 12 ideally)

.4^:3^2XN!sG=sq

Try it online!

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.

\$\endgroup\$
  • \$\begingroup\$ Original: MATL (19 bytes) =============== XH.34^:3^2XN!sH=s1> \$\endgroup\$ – DrQuarius Jun 14 '17 at 7:28
  • 1
    \$\begingroup\$ If any truthy output is valid, you can replace 1> by q. Also, you have H instead of G in 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 Jun 14 '17 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 Jun 15 '17 at 0:42
0
\$\begingroup\$

Ruby, 52 bytes

->n{r=*1..n;r.product(r).count{|i,j|i**3+j**3==n}>1}

Try it online!

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.

Try it online! (modified)

\$\endgroup\$
0
\$\begingroup\$

PHP, 76 bytes

for(;$i++<$argn**(1/3);)for($n=1;$n<$i;)$n++**3+$i**3!=$argn?:$c++;echo$c>1;

Try it online!

Search till 400000 Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.