Question: Consider a positive integer X. Multiplying its non-zero digits produces another integer Y. Repeating this process, we eventually arrive at a single digit between 1
-
Consider a positive integer X. Multiplying its non-zero digits produces another integer Y. Repeating this process, we eventually arrive at a single digit between 1 and 9.
-
Write a program called product_061.py that reads X from stdin and outputs the digit that results from applying the above process.
-
You can assume X is in the range 1-10000.
-
In this example, we start with 707. Repeatedly multiplying the non-zero digits we get 7 x 7 = 49, 4 x 9 = 36, 3 x 6 = 18, and 1 x 8 = 8. We are done since 8 is in the range 1-9 and we output 8.
$ cat product_stdin_00_061.txt 707
$ python3 product_061.py < product_stdin_00_061.txt 8
-
In this example, we start with 50. The only non-zero digit in 50 is 5. The product in this case is the digit itself so we output 5.
$ cat product_stdin_01_061.txt 50
$ python3 product_061.py < product_stdin_01_061.txt 5
-
In this example, we start with 25. 2 x 5 = 10 and the product of the non-zero digits in 10 is 1 so we output 1.
$ cat product_stdin_02_061.txt 25
$ python3 product_061.py < product_stdin_02_061.txt 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
