28
\$\begingroup\$

Challenge

Given three non-negative integers \$a, b\$ and \$c\$, decide if the sum of their cubes is equal to the concatenation of those numbers, aka: $$ a^{3}+b^{3}+c^{3} = a^\frown b ^\frown c $$

Test cases

Truthy

(1,5,3) // 1^3 + 5^3 + 3^3 = 153
(2,2,13)
(4,0,7)
(10,0,0)
(10,0,1)
(22,18,59)
(98,28,27)
(166,500,333)
(828,538,472)

Falsy

(1,2,3) // 1^3 + 2^3 + 3^3 = 32 != 123
(4,5,6)
(6,0,0)
(166,500,334)
(200,0,200)

You can assume there are no leading zeroes.

This is , so the shortest code wins.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Could you define concentration in the question please \$\endgroup\$
    – Simd
    Commented Aug 5 at 11:03
  • 1
    \$\begingroup\$ @Simd Sorry, did you make a typo? \$\endgroup\$
    – Blue
    Commented Aug 5 at 11:08
  • \$\begingroup\$ From your test cases I assume leading zeroes don't count? \$\endgroup\$
    – Jitse
    Commented Aug 5 at 11:37
  • 2
    \$\begingroup\$ @Jitse well, I didn't include any test cases with leading zeroes because it may cause issues with, for example, "001" being different from "1" and I just wanted people to focus on writing shortest code without getting into edge cases too much. \$\endgroup\$
    – Blue
    Commented Aug 5 at 11:51
  • \$\begingroup\$ Yes, I meant concatenation. \$\endgroup\$
    – Simd
    Commented Aug 5 at 12:36

48 Answers 48

13
\$\begingroup\$

Python 3, 36 bytes

lambda*I:eval(3*"%d"%I+3*"-%d**3"%I)

Try it online!

eval golf of @Jitse's answer who generously declined to include it in their post. Returns zero for True and nonzero integer for False.

\$\endgroup\$
8
\$\begingroup\$

R, 30 29 bytes

Edit: -1 byte thanks to @Kirill L..

\(x)x^2%*%x==Reduce(paste0,x)

Attempt This Online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Save a byte with dot product \$\endgroup\$
    – Kirill L.
    Commented Aug 5 at 18:59
7
\$\begingroup\$

Jelly,  5  4 bytes

-1 thanks to Unrelated String! (*3S {cube then sum} -> ḋ² {dot-product with squared values}.)

ḋ²⁼V

A monadic Link that accepts a list of non-negative integers (with or without any leading zeros*) and yields 1 if the list concatenates to the sum of the cubes of its elements or 0 if not.

Try it online! Or see the test-suite.

How?

ḋ²⁼V - Link: list, A = [a, b, c]
 ²   - square {A} -> [a², b², c²]
ḋ    - {A} dot-product {that} -> a³+b³+c³
   V - evaluate {A} as Jelly code -> concatenated integer *
  ⁼  - {SumOfCubes} equals {that}?

* Leading zeros produce a side effect of printing zeros before yielding the result.

\$\endgroup\$
2
  • \$\begingroup\$ *3S -> ḋ² for 4 \$\endgroup\$ Commented Aug 5 at 14:04
  • 1
    \$\begingroup\$ Very nice golf @UnrelatedString, I often use the dot-product too so should really have thought of this one. Thank you! \$\endgroup\$ Commented Aug 5 at 16:54
7
\$\begingroup\$

Python 3, 44 bytes

lambda x:sum(map(pow,x,[3]*3))-int(3*'%d'%x)

Try it online!

-1 byte thanks to Jonathan Allan

-2 bytes thanks to xnor

Returns truthy (nonzero integers) for False cases and falsey (zero integers) for True cases.

Python 3, 45 bytes

lambda a,b,c:a**3+b**3+c**3-int(f'{a}{b}{c}')

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ You can flip truthy and falsey with == -> -. \$\endgroup\$ Commented Aug 5 at 12:03
  • \$\begingroup\$ 36: lambda*I:eval(3*"%d"%I+3*"-%d**3"%I) \$\endgroup\$ Commented Aug 5 at 12:41
  • 3
    \$\begingroup\$ @Albert.Lang amazing! I'll leave it to you to submit it as a separate answer \$\endgroup\$
    – Jitse
    Commented Aug 5 at 12:46
  • 1
    \$\begingroup\$ @david With the space there, the dot is parsed as an attribute accessor instead of a decimal separator. More here. \$\endgroup\$
    – Jitse
    Commented Aug 7 at 8:10
  • 1
    \$\begingroup\$ Shorter to do map(pow,x,[3]*3) in the second answer \$\endgroup\$
    – xnor
    Commented Aug 10 at 10:01
6
\$\begingroup\$

Uiua, 17 14 12 chars

-3 chars thanks to Chunes by replacing fork with on

-2 chars thanks to OVS because I missed the existance of a power operator

=/+ⁿ3⟜⍜°⋕/⍚⊂

Pad Link

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 14 without the function pack: =/+××..⟜⍜°⋕/⍚⊂ \$\endgroup\$
    – chunes
    Commented Aug 5 at 13:07
  • 1
    \$\begingroup\$ I think ××.. is just ⁿ3 \$\endgroup\$
    – ovs
    Commented Aug 5 at 13:20
  • \$\begingroup\$ This =/+ⁿ3⟜⍜°⋕/◇⊂ is what I had came up with when this was first posted to the Sandbox but I didn't see the challenge is already posted... I'm too slow and I had prep time! \$\endgroup\$ Commented Aug 8 at 17:59
  • \$\begingroup\$ @noodleperson Nice! I needed a lot of help to get to the same result \$\endgroup\$
    – mousetail
    Commented Aug 8 at 20:34
6
\$\begingroup\$

Haskell, 33 bytes

(==).show.sum.map(^3)<*>(>>=show)

Try it online!

  • sum.map(^3) is the sum of the cubes
  • show.sum.map(^3) is stringifies the sum
  • (>>=show) is concatMap show, i.e. concatenetes the numbers
  • <*> feeds the input to both arguments
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I tried non-pointfree and it also came to 33 bytes: Try it online! \$\endgroup\$
    – xnor
    Commented Aug 6 at 5:54
5
\$\begingroup\$

Vyxal, 6 bytes

ṅ?3e∑=

Try it Online!

    ∑  # Sum
  3e   # of cubes
 ?     # of input
     = # Is equal to
ṅ      # Concatenation of input?
\$\endgroup\$
5
\$\begingroup\$

APL (Dyalog Unicode), 16 bytes

+.*∘3=∘⍎∘∊⍕¨

Try it online!

+.*∘3 sum of cubes

=∘⍎∘∊ equal to the execution of the enlisted (flattened)

⍕¨ stringification of each

\$\endgroup\$
5
\$\begingroup\$

JavaScript (Node.js), 32 bytes

(a,b,c)=>a**3+b**3+c**3==[a]+b+c

Try it online!

Nothing to say.

\$\endgroup\$
1
  • \$\begingroup\$ Concatenating a number to a list coerces them to string? Obvious in hindsight :) \$\endgroup\$
    – JollyJoker
    Commented Aug 6 at 6:44
5
\$\begingroup\$

Julia, 26 24 23 bytes

-n="$(n'n.^2)"==join(n)

Attempt This Online!

Thanks to ovs for -2 and further -1 by Ashlin Harris.

\$\endgroup\$
3
  • \$\begingroup\$ Your TIO link for the 0.6 version doesn't actually test that function and I can't get it to work? Anyway, I think n'*n.^2 should work in both julia versions if you pass input as arrays instead of tuples \$\endgroup\$
    – ovs
    Commented Aug 5 at 19:48
  • \$\begingroup\$ @ovs Oops, my bad. Anyway, the older version is no longer needed as it becomes the same length. Thanks! \$\endgroup\$
    – Kirill L.
    Commented Aug 5 at 20:18
  • \$\begingroup\$ Julia supports implicit multiplication, so you don't need the * operator. \$\endgroup\$ Commented Aug 5 at 21:53
4
\$\begingroup\$

K (ngn/k), 17 bytes

{(,/$x)~$+/x*x*x}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Google Sheets, 31 bytes

=SUMPRODUCT(A:A^3)=--JOIN(,A:A) 
\$\endgroup\$
4
\$\begingroup\$

Brachylog, 7 bytes

^₃ᵐ+~c?

Try it online!

Explanation

^₃ᵐ        Map cube on the input list
   +       The sum of these cubes…
    ~c?    …can be deconcatenated into the original input list
\$\endgroup\$
4
\$\begingroup\$

Bash, 28

Sometimes the simple solution is best:

(($1**3+$2**3+$3**3^$1$2$3))

This script takes a list as command-line parameters as input. e.g. ./cubeconcat.sh 1 5 3. It returns a shell exit code: 1 for True, 0 for False.

Try it online!


Previous, overcomplicated solution:

Bash, 37

(($(eval printf %s +\$[$1**3] ^ $1)))

This script takes a single quoted brace-enclosed, comma-separated list as input. e.g. ./cubeconcat.sh "{1,5,3}". It returns a shell exit code: 1 for True, 0 for False.

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Desmos, 81 67 bytes

-14 bytes thanks to @emanresu A due to the fact that I didn't realize that it was always guaranteed to be exactly 3 numerical inputs. I was originally solving for an arbitrary amount of numerical inputs.

c(x,y)=x10^{floor(log(y+0^y))+1}+y
f(x,y,z)=c(c(x,y),z)-x^3-y^3-z^3

Returns 0 for truthy and a non-zero integer for falsey.

Try It On Desmos!

Try It On Desmos! - Prettified

\$\endgroup\$
5
  • \$\begingroup\$ 60: c(x,y)=x*10^{ceil(log y)}+y[NEWLINE]f(x,y,z)=c(x,c(y,z))-x^3-y^3-z^3 (I'm not that experienced with Desmos golf, this can probably be a little less) \$\endgroup\$
    – emanresu A
    Commented Aug 7 at 11:17
  • \$\begingroup\$ Actually disregard that it doesn't play nice with zeros, there's probably a trick somewhere though \$\endgroup\$
    – emanresu A
    Commented Aug 7 at 11:20
  • \$\begingroup\$ c(x,y)=x*10^{floor(log(y+0^y))+1}+y[NEWLINE]f(x,y,z)=c(c(x,y),z)-x^3-y^3-z^3 seems to work for 68, since leading zeros won't happen \$\endgroup\$
    – emanresu A
    Commented Aug 7 at 11:24
  • \$\begingroup\$ @emanresuA Wait what??? You can assume there's only three numbers inputted????? \$\endgroup\$
    – Aiden Chow
    Commented Aug 7 at 22:40
  • \$\begingroup\$ Man they gotta make three bolded, I just skipped right over that word. \$\endgroup\$
    – Aiden Chow
    Commented Aug 7 at 23:00
4
\$\begingroup\$

Excel, 23 Bytes

An anonymous workbook function that takes input from a vertical array at A1# and outputs to the calling cell

--(A1&A2&A3)=SUM(A1#^3)

Previous Version, 24 bytes

A slightly more elegant solution that uses the Concat builtin

--CONCAT(A1#)=SUM(A1#^3)

Sample Output

For the input array ={1;5;3} outputs the following

Output Example

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

Perl 5 -MList::Util=sum -ap, 27 bytes

$_=y/ //dr==sum map$_**3,@F

Try it online!

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

Red, 41 bytes

func[a][(sum a * a * a)= do rejoin to[]a]

Try it online!

Takes input as a vector of three numbers. Vectors support arithmetic with multiple values at once, but not powers.

Explanation

func[a][         ; start function taking argument a
(sum a * a * a)  ; sum of cubes
=                ; does it equal...
to[]a            ; input converted to regular list
rejoin           ; concatenated to string
do               ; evaluated (converted to integer)
]                ; end function
\$\endgroup\$
3
\$\begingroup\$

Stack control 1.1, 6 characters

:³+⇆◡=

Explanation

:  - duplicates original array
³  - cubes every array element
+  - sum of array elements
⇆  - swap to another array
◡  - reversed concatenates values from array
=  - check if values are equals
\$\endgroup\$
3
\$\begingroup\$

Javascript (V8), 40 bytes

a=>a.reduce((x,y)=>x+y**3,0)==a.join('')

where a is number[], so it should work for any number of arguments.

Try it online!

33 bytes

Thanks @Neil and @tsh for the shorter version!

a=>a.map(x=>s-=x**3,s=a.join``)|s
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I think a.join`` works. \$\endgroup\$
    – Neil
    Commented Aug 9 at 7:44
  • 1
    \$\begingroup\$ By default, decision-problem on this site allow truthy vs falsy output, and swap meaning of true and false is allowed. So you could use - to replace your ==. Combine with Neil's idea. It could be stripped into 35 bytes: a=>a.reduce((x,y)=>x-y**3,a.join``) \$\endgroup\$
    – tsh
    Commented Aug 9 at 9:24
  • 1
    \$\begingroup\$ And switch to map could save two more bytes: a=>a.map(x=>s-=x**3,s=a.join``)|s \$\endgroup\$
    – tsh
    Commented Aug 9 at 9:26
  • \$\begingroup\$ @tsh I forgot that arrow functions do not have their own scope. That's clever! \$\endgroup\$
    – Bean Sean
    Commented Aug 10 at 10:07
3
\$\begingroup\$

MathGolf, 5 bytes

‼yⁿΣ=

Try it online.

Explanation:

‼     # Apply the next two operands separately on the (implicit) input-triplet:
 y    #  Join them together
  ⁿ   #  Take the cube of each
   Σ  # Sum the triplet of cubes
    = # Check if the two lists are the same
      # (after which the entire stack joined together is output implicitly)
\$\endgroup\$
3
\$\begingroup\$

Japt, 5 bytes

x³¥U¬

Try it here

x³¥U¬     :Implicit input of array U
x         :Sum of
 ³        :  Cubes
  ¥       :Loosely equal to
   U¬     :  U joined to a string
\$\endgroup\$
3
\$\begingroup\$

ARBLE, 21 bytes

a^3+b^3+c^3-(a..b..c)

0 for truthy, non-zero for falsey.

Try it online!

Run all tests!

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

APL+WIN, 17 bytes

Prompts for integers.

(⍎∊⍕¨s)=+/(s←⎕)*3

Try it online! Thanks to Dyalog Classic

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

Arturo, 31 bytes

$->a->=do join a∑map a=>[&^3]

Try it!

Explanation

$->a->        ; a function taking a list named a
=             ; are the following two values equal?
do join a     ; concatenation of input as a number
∑             ; and sum of...
map a=>[&^3]  ; inputs cubed
\$\endgroup\$
2
\$\begingroup\$

Setanta, 84 66 bytes

gniomh(a,b,c){toradh a*a*a+b*b*b+c*c*c==go_uimh(nasc@[a,b,c](""))}

try-setanta.ie link

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

Kotlin 52 Bytes

{it.map{it*it*it}.sum()-it.joinToString("").toInt()}

Accepts a List<Int> and outputs zero for true and a non-zero integer for false.

Try it online

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

C++, 186 165 146 bytes

#include<iostream>
using namespace std;int main(){int a,b,c;cin>>a>>b>>c;cout<<(a*a*a+b*b*b+c*c*c==stoi(to_string(a)+to_string(b)+to_string(c)));}

(-16 thanks to Mousetail) (-19 thanks to Blue)

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ that's fair, didn't see that. give me a second \$\endgroup\$
    – Redz
    Commented Aug 5 at 11:15
  • \$\begingroup\$ @emanresuA fixed \$\endgroup\$
    – Redz
    Commented Aug 5 at 11:19
  • \$\begingroup\$ You can save 16 bytes with cout<<(d==stoi(x)); \$\endgroup\$
    – mousetail
    Commented Aug 5 at 11:41
  • 1
    \$\begingroup\$ You can get rid of d altogether! cout<<(a*a*a+b*b*b+c*c*c==stoi(x)); \$\endgroup\$
    – Blue
    Commented Aug 5 at 12:06
  • \$\begingroup\$ @Blue thanks! good point :P \$\endgroup\$
    – Redz
    Commented Aug 5 at 22:12
2
\$\begingroup\$

Windows Batch, 43 bytes

cmd/cset/a%1*%1*%1+%2*%2*%2+%3*%3*%3-%1%2%3

Prints 0 for truthy, <>0 for falsy.

Sorry, no TIO available; save as C:\Temp\cg274690.cmd or whatever.cmd, run from cmd.exe or PowerShell:
C:\Temp\cg274690.cmd 1 5 3

Ungolfed:

cmd.exe /c set /a %1 * %1 * %1 + %2 * %2 * %2 + %3 * %3 * %3 - %1%2%3

This starts another instance of cmd.exe, as using set /a inside a batch script requires an assignment, which would then have to be echoed out, for a total of 49 bytes (using LF as EOL):

set/ax=%1*%1*%1+%2*%2*%2+%3*%3*%3-%1%2%3
echo %x%

set /a used interactively inside a cmd prompt (or passed with /c) prints the result directly.

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

Ruby, 30 bytes

->a{a.sum{|x|x**3}.to_s==a*''}

Try it online!

\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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