Multiplicative persistence

46

4

Multiplicative Persistence

  1. Multiply all the digits in a number
  2. Repeat until you have a single digit left

As explained by Numberphile:

Example

  1. 277777788888899 → 2x7x7x7x7x7x7x8x8x8x8x8x8x9x9 = 4996238671872
  2. 4996238671872 → 4x9x9x6x2x3x8x6x7x1x8x7x2 = 438939648
  3. 438939648 → 4x3x8x9x3x9x6x4x8 = 4478976
  4. 4478976 → 4x4x7x8x9x7x6 = 338688
  5. 338688 → 3x3x8x6x8x8 = 27648
  6. 27648 → 2x7x6x4x8 = 2688
  7. 2688 → 2x6x8x8 = 768
  8. 768 → 7x6x8 = 336
  9. 336 → 3x3x6 = 54
  10. 54 → 5x4 = 20
  11. 20 → 2x0 = 0

This is the current record, by the way: the smallest number with the largest number of steps.

Golf

A program that takes any whole number as input and then outputs the result of each step, starting with the input itself, until we hit a single digit. For 277777788888899 the output should be

277777788888899
4996238671872
438939648
4478976
338688
27648
2688
768
336
54
20
0

(Counting the number of steps is left as an exercise to the user).

More Examples

From A003001:

25
10
0

From A003001 as well:

68889
27648
2688
768
336
54
20
0

From the Numberphile video:

327
42
8

So there has been a question about Additive Persistence, but this is Multiplicative Persistence. Also, that question asks for the number of steps as output, while I'm interested in seeing the intermediate results.

SQB

Posted 2019-03-21T14:20:13.193

Reputation: 681

Bonus: find a new record: smallest number with the largest number of steps. Caveat: conjecture has it that 11 is the largest possible. – SQB – 2019-03-21T14:31:41.013

7You probably should include a few more tests cases that do not end with $0$. – Arnauld – 2019-03-21T14:33:34.680

Came to make this post, found it already existing, gg – cat – 2019-03-21T14:55:21.397

is a single digit valid input? – dzaima – 2019-03-21T18:30:53.540

@dzaima yes, but it should only print that number and then stop. – SQB – 2019-03-21T18:35:00.450

1In the Numberphile video, Matt Parker states that searches have been done to several hundred digits. – HardScale – 2019-03-21T21:46:56.210

@Arnauld I've added some more examples. – SQB – 2019-03-22T08:15:15.607

For languages with type size limits, do we have to use the fancy bignum types or just fit the largest example case (277777788888899)? – Stackstuck – 2019-03-24T03:24:13.360

Answers

7

Jelly, 4 bytes

DP$Ƭ

Try it online!

Explanation

D    | convert to decimal digits
 P   | take the product
  $  | previous two links as a monad
   Ƭ | loop until no change, collecting all intermediate results

As a bonus, here's a TIO which will find the numbers with the largest number of steps for a given range of numbers of digits. It scales well even on TIO.

Nick Kennedy

Posted 2019-03-21T14:20:13.193

Reputation: 11 829

15

TI-BASIC (TI-84), 30 32 31 bytes

-1 byte thanks to @SolomonUcko!

While Ans>9:Disp Ans:prod(int(10fPart(Ans10^(seq(-X-1,X,0,log(Ans:End:Ans

Input is in Ans.
Output is displayed as the challenge requests. The trailing Ans is needed to print the last step.

I will admit, I did not think of this formula myself, rather I found it here and modified it to better fit the challenge.

EDIT: Upon rereading the challenge, I realized that the program must terminate if the product is one digit. Hence, 2 bytes were to be added to account for this.

Example:

24456756
        24456756
prgmCDGF8
        24456756
          201600
               0
11112
           11112
prgmCDGF8
           11112
               2

Explanation:

While Ans>9               ;loop until the product is one digit
Disp Ans                  ;display the current product
prod(                     ;get the product of...
 int(                     ; the integer part of...
  10fPart(                ; ten times the fractional part of...
  Ans                     ; each element in the following list times the
                          ;  current product
  10^(                    ; multiplied by the list generated by using each
                          ;  element of the following list as an exponent
                          ;  for 10^n
   seq(-X-1),X,0,log(Ans  ; generate a list of exponents from -1 to -L where
                          ;  L = the length of the current product
End
Ans                       ;leave the final product in "Ans" and implicitly
                          ; print it

Visual Model:
Ans starts off as 125673.
This model only covers the logic behind multiplying the digits; everything else is easier to understand.

seq(-X-1,X,0,log(Ans  =>  seq(-X-1,X,0,5.0992
   {-1 -2 -3 -4 -5 -6}
10^(...
   {.1 .01 .001 1E-4 1E-5 1E-6}
Ans...
   {12567.3 1256.73 125.673 12.5673 1.25673 .125673}
fPart(...
   {.3 .73 .673 .5673 .25673 .125673}
10...
   {3 7.3 6.73 5.673 2.5673 1.25673}
int(...
   {3 7 6 5 2 1}
   (the digits of the number, reversed)
prod(...
   1260
   (process is repeated again)

seq(-X-1,X,0,log(Ans  =>  seq(-X-1,X,0,3.1004
   {-1 -2 -3 -4}
10^(...
   {.1 .01 .001 1E-4}
Ans...
   {126 12.6 1.26 .126}
fPart(...
   {0 .6 .26 .126}
10...
   {0 6 2.6 1.26}
int(...
   {0 6 2 1}
prod(...
   0
   (product is less than 10.  loop ends)

Notes:

TI-BASIC is a tokenized language. Character count does not equal byte count.

10^( is this one-byte token.

This program will not provide the correct sequence of products with integers greater than 14 digits long due to the limitations of decimal precision on the TI calculators.

Tau

Posted 2019-03-21T14:20:13.193

Reputation: 1 935

Can you save a byte by moving 10^( outside seq( and omitting the closing parenthesis? – Solomon Ucko – 2019-04-01T11:15:40.987

Yes, I believe so! – Tau – 2019-04-01T13:08:01.160

11

K (ngn/k), 9 bytes

{*/.'$x}\

Try it online!

{ }\ keep applying the function in curly braces until the sequence converges

$x format the argument as a string (list of characters)

.' evaluate each (other dialects of k require a colon, .:')

*/ times over, i.e. product

ngn

Posted 2019-03-21T14:20:13.193

Reputation: 11 449

8

05AB1E, 7 4 bytes

Δ=SP

Try it online or verify all test cases.

Explanation:

Δ     # Loop until the number no longer changes:
 =    #  Print the number with trailing newline (without popping the number itself)
      #  (which will be the implicit input in the first iteration)
  SP  #  Convert the number to a list of digits, and calculate its product

Kevin Cruijssen

Posted 2019-03-21T14:20:13.193

Reputation: 67 575

8

dzaima/APL, 14 11 bytes

∪{×/⍎¨⍕⍵}⍡≡

Try it online!

dzaima

Posted 2019-03-21T14:20:13.193

Reputation: 19 048

8

R, 59 bytes

n=scan();while(print(n)>9)n=prod(n%/%10^(nchar(n):1-1)%%10)

Try it online!

Since print invisibly returns its input, we can use print(n) inside the while loop to simulate a do-while loop. This is inspired by one of my tips for golfing in R.

The header helps prevent large numbers from being printed in scientific notation.

Giuseppe

Posted 2019-03-21T14:20:13.193

Reputation: 21 077

7

Wolfram Language (Mathematica), 47 bytes

Most@FixedPointList[Times@@IntegerDigits@#&,#]&

Try it online!

J42161217

Posted 2019-03-21T14:20:13.193

Reputation: 15 931

7

Wolfram Language (Mathematica), 45 bytes

#//.x_/;x>9:>Times@@IntegerDigits@x&&Print@x&

Try it online!

J42161217

Posted 2019-03-21T14:20:13.193

Reputation: 15 931

42 bytes if you don't mind using Echo. – Roman – 2019-07-28T22:01:31.530

7

Perl 6, 23 bytes

{$_,{[*] .comb}…10>*}

Try it online!

Grimmy

Posted 2019-03-21T14:20:13.193

Reputation: 12 521

7

Python 2,  46  43 bytes

-3 thanks to xnor (chained comparison)

def f(n):print n;n>9>f(eval('*'.join(`n`)))

Try it online!

Jonathan Allan

Posted 2019-03-21T14:20:13.193

Reputation: 67 804

You can do > in place of and. – xnor – 2019-03-22T05:42:52.547

@xnor thanks, easy to forget that will work. – Jonathan Allan – 2019-03-22T07:42:17.913

5

Julia 0.7, 36 33 bytes

f(n)=n>9?[n;f(prod(digits(n)))]:n

Try it online!

Thanks to H.PWiz for -3 bytes.

Kirill L.

Posted 2019-03-21T14:20:13.193

Reputation: 6 693

You can use [n;f(prod(digits(n)))] – H.PWiz – 2019-04-20T01:32:49.217

5

MathGolf, 9 10 bytes

h(ôo▒ε*h(→

Try it online!

Now it correctly handles inputs that are single digits. Not perfect, but at least it is correct.

Explanation

h(            check length of input number and decrease by 1
  ö       →   while true with pop using the next 6 operators
   p          print with newline
    ▒         split to list of chars/digits
     ε*       reduce list by multiplication
       h(     length of TOS without popping, subtracted by 1 (exits when len(TOS) == 1)

maxb

Posted 2019-03-21T14:20:13.193

Reputation: 5 754

The output for a single digit input should be one copy of the number - clarified in the comments – dzaima – 2019-03-21T18:38:30.403

@dzaima I'll look into it, and update the answer when it's solved – maxb – 2019-03-22T08:11:13.067

5

C# (Visual C# Interactive Compiler), 79 74 68 bytes

void f(int a){Print(a);if(a>9)f((a+"").Aggregate(1,(j,k)=>k%48*j));}

I try to stay away from recursion in C# due to how long the method declaration is, but in this case it saves compared to a loop.

Try it online!

Embodiment of Ignorance

Posted 2019-03-21T14:20:13.193

Reputation: 7 014

5

Python 2, 61 62 59 bytes

def f(n):print n;n>9and f(reduce(int.__mul__,map(int,`n`)))

Try it online!

-3 bytes, thanks to Jonathan Allan

TFeld

Posted 2019-03-21T14:20:13.193

Reputation: 19 246

Doesn't work for inputs that don't end with a 0 on their last iteration, for example 23 – Embodiment of Ignorance – 2019-03-21T15:40:46.830

int.__mul__ is three bytes less than lambda a,b:a*b – Jonathan Allan – 2019-03-21T17:17:50.510

@JonathanAllan Thanks! I knew there had to be something like that – TFeld – 2019-03-21T19:05:52.963

Change f(reduce(int.__mul__,map(int,\n`)))tof(eval('*'.join(`n`)))` to save 13 bytes. – mypetlion – 2019-03-21T20:34:49.343

@mypetlion ...I already did that in another post. – Jonathan Allan – 2019-03-21T20:52:58.127

5

PowerShell, 54 bytes

for($a=$args;$a-gt9){$a;$a=("$a"|% t*y)-join"*"|iex}$a

Try it online!


Iterative method that first writes the input argument, then converts it into a string and pipes it into a character array. This array is joined by a single asterisks, and executed as a command with the invoke expression alias. Since this writes Starting number down to the last number greater than 0, (20, in the given test scenario), I add a final $a to the end to output.

KGlasier

Posted 2019-03-21T14:20:13.193

Reputation: 211

5

PHP, 63 bytes

<?=$n=$argn;while($n>9)echo"
",$n=array_product(str_split($n));

Iterative version, call with php -nF input from STDIN.

Try it online!

PHP, 72 71 bytes

function h($n){echo"$n
",($n=array_product(str_split($n)))>9?h($n):$n;}

Try it online!

Recursive version, as function.

Input: 277777788888899

277777788888899
4996238671872
438939648
4478976
338688
27648
2688
768
336
54
20
0

Input: 23

23
6

640KB

Posted 2019-03-21T14:20:13.193

Reputation: 7 149

5

perl 5 (-n -M5.01), 32 30 25 bytes

say$_=eval;s/\B/*/g&&redo

25 bytes

30 bytes

32 bytes

Nahuel Fouilleul

Posted 2019-03-21T14:20:13.193

Reputation: 5 582

You should mention that this uses -lpF// – Grimmy – 2019-03-22T08:24:54.623

1@Grimy i could save 2 bytes without using -lpF//, updating – Nahuel Fouilleul – 2019-03-22T08:42:55.470

4

JavaScript (ES6), 45 bytes

Returns an array of integers.

f=n=>[n,...n>9?f(eval([...n+''].join`*`)):[]]

Try it online!

Arnauld

Posted 2019-03-21T14:20:13.193

Reputation: 111 334

4

Ruby, 38 35 34 bytes

f=->n{p(n)>9&&f[eval n.digits*?*]}

Try it online!

1 byte saved by by G B.

Kirill L.

Posted 2019-03-21T14:20:13.193

Reputation: 6 693

4

PowerShell, 51 bytes

filter f{$_
if($_-gt9){("$_"|% t*y)-join'*'|iex|f}}

Try it online!

mazzy

Posted 2019-03-21T14:20:13.193

Reputation: 4 832

4

APL(NARS), 19 chars, 38 bytes

{⍵≤9:⍵⋄∇×/⍎¨⍕⍵⊣⎕←⍵}

test:

   f←{⍵≤9:⍵⋄∇×/⍎¨⍕⍵⊣⎕←⍵}
   f 23     
23
6
   f 27648     
27648
2688
768
336
54
20
0

RosLuP

Posted 2019-03-21T14:20:13.193

Reputation: 3 036

4

Haskell, 45 bytes

f n=n:[x|n>9,x<-f$product$read.pure<$>show n]

Try it online!

nimi

Posted 2019-03-21T14:20:13.193

Reputation: 34 639

4

J, 16 bytes

([:*/,.&.":)^:a:

Try it online!

Galen Ivanov

Posted 2019-03-21T14:20:13.193

Reputation: 13 815

4

Japt -R, 9 bytes

Horribly inefficient - don't even try to run the first test case!

_ì ×}hN â

Try it

_ì ×}hN â     :Implicit input of integer U
      N       :Starting with the array of inputs (i.e., [U])
     h        :Do the following U times, pushing the result to N each time
_             :Take the last element in N and pass it through the following function
 ì            :  Convert to digit array
   ×          :  Reduce by multiplication
    }         :End function
        â     :Deduplicate N
              :Implicitly join with newlines and output

Shaggy

Posted 2019-03-21T14:20:13.193

Reputation: 24 623

3

JavaScript (Babel Node), 46 bytes

f=a=>a>9?[a,...f(eval([...a+''].join`*`))]:[a]

Try it online!


JavaScript (Babel Node), 44 bytes

If the input can be taken as String

f=a=>a>9?[a,...f(''+eval([...a].join`*`))]:a

Try it online!

Luis felipe De jesus Munoz

Posted 2019-03-21T14:20:13.193

Reputation: 9 639

@Arnauld Yes, I just edited and added the wrong code. Im still looking for something using only strings instead arrays – Luis felipe De jesus Munoz – 2019-03-21T15:12:34.337

3

Brachylog, 7 bytes

ẉ?Ḋ|ẹ×↰

Try it online!

Explanation

ẉ          Write the input followed by a linebreak
 ?Ḋ        If the input is a single digit, then it's over
   |       Otherwise
    ẹ      Split the input into a list of digits
     ×     Multiply them together
      ↰    Recursive call with the result of the multiplication as input

Fatalize

Posted 2019-03-21T14:20:13.193

Reputation: 32 976

I gave it a try myself. Forgot about the Ḋ. The rest i had the same. – Kroppeb – 2019-03-21T19:33:04.287

3

PowerShell, 64 59 bytes

for($a="$args";9-lt$a){$a;$a="$(($a|% t*y)-join'*'|iex)"}$a

Try it online!

Iterative method. Takes input and stores it into $a, then enters a for loop so long as the length of $a is two or more (i.e., it's bigger than 9). Inside the loop we output $a and then recalculate it by converting it toCharArray, joining it together with *, and then iex (short for Invoke-Expression and similar to eval). Once we're out of the loop, we have a single digit left to print, so we place $a onto the pipeline again.

-5 bytes thanks to KGlasier.

AdmBorkBork

Posted 2019-03-21T14:20:13.193

Reputation: 41 581

You could use the comparison 9-lt$a instead of $a.length-1 to save 5 bytes. And if you didn't go string based the whole time you could cut off a decent chunk. Check out my powershell attempt if you want!

– KGlasier – 2019-03-21T16:22:15.937

3

Charcoal, 13 bytes

θW⊖Lθ«≔IΠθθ⸿θ

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

θ

Print the input for the first time.

W⊖Lθ«

Repeat while the length of the input is not 1.

≔IΠθθ

Replace the input with its digital product cast to string.

⸿θ

Print the input on a new line.

Neil

Posted 2019-03-21T14:20:13.193

Reputation: 95 035

3

Retina, 24 bytes

.+~(\`

.
$&$*
^
.+¶$$.(

Try it online! Explanation:

.+~(\`

Print the current value on its own line at the start of every loop until it stops changing and don't print the unchanged value twice. Evaluate the current value at the end of each loop.

.
$&$*

Add a * after each digit.

^
.+¶$$.(

Finish turning the input into an expression that evaluates to the digital product.

Just for the record, Retina can do this in one line (25 bytes):

.+"¶"<~[".+¶$.("|'*]'*L`.

Neil

Posted 2019-03-21T14:20:13.193

Reputation: 95 035

3

Java, 112 106 bytes

"Lossy conversion" thanks Java for the extra ~15 bytes
-6 bytes thanks to @Kevin Cruijssen
Obligatory stream abuse answer

n->{for(long t=10;t>9;n=(n+"").chars().mapToLong(i->i).reduce(1,(x,y)->x*(y-48)))System.out.println(t=n);}

Try it online

Benjamin Urquhart

Posted 2019-03-21T14:20:13.193

Reputation: 1 262

I'm pretty sure you only need to support int – Embodiment of Ignorance – 2019-03-23T00:28:29.473

@EmbodimentofIgnorance then it fails for 277777788888899 since 4996238671872 is too large for an int – Benjamin Urquhart – 2019-03-23T18:31:13.923

106 bytes by making it non-recursive. – Kevin Cruijssen – 2019-07-29T12:09:27.027

@KevinCruijssen thanks – Benjamin Urquhart – 2019-07-29T13:56:35.777

3

C (gcc), 58 bytes

f(n,t){for(;n=printf("%d\n",t=n)>2;)for(;n*=t%10,t/=10;);}

Try it online!

Iterative approach turns out to be 1 byte shorter.

f(n,t){
    for(;n=printf("%d\n",t=n)   //print and update current number
            >2;)                //until only one digit is printed
        for(;n*=t%10,t/=10;);   //n*= product of digits of t (step)
}

C (gcc), 61 59 bytes (recursive)

f(n){printf("%d\n",n)>2&&f(p(n));}p(n){n=n?n%10*p(n/10):1;}

Try it online!

Recursion seems to be shorter than iteration for both print and step...

attinat

Posted 2019-03-21T14:20:13.193

Reputation: 3 495

2

Swift, 115 bytes

func m(a:Int){
print("\(a)")
var b=1,c=a
while c>0{
b*=c%10
c/=10}
b>9 ? m(a:b):print("\(b)")}
m(a:277777788888899)

Try it online!

onnoweb

Posted 2019-03-21T14:20:13.193

Reputation: 211

3

I think you can move the call to m into the Footer portion of TIO, leaving this answer as 95 bytes

– Giuseppe – 2019-03-22T00:33:37.277

1

Also, 81 bytes by removing some whitespace/formatting redundancy.

– Kirill L. – 2019-03-23T09:46:12.337

2

PicoLisp, 73 72 bytes

(de f(x)(if(> 10 x)(list x)(cons x(f(apply *(mapcar format(chop x)))))))

Try it online!

Galen Ivanov

Posted 2019-03-21T14:20:13.193

Reputation: 13 815

2

Japt, 15 bytes

I got some help from Shaggy on this one.

@pUÌì × Ì>9}f U

Run it online

Oliver

Posted 2019-03-21T14:20:13.193

Reputation: 7 160

2

Chevron, 100 bytes

>^__>>^n
->+11?^n<10
>^n
^d<<1
^i<<1
^m<^i>^n
->+4??^m=^__
^d<<^d*^m
^i<<^i+1
->-4
^n<<^d
->-10
><^n

This is a fairly new language of my own creation - prototype interpreter, documentation, and example programs can be found at https://github.com/superloach/chevron.

Explanation:

  • >^__>>^n - take the input as a NUM, with empty prompt
  • ->+11?^n<10 - if the number is under 10 (1 digit), skip to the end
  • >^n - output the current number
  • ^d<<1 - initialise the product to 1
  • ^i<<1 - initialise the character index to 1
  • ^m<^i>^n - get i'th character of the number
  • ->+4??^m=^__ - jump out of loop if no character is found
  • ^d<<^d*^m - multiply character into product
  • ^i<<^i+1 - increment character index
  • ->-4 - hop back to continue getting characters
  • ^n<<^d - once loop is done, assign product as new number
  • ->-10 - hop all the way to the beginning for the next iteration
  • ><^n - exit, printing the final number

Superloach

Posted 2019-03-21T14:20:13.193

Reputation: 71

1Welcome. Great first answer! – mbomb007 – 2019-07-29T16:45:29.070

@mbomb007 thank you!! – Superloach – 2019-07-29T16:53:55.920

1

java 8, 105 104 bytes

-1 byte thanks to @Benjamin Urquhart, replacing '0' with 48

i->{String s="";for(;(s+=i+"\n")!=""&i>9;){long m=1;for(byte c:(""+i).getBytes())m*=c-48;i=m;}return s;}

TIO

Nahuel Fouilleul

Posted 2019-03-21T14:20:13.193

Reputation: 5 582

-1 byte by replacing '0' with 48 – Benjamin Urquhart – 2019-03-21T19:41:37.947

1With java 10 you could save a few bytes by writing var instead of String and so on. – Socowi – 2019-03-21T20:48:36.780

1

Factor, 79 72 bytes

: f ( x -- ) [ 10 >base [ 48 - ] map product dup dup . 9 > ] loop drop ;

Try it online!

Galen Ivanov

Posted 2019-03-21T14:20:13.193

Reputation: 13 815

1

ink, 66 bytes

=p(n)
{n}{n<10:->->}
~temp v=1
-(a)~v=v*n%10
~n=n/10
{n:->a}->p(v)

Try it online!

Explanation

=p(n)       // entry point - a stitch called p, with one parameter.
{n}         // print the value of n
{n<10:->->} // if n is a single digit, divert to wherever we entered from
~temp v=1   // declate a variable v, where we keep track of the product, and set it to 1
-(a)        // a labeled gather - we can jump here later
~v=v*n%10   // multiply v by (n mod 10) - the last digit in n
~n=n/10     // divide n by 10 - removing the last digit
{n:->a}     // if n is nonzero, go back to a
->p(v)      // if we reach this, divert to the top, this time with n set to the value of v

Doesn't feel very golfed, but I have no idea how you'd improve it.

Sara J

Posted 2019-03-21T14:20:13.193

Reputation: 2 576

1

Python 2, 87 bytes

b=input()
while 1:
 a=str(b);print a;b=1
 if len(a)<2:break
 for d in list(a):b*=int(d)

Try it online!

akozi

Posted 2019-03-21T14:20:13.193

Reputation: 551

1

SNOBOL4 (CSNOBOL4), 100 bytes

	P =INPUT
O	OUTPUT =N =P	:(S)
S	P =GT(N,9) 1	:F(END)
D	N LEN(1) . D REM . N	:F(O)
	P =P * D	:(D)
END

Try it online!

A nice round 100 bytes!

	P =INPUT			;* Store input to P
O	OUTPUT =N =P	:(S)		;* print P and set N to P
S	P =GT(N,9) 1	:F(END)		;* if N > 9, set P (the digit product) to 1
D	N LEN(1) . D REM . N	:F(O)	;* take the first digit of N as D and set remaining digits to N
					;* if N is an empty string, goto label O
	P =P * D	:(D)		;* calculate the digit product, then return to D
END

Giuseppe

Posted 2019-03-21T14:20:13.193

Reputation: 21 077

1

C, 138 115 bytes

long atol(char*);main(c,v)char**v;{long z=1,n=atol(v[1]);for(;printf("%ld\n",n),n>9;n=z,z=1)for(;z*=n%10,n/=10;);}

Pass the number as a first command line argument.

Thanks to attinat for help with saving some bytes.

Ungolfed and prettyified:

long atol(char *);
main(c, v) char **v; {
    long z = 1,n = atol(v[1]);
    for(; printf("%ld\n", n), n > 9; n = z, z = 1)
        for(; z *= n % 10, n /= 10;);
}

qookie

Posted 2019-03-21T14:20:13.193

Reputation: 81

for can save you quite a few bytes. – attinat – 2019-03-24T06:14:15.650

Suggest int**v instead of char**v – ceilingcat – 2019-04-21T06:37:45.380

1

Pyth, 9 bytes

j.u*FsM`N

Try it here!

If we're allowed to output as an array, the j at the start is unnecessary, giving 8 bytes.

RK.

Posted 2019-03-21T14:20:13.193

Reputation: 497

You can also omit the N at the end, since Pyth implicitly adds lambda variables at the end of a program if needed. – hakr14 – 2019-07-28T22:42:55.390

1

Racket, 107 bytes

(define(f n)(if(> 10 n)(list n)(cons n(f(apply *(map(λ(x)(-(char->integer x)48))(string->list(~v n))))))))

Try it online!

Galen Ivanov

Posted 2019-03-21T14:20:13.193

Reputation: 13 815

1

C# (.NET Core), 192 191 180 bytes

using C=System.Console;class A{static void Main(string[] a){long i,k;var b=a[0];for(C.WriteLine(b);(k=b.Length)>1;C.WriteLine(b=i.ToString())){i=1;for(int j=0;j<k;)i*=b[j++]-48;}}}

Try it online!

Pretty much just naive looping. I feel like that's a bit of a silly way to do it but here we are. Had a weird issue early on where the input didn't match the output, because it was overflowing an int, so here we are with longs. I could upgrade it to BigInteger if I needed to, though that would cost a few bytes.

I swear I always end up with off by ones when I do loops here, and it annoys me to no end.

Ungolfed-ish version:

using C=System.Console;
class A{
    static void Main(string[] a){
        long i,k;
        var b=a[0];
        for(C.WriteLine(b);(k=b.Length)>1;C.WriteLine(b=i.ToString())){
            i=1;
            for(int j=0;j<k;)i*=b[j++]-48;
        }
    }
}

Works for all integers less than int.MAX_VALUE digits long, C# (.NET Core), 204 bytes

using C=System.Console;class A{static void Main(string[] a){System.Numerics.BigInteger i;int j,k;var b=a[0];for(C.WriteLine(b);(k=b.Length)>1;C.WriteLine(b=i.ToString())){i=1;for(j=0;j<k;)i*=b[j++]-48;}}}

Try it online!

Stackstuck

Posted 2019-03-21T14:20:13.193

Reputation: 209

Saved 1 byte - moved C.WriteLine(b) into loop opening as well as iterator – Stackstuck – 2019-03-24T04:01:45.090

Saved 11 bytes with assorted rearrangements. – Stackstuck – 2019-03-25T15:16:13.863

1

Attache, 28 27 bytes

{If[_>9,_'$@Prod@List@_,_]}

Try it online!

Oddly enough, the recursive approach, rather than a tacit approach, seems to be shortest. (There is a bug in NestListWhile involving currying, but even with the bugfix it'd be 3 bytes longer.)

Explanation

{
    If[ _ > 9 ,
        ?? if this is true, return a list
        [
            ?? with the current number
            _,
            ?? followed by this function called on the digital product
            ...$[ Prod[Digits[_]] ]
        ],
        ?? otherwise, return _
        _
    ]
}

Alternative

32 bytes: NestListWhile<~Prod@List,{_>9}~>

Conor O'Brien

Posted 2019-03-21T14:20:13.193

Reputation: 36 228

1

Stax, 4 bytes

▒σ╛g

Run and debug it

Unpacked, it's pretty simple.

gu  use the rest of the program to generate values until a duplicate is encountered
    output implicitly
E:* calculate product of digits

recursive

Posted 2019-03-21T14:20:13.193

Reputation: 8 616

1

Perl 6, 23 bytes

{$_,{[*] .comb}...10>*}

Try it online!

bb94

Posted 2019-03-21T14:20:13.193

Reputation: 1 831

1

c++, lambda function, 72 bytes

[](auto n){while(cout<<n<<endl&&n>9){auto t=n;for(n=1;n*=t%10,t/=10;);}}

try it online

Johan du Toit

Posted 2019-03-21T14:20:13.193

Reputation: 1 524

1

Clojure, 73 bytes

#(loop[i %](prn i)(if(> i 9)(recur(apply *(for[c(str i)](-(int c)48))))))

NikoNyrh

Posted 2019-03-21T14:20:13.193

Reputation: 2 361

0

Scala, 83 bytes

def^(n:Long):Long={println(n);if(n>9)^((n+"").map(x=>(x-48).toLong).product)else n}

Try it online!

Dr Y Wit

Posted 2019-03-21T14:20:13.193

Reputation: 511