# Square-Cube Digit Usage

The point of this challenge is to find the smallest positive integer that uses up at least all of the digits supplied in the input after it is squared and cubed.

So, when provided an input such as 0123456789 (i.e. a number that finds the result):

69² = 4761
69³ = 328509


It means that 69 is the result of such an input. (Fun fact: 69 is the smallest number that uses up all of the decimal digits of 0 to 9 after squaring and cubing.)

## Specification

The input doesn't have to be unique. For example, the input can be 1466 and here's the result:

4² = 16
4³ = 64


That means we can't just fulfill this input by just doing a number that only uses 1 digit of 6, it has to have 2 digits of 6 in the output.

## Test cases

Here's an exhaustive list of all numbers from 1 to 10000.

1333 (or 3133/3313/3331) -> 111


## Input specification

• You may take input as a list of digits.
• Since the test cases (somehow) have a bug in it, here's an extra rule: the 0's in the input will be ignored.
• I went "Huh?" when I first saw it, but I think I understand. Feb 27, 2020 at 14:16
• Everything before "the point of this challenge" is extremely unclear. Also using 1 as an example input is probably the worst possible choice to make your explanation clear. Feb 27, 2020 at 14:22
• Will the input always be sorted from smallest to largest? Feb 27, 2020 at 20:01
• Can you explain what happens when there are zeros in the input. For example with an input of 50 your list says the output should be 5 which gives us the digits 25 125 which contains no zeros. Does this mean we can ignore zeros in the input? because your example with 69 as an output did not imply that. Feb 27, 2020 at 20:43
• @LevelRiverSt I think that list was created with a buggy implementation that didn't check for the square being zero when the cube was zero. My answer doesn't have that issue. Feb 27, 2020 at 21:38

# 05AB1E, 1412 13 bytes

-2 bytes thanks to @KevinCruijssen

+1 byte thanks to @Grimmy

∞.Δ23SmJœIÅ?Z


Try it online!

### Explanation

∞.Δ                  - First number that...
23Sm              - Power of 2 and 3 [n^2, n^3]
J             - Concatenated
œ            - Permutations of this number


By the way it's quite slow...

• Yup, looks good now! Feb 27, 2020 at 15:39
• Thanks @Grimmy not used the Å? much and think I got my a and b mixed up Feb 27, 2020 at 15:42

# Python 3, 78 bytes

f=lambda s,n=1:n*all(f'{n*n}{n**3}'.count(i)>=s.count(i)for i in s)or f(s,n+1)


Try it online!

• Ah, very nice use of f-string. Just posted to see you're a few steps ahead! T_T Feb 27, 2020 at 14:35

# Japt-æ, 17161514 11 bytes

Still very not happy with this! A little happier! Now I'm happy!

Takes input as an integer.

²+U³s)á dèN


Try it

²+U³s)á dèN     :Implicit map of each U in the range [0,input)
²               :U squared
+              :Concatenate
U³            :  U cubed
s           :  Converted to a string (preventing the + from adding the 2 numbers)
)          :End concatenate
á         :All permutations
d       :Any truthy (not 0) when
è      :  Counting the occurrences of
N     :    The array of inputs, which is implicitly cast to a string
:Implicit output of first U to return true


# Python 2, 82 $$\\cdots\$$78 77 bytes

Added 2 bytes to fix an error kindly pointed out by S.S. Anne.
Switched to Python 2 thanks to Grimmy.
Saved a byte thanks to Arnauld!!!

f=lambda s,i=1:i*all((i*i+i**3).count(c)/s.count(c)for c in s)or f(s,i+1)


Try it online!

# Jelly, 14 bytes

1*2,3DFœ&Ƒ@ʋ1#


Try it online!

A monadic link taking a list of digits and returning an integer in a single element list.

1          ʋ1# | Start with 1 and find the first integer where the following is true, using the input digit list as the right argument:
*2,3          | - To the power of 2 and 3
D         | - Convert to lists of decimal digits
F        | - Flatten
œ&Ƒ@    | - Check whether the inout digit list is invariant when intersected with this list of digits


# Wolfram Language (Mathematica), 89 bytes

1//.t_/;ContainsNone[Subsets[Join@@IntegerDigits[t^{2,3}],Length@#],Permutations@#]:>t+1&


Try it online!

# JavaScript (ES7),  77  72 bytes

Takes input as a list of digits.

f=(a,k)=>([...[k*k]+k**3].sort()+'').match(a.sort().join.*)?k:f(a,-~k)


Try it online!

### Commented

f = (                     // f is a recursive function taking:
a,                      //   a[] = input
k                       //   k   = counter, initially undefined
) =>                      //
( [...[k * k] + k ** 3] // concatenate k² and k³ and split the resulting string
.sort()               // sort from lowest to highest digit
+ ''                  // coerce back to a string (this puts commas between the
// digits, but they are harmless)
).match(                // test whether it matches:
a.sort()              //   the input list sorted the same way
.join.*             //   joined with .* patterns, so that unused digits and
//   commas are ignored
) ?                     // if it does:
k                     //   stop recursion and return k
:                       // else:
f(a, -~k)             //   try again with k + 1


# R, 142134 121 bytes

i=1;x=table(scan());l=function(t)x>table(strsplit(paste0(t^2,t^3),"")[])[names(x)];while(any(l(i),is.na(l(i))))i=i+1;i


Try it online!

# R, 152146144 130 bytes

(This is only if we have to be wrong like your test cases)

i=1;x=scan();x=table(x[x>0]);l=function(t)x>table(strsplit(paste0(t^2,t^3),"")[])[names(x)];while(any(l(i),is.na(l(i))))i=i+1;i


Try it online!

# Ruby, 67 bytes

Uses Arnauld's regex. Takes a list of digits. If that isn't allowed, add 6 bytes to change d.sort to d.chars.sort.

->d,i=0{i+=1until"#{i*i}#{i**3}".chars.sort*''=~/#{d.sort*'.*'}/;i}


Try it online!

# Burlesque, 28 bytes

r0{{2 3}?^im}]mj{j\\z?}j+]fi


Try it online!

r0       # Range from [0,inf]
{
{2 3}?^ # {squared, cubed}
im      # Concatenate
}]m      # Map over each and parse to string
j        # Swap stack
{
j       # Swap
\\      # List difference
z?      # Is null
}
j+]      # Prepend input to make {input j \\ z?}
fi       # Find index s.t.


# Perl 5-pF, 70 bytes

$p=join'.*',sort@F;1while(join'',sort((++$\**2 .$\**3)=~/./g))!~/$p/}{


Try it online!

# Charcoal, 27 bytes

≔⁰ηＷΦχ›№θＩκ№⁺ＩＸη³×ηηＩκ≦⊕ηＩη


Try it online! Link is to verbose version of code. Explanation:

≔⁰η


Start at zero.

ＷΦχ


Repeat until none of the 10 digits satisfies...

›№θＩκ№⁺ＩＸη³×ηηＩκ


... the count of that digit in the input is greater than the count in the cube and the square...

≦⊕η


... increment the result.

Ｉη


Output the result.

import Data.List
f l=[n|n<-[0..],[]==l\\(show=<<[n^2,n^3])]!!0


Try it online!

Even with the import, Haskell's list difference function \\ is quite powerful.

# C (gcc), 93 92 bytes

-1 Byte thanks to ceilingcat

D;I;C(N){N=N?C(N/10)+(N%10==D):0;}F(N){for(D=I=0;D<10;C(N)>C(I*I)+C(I*I*I)?D=!++I:++D);I=I;}


Try it online!

ungolfed:

int D;
int I;

int CNT(N)  // count occurrences of digit D in number N
{
if (N)
return CNT(N / 10) + (N % 10 == D);
return 0;
}

int F(N)
{
for (D = I = 0; D < 10; ++I)
{
for (D = 0; D < 10; ++D)
if (CNT(N) > CNT(I * I) + CNT(I * I * I))
break;
}

return I-1;
}

• @ceilingcat that's clever thanks
– xibu
Feb 28, 2020 at 23:00
• Why do you always have to outgolf me? And how do you do it? Mar 1, 2020 at 2:28

# C (gcc), 134 bytes

s,c,i,m;f(n){for(c=s=i=m=1;m;c=(s=++i*i)*i){int a={};for(;c;s/=10)a[c%10]++,s&&a[s%10]++,c/=10;for(m=n;m*a[m%10]--;m/=10);}n=--i;}


Try it online!

Ungolfed and with better variable names:

int square, cube, result, input_copy;

int f(int input)
{
for(cube = square = result = input_copy = 1;
input_copy;
cube = (square = ++result * result) * result)
{
int digits = { };
for(; cube; square /= 10)
{
digits[cube % 10]++;

if(square)
digits[square % 10]++;

cube /= 10;
}
for(input_copy = input;
input_copy && digits[input_copy % 10]--;
input_copy /= 10);
}
return result-1;
}