7
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.
I'm pretty sure that most answers from here can be trivially modified to answer this question.
– Don Thousand – 2018-09-01T16:00:33.213@Arnauld No, the output needs to be two numbers (returned or printed). I guess you could convert the decimal number to string, replace
.
with whitespace and print it. – DimChtz – 2018-09-01T16:06:17.8401Do we enter step 2 if n<10? – AlexRacer – 2018-09-01T16:55:45.823
@Arnauld That is not the same for n=2 and n=4. – AlexRacer – 2018-09-01T17:00:55.457
@Arnauld Let me know if it's any better :) – DimChtz – 2018-09-01T23:39:51.670
1Yes. It's much clearer now. :) – Arnauld – 2018-09-01T23:42:27.273