# CUDDLE calculation

According to the Wikipedia page on the number 69, it is of note that $$\69^2 = 4761\$$ and $$\69^3 = 328509\$$ together use all decimal digits. The number 69 is in fact the lowest number that satisfies this property.

For a similar reason, $$\32043\$$ is remarkable: $$\32043^2 = 1026753849\$$ uses all decimal digits.

If we're going to keep talking about numbers that are interesting this way, we'll need some notation.

For most integers $$\n\$$, the powers $$\n^2, ..., n^k\$$ will use all ten decimal digits (not counting leading zeroes) at least once for sufficiently large values of $$\k\$$ . If it exists, we'll call the lowest such $$\k\$$ the CUDDLE (CUmulative Decimal Digits, Least Exponent) of $$\n\$$.

Write a program or a function that accepts a single non-negative integer $$\n\$$ as input and calculates and returns its CUDDLE.

If $$\n\$$ does not have a CUDDLE, you may return anything but a positive integer, including an error or an empty string, as long as your code halts eventually.

### Test cases

Left column is input, right column is output.

0
1
2          15
3          10
4          10
5          11
6          12
7           7
8           5
9           6
10
11          7
12          6
13          6
14          7
15          9
16          5
17          7
18          4
19          5
20         15
26          8
60         12
69          3
128         3
150         9
200        15
32043       2
1234567890  3


• Your code must work for all inputs up to $$\255\$$.

Note that this involves dealing with pretty large numbers. $$\20^{15}\$$ is already larger than $$\2^{64}\$$.

• If you print the result, it may be followed by a linefeed.

• Standard rules apply.

• I was wondering how we went from CUDDLE to 69, and I find it a bit disturbing that it has to do with power ;) Oct 10, 2015 at 1:01
• If the number has no CUDDLE, is it okay if the program halts... eventually? (i.e. when the integer counter overflows) Oct 10, 2015 at 1:07
• Also, for the first additional rule: Does this mean that if your language's integer type can store numbers > 255, your code must work for them? Oct 10, 2015 at 1:10
• @Doorknob Eventually is fine, as long as it actually halts. I've put an upper limit of 255 on the input. Still involves some pretty big numbers in the calculations though... Oct 10, 2015 at 1:20
• I added the test case 26->8 because it's the smallest example where including n^1 gives the wrong answer (of 6), an error I had made in my code.
– xnor
Oct 10, 2015 at 2:02

## Python 2, 56

f=lambda n,i=2,s='L':len(set(s))>10or-~f(n,i+1,s+n**i)


A recursive solution. Counts up exponents i starting from 2 and accumulates the digits of powers n**i into the string s. When s has all ten digits, returns True, which equals 1, and otherwise recurses and adds 1. This turned out shorter than returning i.

Calling the function on a number without a CUDDLE terminates with Internal error: RangeError: Maximum call stack size exceeded. Numbers up to 255 that do output never need more than 15 iterations.

Because of Python 2's annoying habit of appending an L to large numbers, we actually initialize the digit string to L and check if the set size is at least 11. Python 3 saves 2 chars by not needing this, but loses 3 chars on using str over backticks. Python 3.5 saves 2 more chars with set unpacking, saving a char over Python 2 in total:

f=lambda n,i=2,s='':len({*s})>9or-~f(n,i+1,s+str(n**i))


# Pyth, 16 bytes

hf<9l{=+k^QTtS15


Try it online: Demonstration or Test Suite

Like other solutions, I use 15 as an upper limit. I believe that this is also the maximal CUDDLE. I tested all numbers up to 10.000.000, and there's no number with a CUDDLE greater than 15.

Numbers with a CUDDLE >= 10 are already quite rare. The only numbers with a CUDDLE of 15 are the numbers 2*10^k. There are no numbers with a CUDDLE of 14 or 13, the CUDDLE 12 only appears for the numbers 6*10^k, the CUDDLE 11 only for 5*10^k.

So I think this code works perfectly for any natural number.

Prints an error message, if there is no solution.

### Explanation:

hf<9l{=+k^QTtS15   implicit: Q = input number
k = empty string
tS15   the list [2, 3, 4, ..., 15]
f                 filter this list for elements T, which satisfy:
^QT          compute Q^T
+k             k + ^ (converts to string implicitly)
= k             save the result in k
l{  k             length of set of k (number of different chars)
<9                  test if 9 is smaller than ^
h                  print the first number in the filtered list
(throws error if empty)


## Ruby, 67 65 chars

->n{s='';[*2..99].index{|i|(s+="#{n**i}").chars.uniq.size==10}+2}


Works near-instantaneously for all the test cases, even the ones > 255.

Errors for numbers with no CUDDLE.

Explanation:

-> n {                         # define function with short lambda syntax
s = ''                       # the string we are storing the numbers in
[*2..99]                     # for all numbers from 2 to 99...
.index {|i|                # find index of the number i for which...
(s+="#{n**i}")           # after appending pow(n,i) to s...
.chars.uniq.size==10}  # num of uniq chars in s is 10 (each digit)
+ 2                          # add 2, because our index starts from 2
}


# CJam, 28 bytes

LliG,2>f#{s+_&_}%:,A#)_)s\g*


Try it online

This relies on the fact that the CUDDLE (if it exists) is never larger than 15 for the input range, as first observed by @xnor.

There's probably a better way of producing the output for the case where there's no solution. I'll update if I think of anything.

Explanation:

L     Push empty string, will be used for accumulating digits.
li    Get input and convert to integer.
G,    Build list of exponents [0 .. 15].
2>    Slice off first two values, to produce [2 .. 15].
f#    Apply power operator with all exponents to input.
{     Start loop over powers.
s     Convert to string. We care about the digits here.
+     Concatenate with previously found digits.
_&    Uniquify using set intersection of digit list with itself.
_     Copy for continued accumulation in next loop iteration.
}%    End of loop over powers. We'll have an extra copy of the last value here,
but it does no harm so we just keep it.
:,    Apply length operator to accumulated digit lists.
A#    Find 10 in the list. The search result will correspond to the first power
that resulted in 10 different accumulated digits. If not found, the result
will be -1. Note that 0 corresponds to power 2, since that was the first
power we used. So we need to add 2 to get the result, and check for -1.
)     Increment value. 0 now corresponds to no solution.
_     Copy this value. Will be used as multiplier to create empty string if 0.
)     Increment again, to get the +2 needed for the result.
s     Convert to string.
\     Swap once-incremented value to top, which is 0 for no solution, non-zero
otherwise.
g     Signum to get 0/1 for no solution vs. solution.
*     Multiply with result string, to get empty string for no solution.


## Mathematica, 103 bytes

f=(d=DigitCount;x=1;y=d[0];For[n=0,!IntegerQ@Log10[#]&&MemberQ[y,0],++n,x*=#;y+=d[x]];Print[#,"\t",n])&


It appears that only powers of 10 would not eventually have CUDDLEs, so they are skipped. Function keeps a list of seen digits and stops when there are no longer zeros in it.

Output:

1    0
2    15
3    10
4    10
5    11
6    12
7    7
8    5
9    6
10    0
11    7
12    6
13    6

• Fun fact: As long as log_10(n) is irrational, given any positive integer k there exists m such that the decimal representation of n^m starts with k. Which means that skipping the powers of 10 (and 0) is fine :) Oct 10, 2015 at 2:41

# JavaScript (ES6) 188

Not bad for a language that is limited to 53 bits integers.

Test running the snippet below in a browser that implements EcmaScripts 6, including arrow functions and spread operator (AFAIK Firefox)

f=n=>{for(p=1,d=[],v=n=[...n+''].reverse();++p<20;){v.map((a,i)=>n.map((b,j)=>r[j+=i]=a*b+~~r[j]),r=[],c=0),r=r.map(r=>(r+=c,c=r/10|0,d[r%=10]=r));v=c?[...r,c]:r;if(d.join[9])return p;}}

// Less golfed
U=n=>{
// Arbitrary precision multiplication
M=(A,B,R=[],c=0)=>
(
A.map((a,i)=>B.map((b,j)=>R[j+=i]=a*b+~~R[j])),
R=R.map(r=>(r+=c,c=r/10|0,r%10)),
c?[...R,c]:R
);
v=n=[...n+''].reverse();
for(p=1,d=[];++p<20;)
{
v=M(n,v)

v.map(c=>d[c]=c)
if (d.join[9])return p
}
}

// TEST
for(i=o='';++i<300;)o+=i+' : '+f(i)+'\n'
O.innerHTML=o


<pre id=O></pre>

### PowerShell, 94 bytes

param($n,$s='')
2..99|%{$s+=[bigint]::Pow($n,$_);if(($s-split''|sort -U).Count-eq11){$_;break}} (As a single line)  Nothing too clever about it, but piping to sort -U[nique] is a neat way to ape Python's set() functionality for this kind of use, without explicitly adding items to a hashtable. param($n,$s='') # Take command line parameter. 2..99 |%{ # Loop from 2 to 99, inclusive.$s+=[bigint]::Pow($n,$_)                 # $n^Loopvar, concatenate to string. if (($s-split''|sort -U).Count-eq11) {   # Convert to unique-characters-array; count.
$_;break # Print current loopvar and quit. } } # Otherwise, finish (silently).  e.g. PS C:\> .\CUDDLE-of-n.ps1 10 PS C:\> .\CUDDLE-of-n.ps1 12 6 PS C:\> .\CUDDLE-of-n.ps1 255 5  # gawk 4, 73 + 5 for flags = 78 bytes {for(n=$0;a-1023&&++i<15;j=0)for($0*=n;j++<NF;)a=or(a,2^$j)}$0=i<15?++i:_  For every digit 0 bis 9 it encounters in the powers of the input, it sets the bit representing 2^digit in a, until the first 10 digits are found (a == 1023 == 2^10-1) or there's been more than 15 iterations. Has to be called with an empty field seperator and the -M flag for big numbers. echo 17 | awk -M '{for(n=$0;a-1023&&++i<15;j=0)for($0*=n;j++<NF;)a=or(a,2^$j)}\$0=i<15?++i:_' FS=


Fiddling around with this I found the following sequences for the different CUDDLEs:

2: 32043 32286 33144 35172 35337 35757 35853 37176 37905 38772 39147 39336 40545 42744 43902 44016 45567 45624 46587 48852 49314 49353 50706 53976 54918 55446 55524 55581 55626 56532 57321 58413 58455 58554 59403 60984 61575 61866 62679 62961 63051 63129 65634 65637 66105 66276 67677 68763 68781 69513 71433 72621 75759 76047 76182 77346 78072 78453 80361 80445 81222 81945 83919 84648 85353 85743 85803 86073 87639 88623 89079 89145 89355 89523 90144 90153 90198 91248 91605 92214 94695 95154 96702 97779 98055 98802 99066
3: 69 128 203 302 327 366 398 467 542 591 593 598 633 643 669 690 747 759 903 923 943 1016 1018 1027 1028 1043 1086 1112 1182 1194 1199 1233 1278 1280 1282 1328 1336 1364 1396 1419 1459 1463 1467 1472 1475 1484 1499 1508 1509 1519 1563 1569 1599 1602 1603 1618 1631 1633 1634 1659 1669 1687 1701 1712 1721 1737 1746 1767 1774 1778 1780 1791 1804 1837 1844 1869 1889 1895 1899 1903 1919 1921 1936 1956 1958 1960 1962 1973 1984 1985 1991 1994 1996 2003 2017 2019 2030 2033 2053 2075 2123 2126 2134 2157 2158 2159 2168 2175 2183
4: 18 54 59 67 71 84 93 95 97 108 112 115 132 139 144 147 148 152 156 157 159 169 172 174 178 179 180 181 182 184 195 196 213 214 215 216 221 223 227 228 232 234 235 239 241 242 248 265 266 267 270 272 273 279 281 285 287 294 298 299 306 311 312 314 315 316 323 326 329 332 336 338 342 343 353 354 356 361 362 364 365 368 369 379 388 391 393 395 396 397 403 412 413 414 416 419 423 426 431 434 439 442 443 444 448 451 452 453 454 455 457 459 463 466 469 472 473 477 479 482 484 486 489 493 494 496 503 507 508 509 515 517 523
5: 8 16 19 27 28 38 44 47 55 57 61 77 79 80 82 83 86 87 91 92 103 106 113 116 117 118 121 123 125 126 129 131 133 136 138 140 141 142 143 145 146 151 154 158 160 161 165 167 173 175 176 177 183 185 186 187 189 190 191 192 193 197 198 204 207 218 224 226 229 230 231 236 240 243 246 249 253 255 257 258 259 261 263 268 269 271 275 276 277 278 280 282 283 284 286 288 289 292 293 304 309 322 328 331 339 341 344 345 346 347 348 349 352 357 359 367 371 372 373 374 375 377 380 381 384 387 389 402 407 408 409 411 417 418 422 427
6: 9 12 13 22 23 24 33 36 37 39 42 43 45 46 49 51 53 58 62 66 72 73 75 78 81 88 90 94 98 105 107 109 114 119 120 122 127 130 134 137 149 153 155 162 163 164 166 168 170 194 199 206 211 212 217 219 220 222 225 233 237 238 244 247 252 254 256 262 264 274 291 295 296 301 308 317 319 321 324 325 330 333 334 337 351 355 358 360 370 376 378 382 383 385 386 390 394 399 401 404 405 406 415 420 421 424 425 429 430 433 435 438 446 450 460 471 476 478 488 490 498 502 504 506 510 513 514 519 530 539 548 556 578 620 628 631 634 636
7: 7 11 14 17 29 31 32 35 41 48 52 56 63 64 70 74 85 89 96 99 102 104 110 111 135 171 188 201 202 205 208 245 251 290 297 303 305 307 310 313 318 320 335 350 363 392 410 465 475 480 483 485 501 511 518 520 521 560 582 584 595 601 630 640 682 700 736 740 786 798 850 890 952 956 965 975 982 990 999 1002 1005 1011 1020 1040 1054 1100 1110 1171 1219 1313 1331 1350 1379 1414 1447 1468 1601 1707 1710 1735 1748 2001 2010 2020 2050 2080 2450 2510 2534 2641 2745 2900 2914 2955 2970 3030 3050 3070 3100 3130 3136 3180 3193 3200
8: 21 25 26 30 34 65 76 124 209 210 250 260 300 340 505 650 1004 1240 2002 2090 2100 2500 2600 2975 3000 3400 3944 4376 5050 6500 6885 7399 10040 12400 15483 20002 20020 20900 21000 25000 26000 29750 30000 34000 43760 50500 65000 68850 73990
9: 15 68 101 150 1001 1010 1500 10001 10010 10100 15000
10: 3 4 40 400 4000 40000
11: 5 50 500 5000 50000
12: 6 60 600 6000 60000
15: 2 20 200 2000 20000


# Jelly, 13 bytes

*Ḋ}DFQL
çⱮ⁴i⁵


Try it online!

Assumes that the CUDDLE of a number is never greater than 16. This is a 14 byte version that only assumes the CUDDLE exists (runs forever for $$\n = 0, 1, 10\$$)

## How it works

*Ḋ}DFQL - Helper link. Takes n on the left and k on the right
}     - To k:
Ḋ      -   Dequeue; Yield [2, 3, ..., k]
*       - Raise each to the power n
D    - Convert to digits
F   - Flatten
Q  - Deduplicate
L - Length

çⱮ⁴i⁵ - Main link. Takes n on the left
⁴   - 16
Ɱ    - Over each k in [1, 2, ..., 15, 16]:
ç     -   Run the helper link with n on the left and k on the right
⁵ - 10
i  - First index of 10 or yield 0 if not present


# 05AB1E, 12 bytes

₂L.ΔL¦mS9ÝåP


Outputs -1 if $$\n\$$ does not have a CUDDLE.

Explanation:

₂L            # Push a list in the range [1,26]
# (note: 26 is the smallest single-byte builtin integer above 15)
.Δ          # Find the first integer k in this list which is truthy for:
# (results in -1 if none were truthy)
L         #  Push a list in the range [1,k]
¦        #  Remove the first item to make the range [2,k]
m       #  Take the (implicit) input-integer to the power of each of these
S      #  Convert the list of integers to a flattened list of digits
9Ý    #  Push a list in the range [0,9]
å   #  Check for each digit it it's in the flattened list of digits
P  #  And check if this is truthy for all of them
# (after which the result is output implicitly)


The S9ÝåP could also have been JÙgTQ for the same byte-count:

       J      #  Join the list of integers together to a single string
Ù     #  Uniquify its characters/digits
g    #  Pop and push its length
TQ  #  And check that this length is equal to 10