53
\$\begingroup\$

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, showing that the single digit doesn't have to be 0:

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.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Bonus: find a new record: smallest number with the largest number of steps. Caveat: conjecture has it that 11 is the largest possible. \$\endgroup\$
    – SQB
    Mar 21, 2019 at 14:31
  • 8
    \$\begingroup\$ You probably should include a few more tests cases that do not end with \$0\$. \$\endgroup\$
    – Arnauld
    Mar 21, 2019 at 14:33
  • \$\begingroup\$ Came to make this post, found it already existing, gg \$\endgroup\$
    – cat
    Mar 21, 2019 at 14:55
  • \$\begingroup\$ is a single digit valid input? \$\endgroup\$
    – dzaima
    Mar 21, 2019 at 18:30
  • 1
    \$\begingroup\$ In the Numberphile video, Matt Parker states that searches have been done to several hundred digits. \$\endgroup\$
    – HardScale
    Mar 21, 2019 at 21:46

67 Answers 67

1 2
3
1
\$\begingroup\$

Python 3, 47 bytes

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

First Python 3 answer here.

Try it online!

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

FunStack alpha, 38 bytes

Product Minus 48 Show compose3 fixiter

Try it at Replit: pass the input number as a command-line argument and enter the program on stdin.

Explanation

The fixiter modifier iterates a function until it reaches a fixed value, returning the full list of intermediate values. This is exactly what we want; there is a slight complication because the builtin ToBase function, which returns a list of digits, converts zero to an empty list rather than a list containing 0. So instead, we convert the number to a string and then convert each character to the corresponding digit.

                      compose3          Compose these three functions:
                 Show                     Convert number to string
        Minus 48                          Subtract 48 from each character
Product                                   Take the product of the character codes
                               fixiter  Iterate that function, stopping when a fixed point
                                        is reached, and return a list of intermediate values
\$\endgroup\$
1
\$\begingroup\$

><>, 31 29 bytes

Saved 2 bytes thanks to Jo King

:nao:a),1}\~
1:,a}*{%a:/!?:-%

Try it online!

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

Vyxal 3, 2 bytes

ᵘΠ

Try it Online!

2 bytes with a new modifier. Come try Vyxal 3!

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

Vyxal, j, 3 bytes

⁽Π↔

Try it Online!

Very nice little 3 byter here. Uses the same method as the 05ab1e answer.

Explained

⁽Π↔
⁽Π  # lambda x: product(x) // treats numbers as a list of digits
 ↔ # repeat the above function until the result doesn't change, collecting intermediate results. 
\$\endgroup\$
0
\$\begingroup\$

Pyt, 6 bytes

Đ`ҎĐłŕ

Try it online!

Đ         Implicit input; duplicate top of stack
 `  ł     do while top of stack is not 0 (consumes top of stack when checking)
  Ҏ       digit product of top of stack
   Đ      duplicate top of stack
     ŕ    remove top of stack (duplicate 0); implicit print
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Fails for input 327 (since it does not necessarily end with 0). \$\endgroup\$
    – SQB
    Dec 19, 2022 at 9:34
0
\$\begingroup\$

it's not efficient but gets the job done

echo 777777777777777777777777777777777779999999999999999999933333333333  |


gawk -M 'function __(_){return _*_==_?+$_:$_*__(--_)}($_=__(NF*=!/0/))_' FS=
815859530632739726716133814001433680647636177446909421
\$\endgroup\$
1 2
3

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.