Challenge
For a given positive integer \$n\$:
- Repeat the following until \$n < 10\$ (until \$n\$ contains one digit).
- Extract the last digit.
- If the extracted digit is even (including 0) multiply the rest of the integer by \$2\$ and add \$1\$ ( \$2n+1\$ ). Then go back to
step 1
else move tostep 4
. - Divide the rest of the integer with the extracted digit (integer / digit) and add the remainder (integer % digit), that is your new \$n\$.
Note: In case n < 10
at the very beginning obviously you don't move to step 2. Skip everything and print \$[n, 0]\$.
Input
n: Positive integer.
Output
[o, r]: o
: the last n
, r
: number of repeats.
Step-by-step example
For input n = 61407
:
Repeat 1: 6140_7 -> 6140 / 7 = 877, 6140 % 7 = 1 => 877 + 1 = 878
Repeat 2: 87_8 -> 2 * 87 + 1 = 175 (even digit)
Repeat 3: 17_5 -> 17 / 5 = 3, 17 % 5 = 2 => 3 + 2 = 5 ( < 10, we are done )
The output is [5, 3]
(last n
is 5 and 3 steps repeats).
Rules
There are really no rules or restrictions just assume n > 0
. You can either print or return the output.
.
with whitespace and print it. \$\endgroup\$ – DimChtz Sep 1 '18 at 16:06