# Extract and Divide

## Challenge

For a given positive integer $n$:

1. Repeat the following until $n < 10$ (until $n$ contains one digit).
2. Extract the last digit.
3. 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 to step 4.
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. Sep 1 '18 at 16:00
• @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. Sep 1 '18 at 16:06
• Do we enter step 2 if n<10? Sep 1 '18 at 16:55
• @Arnauld That is not the same for n=2 and n=4. Sep 1 '18 at 17:00
• Yes. It's much clearer now. :) Sep 1 '18 at 23:42

# R, 93 bytes

function(n,r=0,e=n%%10,d=(n-e)/10)"if"(n<10,c(n,r),"if"(e%%2,f(d%/%e+d%%e,r+1),f(d*2+1,r+1)))


Try it online!

• Save some bytes by getting rid of second "if", reversing n<10 condition and using %/% in d definition : TIO Sep 1 '18 at 20:24
• agree with @KirillL. . Also unfortunately f= needs to be included in the bytecount since the function is called recursively. Sep 13 '18 at 17:03

# Python 2, 78 77 76 73 bytes

Recursive version. Saved 3 bytes thanks to Jonathan Frech.

f=lambda n,r=0:n>9and f(n%2*sum(divmod(n/10,n%10))or n/10*2+1,-~r)or[n,r]


Try it online!

# 74 bytes

Full program.

n,i=input(),0
while n>9:i+=1;r,R=n/10,n%10;n=[r-~r,r/R+r%R][R&1]
print n,i


Try it online!

• Since 2 divides 10, is n%10%2 not equivalent to n%2? Sep 1 '18 at 23:47
• Great point by @JonathanFrech that will get you to 73 bytes. I think the following form would also work, and is also 73 bytes: def g(n,r=0):u,v=n/10,n%10;return u and g([u-~u,u/v+u%v][v&1],r+1)or(n,r) Sep 2 '18 at 21:16
• Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP. Sep 2 '18 at 21:17

# JavaScript (ES6), 62 59 bytes

f=(n,r=0)=>(x=n/10|0)?f((n%=10)&1?x/n+x%n|0:x-~x,r+1):[n,r]


Try it online!

### Commented

f = (n, r = 0) =>         // n = input, r = number of iterations
(x = n / 10 | 0) ?      // x = floor(n / 10); if x is not equal to 0:
f(                    //   do a recursive call:
(n %= 10) & 1 ?     //     n = last digit; if odd:
x / n + x % n | 0 //       use quotient + remainder of x / n
:                   //     else:
x - ~x,           //       use x - (-x - 1) = x + x + 1 = 2x + 1
r + 1               //     increment the number of iterations
)                     //   end of recursive call
:                       // else:
[n, r]                //   return [ final_result, iterations ]


# 05AB1E (legacy), 19 bytes

[DgD–#ÁćDÈi\·>ë‰O]?


Explanation:

[                     Start infinite loop
DgD                  Get the length
–#                If it's 1, print the loop index and break.
ÁćD             Extract: "hello" -> "hell", "o"
Èi           if even,
\·>        Double and increment
ë       else,
‰O     Divmod, and sum the result (div and mod).
]    End if statement & infinite loop
?   Print


Try it online!

# Julia 1.0, 8180 79 bytes

function d(n,c=0)l,r=n÷10,n%10;n>9 ? d(r%2<1 ? 2l+1 : l÷r+l%r,c+1) : [n,c]end


Try it online!

• -1 (thanks Mr. Xcoder)
• Whis is my first golfcode :) Sep 1 '18 at 19:11
• Welcome to PPCG! Sep 1 '18 at 19:45
• @Giuseppe thanks Sep 1 '18 at 19:49
• Save a byte using <1 rather than ==0. Sep 1 '18 at 20:50

# Ruby, 61 bytes

f=->n,r=0{n>9?(a=n%10;n/=10;f[a%2<1?n-~n:n/a+n%a,r+1]):[n,r]}


Try it online!

# Retina, 79 bytes

(\d+)[02468]$;$.(_2*$1* (\d+)(.)$2*_;$1* }(_+);(\1)*(_*) ;$.($3$#2*
^;*
$.&;  Try it online! Explanation: (\d+)[02468]$
;$.(_2*$1*


If the last digit is even, multiply the rest of the integer by 2 and add 1. This always results in an odd number, so we don't need to repeat this.

(\d+)(.)
$2*_;$1*


Split off the last digit and convert to unary for the divmod.

}(_+);(\1)*(_*)
;$.($3$#2*  Divide the rest of the integer by the last digit and add the remainder. Then repeat the whole program until there's only one digit left. ^;*$.&;


Count the number of operations performed.

# Perl 6, 61 bytes

{tail kv $_,{$1%2??$0/$1+$0%$1+|0!!$0*2+1}...{!/(.+)(.)/}: 2}  Try it online! Returns (r, o). # Brachylog, 43 bytes Feels messy and bad but here it is anyway ;0{hl1&|⟨{h⟨k{t%₂0&h×₂+₁|⟨÷+%⟩}t⟩}↰₁{t+₁}⟩}  Try it online! VBA (Excel), 145?, 142, 139, 123 bytes Condensed Form: Sub t(n) While n>10 e=Right(n, 1) n=Left(n,Len(n)-1) If e Mod 2=0Then n=n*2+1 Else n=Int(n/e)+n Mod e c=c+1 wend Debug.?n;c End Sub  Expanded (with comments) Sub test(n) 'For a given positive integer n 'Repeat the following until n<10 While n > 10 'Extract the last digit e = Right(n, 1) 'Remove the last digit n = Left(n, Len(n) - 1) 'If the extracted digit is even... If e Mod 2 = 0 Then 'Multiply the rest of the integer by 2 and add 1 n = n * 2 + 1 Else 'Divide the rest of the integer with the extracted digit and add the remainder n = Int(n / e) + n Mod e End If 'Keep count c = c + 1 'End the Loop Wend 'Give the output Debug.Print (n ; c) End Sub  (Do excuse me, as this is my first post. I will happily edit! Right now, it looks like to test it you open the Immediate Window and type 't n' where n is your number to test :D) • If you use an inline if statement ( IIf(..) ), integer division (\ ), and change the n>10 to n>9, then you can get this down to an immediate window function worth 97 bytes. n=[A1]:While n>9:e=Right(n,1):n=Left(n,Len(n)-1):n=IIf(e/2=e\2,n*2+1,n\e+n Mod e):c=c+1:Wend:?n;c Feb 15 '19 at 19:28 # Swift 4, 112104100 93 bytes var n=Int(readLine()!)!,r=0,p=n%10;while n>9{n=[n/10*2+1,n/10/p+n/10%p][n%2];r+=1};print(n,r)  Try it online! Prints n r • -7 (Thanks to Mr. Xcoder) • Very nice first answer (+1)! var n=Int(readLine()!)!,r=0,p=n%10;while n>9{n=[n/10*2+1,n/10/p+n/10%p][n%2];r+=1};print(n,r) should save you 7 bytes (golfing the conditional). Sep 3 '18 at 12:24 # Jelly, 19 bytes dd/Sɗ:Ḥ‘ɗḂ?Ƭ⁵ṖṖṪ,LƊ  Try it online! # Common Lisp, 145 bytes A function which takes the n provided and returns the list (n r). Lisp syntax is fun. (defun f(n &optional(r 0))(if(< n 10)(,n,r)(let((h(floor n 10))(l(mod n 10)))(if(evenp l)(f(1+(* 2 h))(1+ r))(f(+(mod h l)(floor h l))(1+ r)))))  • Welcome to the site =D Sep 2 '18 at 12:48 • Thankyou very much, Luis. – user82200 Sep 3 '18 at 8:46 ## Haskell, 78 bytes f n|n<10=(n,0)|(d,m)<-divMod n 10=(1+)<$>f(last\$2*d+1:[div d m+mod d m|odd n])


Try it online!

# 8086 machine code, 35 bytes

00000000  31 c9 31 d2 bb 0a 00 f7  f3 85 c0 74 14 f6 c2 01  |1.1........t....|
00000010  75 05 d1 e0 40 e2 eb 89  d3 88 f2 f7 f3 01 d0 e2  |u...@...........|
00000020  e1 f7 d9                                          |...|
00000023


Input: AX = n
Output: DX = o, CX = r

Assembled from:

        xor cx, cx
next:   xor dx, dx
mov bx, 10
div bx
test ax, ax
jz done
test dl, 1
jnz odd
shl ax, 1
inc ax
loop next
odd:    mov bx, dx
mov dl, dh
div bx
loop next
done:   neg cx


# Red, 131 bytes

func[n][s: 0 while[n > 9][r: do form take/last t: form n t: do t
n: either r % 2 = 1[t / r +(t % r)][2 * t + 1]s: s + 1]print[n s]]


Try it online!

f: func [ n ] [
s: 0
while [ n > 9 ] [
r: do form take/last t: form n
t: do t
n: either r % 2 = 1
[ t / r + (t % r) ]
[ 2 * t + 1 ]
s: s + 1
]
print [ n s ]
]
`