16
\$\begingroup\$

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.
\$\endgroup\$
6
  • 1
    \$\begingroup\$ I went "Huh?" when I first saw it, but I think I understand. \$\endgroup\$
    – S.S. Anne
    Commented Feb 27, 2020 at 14:16
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – Fatalize
    Commented Feb 27, 2020 at 14:22
  • \$\begingroup\$ Will the input always be sorted from smallest to largest? \$\endgroup\$
    – Xcali
    Commented Feb 27, 2020 at 20:01
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 27, 2020 at 20:43
  • \$\begingroup\$ @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. \$\endgroup\$
    – S.S. Anne
    Commented Feb 27, 2020 at 21:38

15 Answers 15

5
\$\begingroup\$

05AB1E, 14 12 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
         IÅ?Z        - any of these start with the number 

By the way it's quite slow...

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Yup, looks good now! \$\endgroup\$
    – Grimmy
    Commented Feb 27, 2020 at 15:39
  • \$\begingroup\$ Thanks @Grimmy not used the Å? much and think I got my a and b mixed up \$\endgroup\$ Commented Feb 27, 2020 at 15:42
3
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ Ah, very nice use of f-string. Just posted to see you're a few steps ahead! T_T \$\endgroup\$
    – Noodle9
    Commented Feb 27, 2020 at 14:35
2
\$\begingroup\$

Japt , 17 16 15 14 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
\$\endgroup\$
2
\$\begingroup\$

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!

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

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
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 89 bytes

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

Try it online!

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

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
\$\endgroup\$
0
1
\$\begingroup\$

R, 142 134 121 bytes

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

Try it online!

R, 152 146 144 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),"")[[1]])[names(x)];while(any(l(i),is.na(l(i))))i=i+1;i

Try it online!

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

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!

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

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.
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -pF, 70 bytes

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

Try it online!

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

Charcoal, 27 bytes

≔⁰ηWΦχ›№θIκ№⁺IXη³×ηηIκ≦⊕ηIη

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

≔⁰η

Start at zero.

WΦχ

Repeat until none of the 10 digits satisfies...

›№θIκ№⁺IXη³×ηηIκ

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

≦⊕η

... increment the result.

Iη

Output the result.

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

Haskell, 62 bytes

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.

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

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;
}
\$\endgroup\$
2
  • \$\begingroup\$ @ceilingcat that's clever thanks \$\endgroup\$
    – xibu
    Commented Feb 28, 2020 at 23:00
  • \$\begingroup\$ Why do you always have to outgolf me? And how do you do it? \$\endgroup\$
    – S.S. Anne
    Commented Mar 1, 2020 at 2:28
0
\$\begingroup\$

C (gcc), 134 bytes

s,c,i,m;f(n){for(c=s=i=m=1;m;c=(s=++i*i)*i){int a[10]={};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[10] = { };
        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;
}
\$\endgroup\$
0

Your Answer

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