Task:
Given an integer number in decimal number system, reduce it to a single decimal digit as follows:
- Convert the number to a list of decimal digits.
- Find the largest digit, D
- Remove D from the list. If there is more than one occurrence of D, choose the first from the left (at the most significant position), all others should remain intact.
- Convert the resulting list to a decimal number and multiply it by D.
- If the number is bigger than 9 (has more than 1 decimal digit), repeat the whole procedure, feeding the result into it. Stop when you get a single-digit result.
- Display the result.
Example:
26364 ->
1. 2 6 3 6 4
2. The largest digit is 6, so D=6
3. There are two occurrences or 6: at positions 1 and 3 (0-based). We remove the left one,
at position 1 and get the list 2 3 6 4
4. we convert the list 2 3 6 4 to 2364 and multiply it by D:
2364 * 6 = 14184
5. 14184 is greater than 9 so we repeat the procedure, feeding 14184 into it.
We continue by repeating the procedure for 14184 and so on and we go through the following intermediate results, finally reaching 8:
11312
3336
1998
1782
1376
952
468
368
288
224
88
64
24
8
So the result for 26364 is 8.
Input: An integer / a string representing an integer
Output: A single digit, the result of the reduction applied to the number.
Test cases:
9 -> 9
27 -> 4
757 -> 5
1234 -> 8
26364 -> 8
432969 -> 0
1234584 -> 8
91273716 -> 6
This is code-golf, so the shortest answers in bytes in each language win.
34 Answers 34
05AB1E, 6 bytes
Code:
[Dg#à*
Uses the 05AB1E encoding. Try it online!
Explanation
[Dg# # While the length of the number is not 1
à # Extract the largest element from the current number
* # Multiply it with the leftover number
JavaScript (ES6), 49 bytes
f=n=>n>9?f(""+n.replace(m=Math.max(...n),"")*m):n
Takes input as a string representation of an integer, like f("26364").
Test Cases
f=n=>n>9?f(""+n.replace(m=Math.max(...n),"")*m):n
;["9", "27", "757", "1234", "26364", "432969", "1234584", "91273716"]
.forEach(num => console.log(num + " -> " + f(num)))
.as-console-wrapper{max-height:100%!important}
Pyth, 16 bytes
.WtH`*s.-ZKeSZsK
Takes input as a String. Try it here! (Alternative: .WtH`*s.-ZeSZseS)
Pyth, 18 bytes
.WgHT*s.-`ZKeS`ZsK
Takes input as an integer. Try it here!
How it works
16-byter
.WtH`*s.-ZKeSZsK ~ Full program. .W ~ Functional while. While A(value) is truthy, value = B(value). ~ The final value is returned. tH ~ A, condition: Is value[1:] truthy? Is the length ≥ 2? `*s.-ZKeSZsK ~ B, setter. .- ~ Bagwise subtraction, used for removing the highest digit, with... Z ~ The current value Z, and... KeSZ ~ The highest digit of Z (as a String). Also assigns to a variable K. s ~ Casted to an integer. * ~ Multiplied by... sK ~ The highest digit. ` ~ Convert to a String.
18-byter
.WgHT*s.-`ZKeS`ZsK ~ Full program. .W ~ Functional while. While A(value) is truthy, value = B(value). ~ The final value is returned. gHT ~ A, condition: is value (H) ≥ 10? *s.-`ZKeS`ZsK ~ B, setter. .- ~ Bagwise substraction (used for removing first occurrence). `Z ~ The string representation of Z. KeS`Z ~ And the highest (lexicographically) character of Z (highest digit). It also assigns it to a variable called K. s ~ Cast to integer. * ~ Multiply by... sK ~ K casted to int.
Being that close to Jelly at such type of challenge is very good for Pyth IMO :-)
Husk, (削除) 14 (削除ここまで) (削除) 13 (削除ここまで) 12 bytes
Thanks Zgarb for saving 1 byte.
Ω≤9oṠS*od-▲さんかくd
Explanation:
Ω≤9 Repeat the following function until the result is ≤ 9
d Convert to a list of digits
-▲さんかく Remove the largest one
od Convert back to an integer
oṠS* Multiply by the maximum digit
-
-
\$\begingroup\$ @Zgarb Thanks, I was looking for something like that. \$\endgroup\$H.PWiz– H.PWiz2017年11月18日 21:33:33 +00:00Commented Nov 18, 2017 at 21:33
R, (削除) 99 (削除ここまで) 95 bytes
f=function(x)"if"(n<-nchar(x)-1,f(10^(n:1-1)%*%(d=x%/%10^(n:0)%%10)[-(M=which.max(d))]*d[M]),x)
A recursive function. Adding f(number) in the footer can be used to test for other values of number. Straightforward implementation, d is the list of digits, and 10^(n:2-2)%*%d[-M] computes the number with the largest digit removed.
Ruby, 59 bytes
f=->n{m=n.digits.max;n>9?f[n.to_s.sub(m.to_s,"").to_i*m]:n}
Recursive lambda function called like f[26364].
Python 2, 72 bytes
f=lambda n:`n`*(n<=9)or f(int(`n`.replace(max(`n`),'',1))*int(max(`n`)))
-
1\$\begingroup\$ ...I was debugging a dumb mistake in this. Darn, I got ninja'd. \$\endgroup\$totallyhuman– totallyhuman2017年11月18日 20:21:59 +00:00Commented Nov 18, 2017 at 20:21
-
\$\begingroup\$ I'm getting an error on input 9 \$\endgroup\$RoryT– RoryT2017年11月19日 07:27:55 +00:00Commented Nov 19, 2017 at 7:27
-
\$\begingroup\$ This seems to fail for the test case
432969. "ValueError: invalid literal for int() with base 10: ''" \$\endgroup\$James Webster– James Webster2017年11月20日 13:25:05 +00:00Commented Nov 20, 2017 at 13:25 -
\$\begingroup\$ @JamesWebster should be fixed now. \$\endgroup\$FlipTack– FlipTack2017年11月20日 18:15:25 +00:00Commented Nov 20, 2017 at 18:15
-
1\$\begingroup\$ @recursive No, as then if
nwas 0 thenn*(n<=9)would still evaluate to a falsy value, 0, making the recursion continue and causing an error, whereas the string'0'is a truthy value and therefore the recursion is halted. \$\endgroup\$FlipTack– FlipTack2017年11月21日 22:16:43 +00:00Commented Nov 21, 2017 at 22:16
Jelly, 15 bytes
×ばつṀ$œṡṀ$FḌμ>9μ¿
Try it online! or see the test-suite.
How?
×ばつṀ$œṡṀ$FḌμ>9μ¿ - Link: number, n
¿ - while:
μ - ...condition (monadic):
9 - literal 9
> - loop value greater than (9)?
μ - ...do (monadic): e.g. 432969
D - convert to a decimal list [4,3,2,9,6,9]
$ - last two links as a monad:
Ṁ - maximum 9
×ばつ - multiply (vectorises) [36,27,18,81,54,81]
$ - last two links as a monad:
Ṁ - maximum 81
œṡ - split at first occurrence [[36,27,18],[54,81]]
F - flatten [36,27,18,54,81]
Ḍ - convert from base 10 389421 (i.e. 360000 + 27000 + 1800 + 540 + 81)
Haskell, (削除) 70 (削除ここまで) (削除) 67 (削除ここまで) 66 bytes
Saved (削除) 3 (削除ここまで) 4 bytes thanks to nimi!
f x|x<10=x|(a,b:c)<-span=<<(>).maximum$show x=f$read[b]*read(a++c)
-
\$\begingroup\$ An explicit recursion is shorter than
until. Try it online!. \$\endgroup\$nimi– nimi2017年11月18日 21:52:58 +00:00Commented Nov 18, 2017 at 21:52 -
\$\begingroup\$ one more byte to save:
...span=<<(>).maximum.... \$\endgroup\$nimi– nimi2017年11月18日 22:06:53 +00:00Commented Nov 18, 2017 at 22:06 -
\$\begingroup\$ Ooh ... Nice :) \$\endgroup\$H.PWiz– H.PWiz2017年11月18日 22:10:14 +00:00Commented Nov 18, 2017 at 22:10
C# (.NET Core), 126 bytes
int F(int n){var x=(n+"").ToList();var m=x.Max();x.RemoveAt(x.IndexOf(m));return n>9?F(int.Parse(string.Concat(x))*(m-48)):n;}
-
\$\begingroup\$ Welcome to PPCG! You can remove a space. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年11月20日 11:25:10 +00:00Commented Nov 20, 2017 at 11:25
-
\$\begingroup\$ @EriktheOutgolfer Thank you, missed that one. \$\endgroup\$Timmeh– Timmeh2017年11月20日 11:51:49 +00:00Commented Nov 20, 2017 at 11:51
-
1\$\begingroup\$ @totallyhuman Thank you, down to 137 after some refactoring. \$\endgroup\$Timmeh– Timmeh2017年11月20日 12:13:16 +00:00Commented Nov 20, 2017 at 12:13
-
\$\begingroup\$ You can change
if(n<10)return n;...return F(...);to a single return with ternary-if, like this:int F(int n){var x=(n+"").ToList();var m=x.Max(d=>d);x.RemoveAt(x.IndexOf(m));return n<10?n:F(int.Parse(string.Concat(x))*(m-48));}(131 bytes) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年11月20日 13:38:27 +00:00Commented Nov 20, 2017 at 13:38 -
\$\begingroup\$ I think you need to include
using System.Linq;(18 bytes) into the bytecount. \$\endgroup\$Ian H.– Ian H.2017年11月21日 17:37:10 +00:00Commented Nov 21, 2017 at 17:37
APL (Dyalog), (削除) 36 (削除ここまで) (削除) 35 (削除ここまで) 33 bytes
-1 due to updated OP specs. -2 thanks to ngn.
Anonymous tacit prefix function. Takes integer as argument.
{⍵>9:∇(×ばつ10⊥⊂⌷⍨ ̈⍳∘≢~⊢⍳⌈/)⍎ ̈⍕⍵⋄⍵}
{...}a function where ⍵ is the argument:
⍵>9: if the argument is greater than 9, then:
⍕⍵ format (stringify) the argument
⍎ ̈ execute (evaluate) each (this gets us the digits as numbers)
(...) apply the following tacit function on those
⌈/ the largest digit
×ばつ times
10⊥ the base-10 decoding of (collects digits)
⊂ all the digits
⌷⍨ ̈ indexed by each of
⍳∘≢ the indices of the number of digits
≠ differs from
⊢⍳⌈/ the largest digit's index in the entire list of digits
∇ recurse (i.e. call self) on that
⋄ else
⍵ return the argument unmodified
-
\$\begingroup\$ Shouldn't
>10be>9? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年11月18日 21:49:12 +00:00Commented Nov 18, 2017 at 21:49 -
\$\begingroup\$ @EriktheOutgolfer Probably, but OP is actually unclear (self-contradictory) about that. \$\endgroup\$Adám– Adám2017年11月18日 21:59:32 +00:00Commented Nov 18, 2017 at 21:59
-
\$\begingroup\$ That's true, but
>9would save a byte. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年11月18日 22:10:40 +00:00Commented Nov 18, 2017 at 22:10 -
\$\begingroup\$ @EriktheOutgolfer Updated. \$\endgroup\$Adám– Adám2017年11月19日 09:33:56 +00:00Commented Nov 19, 2017 at 9:33
-
\$\begingroup\$ @Adám ∇ instead of ⍣= for -1 byte: {⍵>9:∇(⌈/ ... ⋄⍵} \$\endgroup\$ngn– ngn2017年11月20日 18:26:12 +00:00Commented Nov 20, 2017 at 18:26
Perl 6, (削除) 45 (削除ここまで) 41 bytes
{($_,{$/=.comb.max;S/"$/"//*$/}...10>*).tail}
{($_,{S/"{.comb.max}"//*$/}...10>*).tail}
Expanded:
{ # bare block lambda with implicit parameter 「$_」
( # generate the sequence
$_, # start the sequence with the input
{ # generate the rest of the values in the sequence
S/ # find and replace (not in-place)
"{ .comb.max }" # find the max digit and match against it
// # replace it with nothing
* # multiply the result with
$/ # the digit that was removed
}
... # keep generating values until
10 > * # the value is less than 10
).tail # get the last value from the sequence
}
Retina, 67 bytes
{1`(..+)?
1$&;$&
O`\G\d
.+((.);.*?)2円
1ドル
\d+
$*
1(?=.*;(1+))|.
1ドル
1
Try it online! Link includes the test cases fast enough not to hammer Dennis's server. Explanation:
{1`(..+)?
1$&;$&
For two digit numbers, this duplicates the number with a ; separator, prefixing a 1 to the duplicate. For one digit numbers, this prefixes 1; to the number.
O`\G\d
Sort the digits of the duplicate. (For one digit numbers, this has no effect.)
.+((.);.*?)2円
1ドル
Find the first occurrence of the largest digit, and delete it, and also the other digits in the duplicate, and the extra 1 that was added earlier. (For one digit numbers, the match fails so this does nothing.)
\d+
$*
1(?=.*;(1+))|.
1ドル
1
Multiply the number by the digit. For one digit numbers, this results in the original number, and the loop terminates. Otherwise, the program loops until a single digit is reached.
PowerShell, 123 bytes
[Collections.ArrayList]$a=[char[]]"$args"
while(9-lt-join$a){$a.remove(($b=($a|sort)[-1]));$a=[char[]]"$(+"$b"*-join$a)"}$a
Ooof. PowerShell arrays are immutable, so we need to use the lengthy [Collections.ArrayList] casting here so we can call .remove() later.
Takes input $args, converts it to a string, then a char-array, then an ArrayList. Stores that into $a. Then we while loop until we're at or below 9. Each iteration, we're calling .remove on the largest element of $a (done by sort and taking the last element [-1]), storing the largest element into $b at the same time. This happens to work because the ASCII values sort in the same fashion as the literal digits.
Next, we recompute $a, again as an char-array (and ArrayList implicitly), by casting our $b (which is currently a char) to a string, then an int with +, and multiplying that to $a -joined into a string (implicitly cast to int). This satisfies the "multiply by D" portion of the challenge.
Finally, once we're out of the loop, we put $a onto the pipeline and output is implicit.
C# (.NET Core), (削除) 177 (削除ここまで) 164 + 18 bytes
Saved 13 bytes thanks to @raznagul!
int f(int n){string s=n+"",m=s.Max(d=>d)+"";if(n<10)return n;var x=s.ToList();x.RemoveAt(s.IndexOf(m));int y=int.Parse(string.Join("",x))*int.Parse(m);return f(y);}
-
\$\begingroup\$ You can change
s.Length<2ton<10. Also, you can remove the ternary operator and justreturn f(y)at the end, as the case is handled by theifin the next step of the recursion. \$\endgroup\$raznagul– raznagul2017年11月21日 13:04:58 +00:00Commented Nov 21, 2017 at 13:04
Java 8, (削除) 126 (削除ここまで) 104 bytes
n->{for(;n>9;n=new Long((n+"").replaceFirst((n=(n+"").chars().max().getAsInt()-48)+"",""))*n);return n;}
-22 bytes thanks to @OlivierGrégoire.
Explanation:
n->{ // Method with long as both parameter and return-type
for(;n>9; // Loop as long as the number contains more than 1 digit
n= // Replace the current number with:
new Long((n+"").replaceFirst((n=(n+"").chars().max().getAsInt()-48)+"",""))
// Remove the first largest digit from the number,
*n // and multiply this new number with the removed digit
); // End of loop
return n; // Return the result
} // End of method
-
\$\begingroup\$ 111 bytes \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年11月22日 14:10:19 +00:00Commented Nov 22, 2017 at 14:10
-
1\$\begingroup\$ 104 bytes (same as above, but iterative instead of recursive, also:
n>9and revert conditions instead ofn<10). \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年11月22日 14:14:22 +00:00Commented Nov 22, 2017 at 14:14
C (削除) 103 (削除ここまで) , (削除) 95 (削除ここまで) , 90 bytes
a,b;t,m;f(n){for(t=m=0,a=b=1e9;a/=10;)if((t=n/a%10)>m)m=t,b=a;n=n>9?f(m*=n/b/10*b+n%b):n;}
Java 8: 115 bytes
-10 bytes thanks to Jo King
Unfortunately you can't call a lambda function recursively, so an extra 11 bytes is needed for the method header. I am aware there is a shorter Java answer that loops instead, but I decided to come up with this on my own.
long f(long n){int m=(n+"").chars().max().getAsInt()-48;return n>9?f(new Long((n+"").replaceFirst(""+m,""))*m):n;};
-
\$\begingroup\$ You can move the
-48from the map to the end of themdefinition. Try it online! You also have some extra whitespace in your TIO link \$\endgroup\$Jo King– Jo King2019年03月08日 00:28:57 +00:00Commented Mar 8, 2019 at 0:28 -
\$\begingroup\$ Technically you can have recursive lambda expressions, however, they require that you pass the lambda as a parameter when it is called \$\endgroup\$KrystosTheOverlord– KrystosTheOverlord2020年10月13日 18:17:13 +00:00Commented Oct 13, 2020 at 18:17
-
\$\begingroup\$ @KrystosTheOverlord good to know, not sure I can use it here though due to the input rules (only 1 input which is the number). \$\endgroup\$Benjamin Urquhart– Benjamin Urquhart2020年10月17日 23:30:07 +00:00Commented Oct 17, 2020 at 23:30
-
\$\begingroup\$ @BenjaminUrquhart ah my bad, well in the future it could be handy, as it makes recursive java solutions much smaller :) \$\endgroup\$KrystosTheOverlord– KrystosTheOverlord2020年10月19日 17:32:23 +00:00Commented Oct 19, 2020 at 17:32
Jq 1.5, 86 bytes
until(.<10;"\(.)"|(./""|max)as$v|index($v)as$x|.[:$x]+.[1+$x:]|tonumber*($v|tonumber))
Expanded
until(
.<10 # until number is below 10
; "\(.)" # convert to string
| (./""|max) as $v # find largest digit, call it $v
| index($v) as $x # find index of digit
| .[:$x]+.[1+$x:] # remove digit
| tonumber*($v|tonumber) # convert back to number and multiply by $v
)
-
\$\begingroup\$ Fails on input of
9. :( but tinkered a little and managed to get 41 bytes with a fix: Try it online! \$\endgroup\$Dom Hastings– Dom Hastings2017年11月20日 13:57:36 +00:00Commented Nov 20, 2017 at 13:57
Lua, (削除) 137 (削除ここまで) 108 bytes
function f(n)while n>9 do b="0"g=b.gsub g(n,".",function(m)b=math.max(m,b)end)n=b*g(n,b,"",1)end print(n)end
Thanks to Jonathan S for golfing off 29 bytes.
-
1\$\begingroup\$ 108 bytes \$\endgroup\$Jonathan S.– Jonathan S.2017年11月19日 12:28:43 +00:00Commented Nov 19, 2017 at 12:28
-
\$\begingroup\$ Thanks. That looks worthy of an answer of its own - will link to a post you make for it, otherwise will edit & credit. \$\endgroup\$MCAdventure10– MCAdventure102017年11月19日 12:34:43 +00:00Commented Nov 19, 2017 at 12:34
-
\$\begingroup\$ Just edit it in. It's still your code, I haven't written it from scratch. \$\endgroup\$Jonathan S.– Jonathan S.2017年11月19日 12:46:21 +00:00Commented Nov 19, 2017 at 12:46
D, (削除) 188 (削除ここまで) (削除) 186 (削除ここまで) 185 bytes
import std.conv,std.algorithm;T f(T,U=string)(T u){if(u<10)return u;T[]r;u.text.each!(n=>r~=n.to!T-48);T m=r.maxElement;U s;r.remove(r.maxIndex).each!(n=>s~=n.to!U);return f(m*s.to!T);}
I hate lazy evaluation, so much. Any tips are welcome!
Lua, 154 Bytes
I should have some ways to golf this down, I'm experimenting right now.
n=...z=table
while n+0>9 do
t={}T={}n=n..''n:gsub(".",function(c)t[#t+1]=c T[#T+1]=c
end)z.sort(t)x=t[#t]z.remove(T,n:find(x))n=z.concat(T)*x
end
print(n)
Explanations
n=... -- define n as a shorthand for the argument
z=table -- define z as a pointer to the object table
while n+0>9 -- iterate as long as n is greater than 9
do -- n+0 ensure that we're using a number to do the comparison
t={} -- intialise two tables, one is used to find the greatest digit
T={} -- the other one is used to remove it from the string
n=n..'' -- ensure that n is a string (mandatory after the first loop)
n:gsub(".",function(c) -- apply an anonymous function to each character in n
t[#t+1]=c -- fill our tables with the digits
T[#T+1]=c
end)
z.sort(t) -- sort t to put the greatest digit in the last index
x=t[#t] -- intialise x to the value of the greatest digit
z.remove(T,n:find(x)) -- remove the first occurence of x from the table T
-- based on its position in the input string
n=z.concat(T)*x -- assign the new value to n
end -- if it still isn't a single digit, we're looping over again
print(n) -- output the answer
Pip, (削除) 22 (削除ここまで) 21 bytes
Wa>9a:aRAa@?YMXax*:ya
Takes input as a command-line argument. Verify all test cases: Try it online!
Explanation
Ungolfed, with comments:
a is 1st cmdline arg
W a>9 { While a > 9:
Y MXa Yank max(a) into y
a RA: a@?y "" Find index of y in a; replace the character at that position with ""
a *: y Multiply a by y
}
a Autoprint a
In the golfed version, the loop body is condensed into a single expression:
a:aRAa@?YMXax*:y
YMXa Yank max(a)
a@? Find its index in a
aRA x Replace at that index with x (preinitialized to "")
*:y Multiply that result by y (using : meta-operator to lower the precedence)
a: Assign back to a
Scala, 121 bytes
x=>def g(y:Int):Int={if(y>9){val m=y.toString.max.asDigit;g(y.toString.replaceFirst(m.toString,"").toInt*m)}else y}
g(x)}
pure Bash, (削除) 116 (削除ここまで) 112 bytes, or 101, or even 97?
Based on answers from iBug and Léa Gris and tips-for-golfing-in-bash (thanks, @AaronMiller and @BrowncatPrograms!):
#!/bin/bash
test112()
{
n=1ドル;shopt -s extglob;while((n>9));do m=0;for x in ${n//?()/ };{ ((m<x))&&m=$x;};n=$[10#${n/$m/}*m];done;echo $n
}
for k in 9 27 757 1234 26364 432969 1234584 91273716; do printf '%d -> ' "$k"; test112 "$k"; done
Also there are two shorter variants:
#!/bin/bash
f()(shopt -s extglob;((1ドル>9))||exit 1ドル;m=0;for x in ${1//?()/ };{ ((m<x))&&m=$x;};f $[10#${1/$m/}*m])
for k in 9 27 757 1234 26364 432969 1234584 91273716; do printf '%d -> ' "$k"; f "$k"; echo "$?"; done
(Try it online!) with 101 bytes and with 97 bytes (Try it online!):
shopt -s extglob;((1ドル>9))||exit 1ドル;m=0;for x in ${1//?()/ };{ ((m<x))&&m=$x;};0ドル $[10#${1/$m/}*m]
But I am not entirely sure the last two apply to the rules. The latter even uses 0ドル (I neither found a rule allowing that nor denying this) to recurse to itself, so it only works if put into a script (like golf.sh), made executable and then is called like golf.sh 91273716; echo $?
Output of the scripts:
9 -> 9
27 -> 4
757 -> 5
1234 -> 8
26364 -> 8
432969 -> 0
1234584 -> 8
91273716 -> 6
Explained first variant (112 bytes):
n=1ドル; # (*) get argument n
shopt -s extglob; # enable ${n//?()/ } below
while((n>9));do # loop until number is <=9
m=0; # preset maximum to 0
for x in ${n//?()/ };{ # loop over the digits
((m<x))&&m=$x; # find max
};
n=$[ # calculate new number
10# # force base 10 as 0N becomes octal
${n/$m/} # remove first occurance of max
*m]; # multiply m
done;
echo $n # output result
Notes:
- Shebang
#!/bin/bash+LF(12 byte) is not included in the count.- We are in
bash, so this is already the default
- We are in
- AFAICS passing in the number directly as variable
n(see marked(*)above) is not allowed. Hence these 5 bytes are needed.- Else
./golf.sh 1234would look liken=1234 ./golf.sh - A
read n;if number is passed in from STDIN is 2 byte longer
- Else
shopt -s extglob;can be left away, if this option is given on commandline- Like in
n=1234 bash -Oextglob ./golf.sh - But
extglobis not set by default, hence AFAICS it must be included in the count
- Like in
10#is needed as${n/$m/}can leave a number with a leading0- Without the
10#input91273716wrongly returns8.
- Without the
Explained shortest variant (97 bytes):
shopt -s extglob; # see above
((1ドル>9))||exit 1ドル; # stop if goal reached
m=0;for x in ${1//?()/ };{ ((m<x))&&m=$x;}; # max calc, as above
0ドル $[10#${1/$m/}*m] # recurse to next step
Notes:
- Actually the last line is a tail recursion, so bash uses
execand does notfork
-
2\$\begingroup\$ Welcome to Code Golf, and nice first answer! Make sure to check out our tips for golfing in Bash to see if you can find ways to shorten your code. \$\endgroup\$Aaroneous Miller– Aaroneous Miller2021年09月08日 17:01:09 +00:00Commented Sep 8, 2021 at 17:01
-
1\$\begingroup\$ Nice answer! For the input, you might want to look through the default I/O rules. \$\endgroup\$2021年09月08日 17:06:01 +00:00Commented Sep 8, 2021 at 17:06
-
\$\begingroup\$ @AaronMiller Thanks for the link! 4 bytes less. \$\endgroup\$Tino– Tino2021年09月08日 17:26:30 +00:00Commented Sep 8, 2021 at 17:26
J, 40 bytes
((]*<^:3@i.{[)>./)&.(10&#.inv)^:(9&<)^:_
explanation
( iterate )^:(9&<)^:_ NB. keep iterating while the number is > 9
( stuff )&.(10&#.inv) NB. convert to digits, do stuff, convert back to number
( )>./) NB. stuff is a hook, with max digit >./ on the right
(]*<^:3@i.{[) NB. so that in this phrase, ] means "max" and [ means "all digits"
] NB. the max digit...
* NB. times...
<^:3@ NB. triple box...
i. NB. the first index of the max in the list of all digits
{ NB. "from" -- which because of the triple box means "take all indexes except..."
[ NB. from all the digits of the number
-
1\$\begingroup\$ I learnt about the triple box selection from you today, thank you! \$\endgroup\$Galen Ivanov– Galen Ivanov2017年11月19日 08:33:51 +00:00Commented Nov 19, 2017 at 8:33
PowerShell, 230 bytes
$n="$args";do{$n=$n.ToString();$a=@();0..$n.Length|%{$a+=$n[$_]};$g=[convert]::ToInt32(($a|sort|select -last 1),10);[regex]$p=$g.ToString();[int]$s=$p.replace($n,'',1);if($n.Length-eq1){$n;exit}else{$r=$s*$g}$n=$r}until($r-lt10)$r
Wasted too much on all the type casting.
PHP, (削除) 82 (削除ここまで) 77+1 bytes
for($n=$argn;$n>9;)$n=join("",explode($d=max(str_split($n)),$n,2))*$d;echo$n;
Run as pipe with -nR or try it online.
10 -> 10? \$\endgroup\$