30
\$\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\$ Commented Aug 5, 2024 at 11:03
  • 1
    \$\begingroup\$ @Simd Sorry, did you make a typo? \$\endgroup\$ Commented Aug 5, 2024 at 11:08
  • \$\begingroup\$ From your test cases I assume leading zeroes don't count? \$\endgroup\$ Commented Aug 5, 2024 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\$ Commented Aug 5, 2024 at 11:51
  • \$\begingroup\$ Yes, I meant concatenation. \$\endgroup\$ Commented Aug 5, 2024 at 12:36

53 Answers 53

1
2
2
\$\begingroup\$

Charcoal, 9 bytes

⁼I⪫θωΣXθ³ 

Try it online! Link is to verbose version of code. Takes input as an array and outputs a Charcoal boolean, i.e. - if the sum of cubes equals the concatenation, nothing if not. Explanation:

 θ Input array ⪫ Joined by ω Predefined variable empty string I Cast to integer ⁼ Equals θ Input array X Vectorised raised to power ³ Literal integer `3` Σ Take the sum Implicitly print 
\$\endgroup\$
2
\$\begingroup\$

J, 15 bytes

;(=1#.^&3)&:".> 

Try it online!

Takes input as list of boxed strings.

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

gnuplot, 33 bytes

f(a,b,c)=a**3+b**3+c**3=="".a.b.c 

Try it online!

A function that inputs 3 integer values and outputs 1 if their sum of cubes is equal to the concatenation of those numbers, otherwise the output is 0.

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

Lua, 38 bytes

x,y,z=...print(x^3+y^3+z^3==0|x..y..z) 

Try it online!

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

JavaScript (Node.js), 31 bytes

(x,y,z)=>[x]+y+z-x**3-y**3-z**3 

Try it online!

A boring trivial solution is short...

\$\endgroup\$
1
  • \$\begingroup\$ You could save 2 bytes by taking the arguments as strings. \$\endgroup\$ Commented Aug 16, 2024 at 15:38
2
\$\begingroup\$

05AB1E, 6 bytes

3mOIJQ 

Try it online or verify all test cases.

The 3m could alternatively be ** or n* for the same byte-count; or the order could be swapped to JI3mOQ as well:
Try it online.

Explanation:

3m # Take the cube of each value in the (implicit) input-triplet O # Sum those together I # Push the input-triplet again J # Join them together Q # Check if the two are the same # (after which the result is output implicitly) 
\$\endgroup\$
2
\$\begingroup\$

Go, 108 bytes

import."fmt" func f(a,b,c int)bool{n:=0 Sscanf(Sprintf("%d%d%d",a,b,c),"%d",&n) return a*a*a+b*b*b+c*c*c==n} 

Attempt This Online!

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

AWK, 24 21 bytes

$1$2$3~$1^3+$2^3+$3^3 

Try it online!

Prints the digits if true, or returns nothing.

\$\endgroup\$
1
\$\begingroup\$

Factor, 46 45 bytes

[ 4 dupn v* v. swap "%d%d%d"vsprintf dec> = ] 

Try it online!

Explanation

[ ! start function 4 dupn ! put 4 total copies of input list on stack v* ! multiply top two v. ! dot product w/ third swap ! put input on top "%d%d%d"vsprintf ! concat all three to string dec> ! to number = ! check equality ] ! end function 
\$\endgroup\$
1
\$\begingroup\$

MATLAB, 41 bytes

f=@(x)sum(x.^3)==str2num(sprintf('%d',x)) 

An @-function that accepts input as column vector.

>> f([828;538;472]) ans = logical 1 >> f([200;0;200]) ans = logical 0 
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the site and nice first answer! You may skip f= from your bytecount. Also for the demonstration purposes and to have the bytes counted you can use TIO website. MATLAB isn't available there, but you try GNU Octave . See this example. \$\endgroup\$ Commented Aug 7, 2024 at 8:34
1
\$\begingroup\$

Pyth, 9 bytes

qsjkQsm** 

Try it online!

First time golfing in Pyth, so this might be not optimal.

Explanation:

sm** calculates the sum of the cubes;

jkQ joins the list elements into a string and s converts the result to a number;

q: are the values equal?

\$\endgroup\$
1
\$\begingroup\$

Retina, 54 bytes

,(\d+),(\d+) $1$2,$.(***_$2*$2*$2*_$`*$`*$`* ^(.+),\1$ 

Try it online! Link includes test cases. Explanation:

,(\d+),(\d+) 

Match the first number in $`, the second number in $1 and the third number in $2.

$1$2,$.(***_$2*$2*$2*_$`*$`*$`* 

Concatenate the input numbers, then compute the sum of the cube of $1 (implicitly because it's the first number in the match), the cube of $2 and the cube of $`. (Retina 1 will do this calculation using arbitrary-precision integers as it knows it will be converting the result back to decimal.)

^(.+),\1$ 

Check that the two results are the same.

\$\endgroup\$
1
\$\begingroup\$

Bash, 33 bytes

[ $1$2$3 = $[$1**3+$2**3+$3**3] ] 

Try it online!

The script returns a shell exit code: 1 for True, 0 for False.

\$\endgroup\$
1
\$\begingroup\$

Java (JDK), 120 bytes

t->t.stream().mapToInt(x->x*x*x).sum()==Integer.parseInt(t.stream().map(Object::toString).collect(Collectors.joining())) 

Try it online!

I'm not sure if the import of java.util.List needs to be counted. The predicate takes a List and some old advice says that all the types for lambdas need to be included, with any imports, but in JShell it is imported by default and in JDK 23 (which is currently only a preview edition) you also get it for free in a Java program that is all in one file (you can even skip declaring a class or having a full public-static-void-main), but TIO doesn't support JShell or recent versions of Java.

\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 31 30 bytes

boring wins

s[#.#^2]==s/@#<>""& s=ToString 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PowerShell Core, 45 bytes

-join$args-(($args|%{"$_*"*3+1})-join'+'|iex) 

Try it online!

Takes 3 integers as parameters
Returns \$0\$ for true, otherwise false=

\$\endgroup\$
1
\$\begingroup\$

Clojure, 56 bytes

#(=(seq(str(apply +(for[i %](* i i i)))))(mapcat str %)) 

TIO. This was surprisingly painless, thanks to str and mapcat. Takes the input as a list of numbers.

\$\endgroup\$
1
\$\begingroup\$

Husk, 8 bytes

=dṁd¹ṁ^3 

Try it online!

Takes a list of numbers and outputs 1 (truthy) "if the sum of their cubes is equal to the concatenation of those numbers", otherwise 0 (falsy).

Explanation:

 ṁ^3 # sum up the cubes ṁd¹ # split every number into its digits and flatten the list d # join digits back into a single number = # check for the equality 
\$\endgroup\$
1
\$\begingroup\$

AWK, 24 bytes

$0=$1^3+$2^3+$3^3~$1$2$3 
awk '$0=$1^3+$2^3+$3^3~$1$2$3' << "1 5 3" 

Prints 1 if truthy, or nothing.

\$\endgroup\$
1
\$\begingroup\$

Desmos, 66 bytes

d(n)=10^{floor(log(n+0^n))+1} f(a,b,c)=a^3+b^3+c^3-c-d(c)(b+d(b)a) 

This solution evolved very close to Aiden Chow's solution, but it does improve it by one byte. 0 represents truthy outputs, any any other integer represents falsey.

Try it on Desmos!

\$\endgroup\$
1
\$\begingroup\$

Jalapeño, 4 bytes

J=Σₓ³ 

Explained

J=Σₓ³ J # Join (implicit input) together = # Is equal to Σₓ # The sum of (implicit input) mapped by ³ # Cubed 

Hex-Dump of Bytecode

 0 1 2 3 4 5 6 7 8 9 A B C D E F 0000: 14 59 ea 62 

Try it Online!

\$\endgroup\$
1
\$\begingroup\$

APL(NARS), 15 chars

{(∊⍕¨⍵)≡⍕+/⍵*3} 

test:

 {(∊⍕¨⍵)≡⍕+/⍵*3}10 0 0 1 ~ {(∊⍕¨⍵)≡⍕+/⍵*3}10 0 1 1 ~ {(∊⍕¨⍵)≡⍕+/⍵*3}¨(1 5 3)(2 2 13)(4 0 7)(828 538 472) ┌4───────┐ │ 1 1 1 1│ └~───────┘ {(∊⍕¨⍵)≡⍕+/⍵*3}¨(1 2 3)(4 5 6)(6 0 0)(166 500 334) ┌4───────┐ │ 0 0 0 0│ └~───────┘ 
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 174 bytes

char c[99];j;k; #define F(x)atoi(x)*atoi(x)*atoi(x) main(int a,char**v){strcpy(c,v[1]);while(k++-2)strcpy(c+(j+=strlen(v[k])),v[k+1]);exit(atoi(c)!=F(v[1])+F(v[2])+F(v[3]));} 

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 108 bytes \$\endgroup\$ Commented Feb 7 at 9:01
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.