Extract and Divide

7

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.

DimChtz

Posted 2018-09-01T15:26:09.880

Reputation: 916

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.840

1Do 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

Answers

6

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!

duckmayr

Posted 2018-09-01T15:26:09.880

Reputation: 441

3

Save some bytes by getting rid of second "if", reversing n<10 condition and using %/% in d definition : TIO

– Kirill L. – 2018-09-01T20:24:56.257

agree with @KirillL. . Also unfortunately f= needs to be included in the bytecount since the function is called recursively. – JayCe – 2018-09-13T17:03:44.387

4

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!

Mr. Xcoder

Posted 2018-09-01T15:26:09.880

Reputation: 39 774

1Since 2 divides 10, is n%10%2 not equivalent to n%2? – Jonathan Frech – 2018-09-01T23:47:34.280

1Great 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) – mathmandan – 2018-09-02T21:16:11.117

Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP. – Mr. Xcoder – 2018-09-02T21:17:17.587

3

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 ]

Arnauld

Posted 2018-09-01T15:26:09.880

Reputation: 111 334

3

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!

Okx

Posted 2018-09-01T15:26:09.880

Reputation: 15 025

3

Julia 1.0, 81 80 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)

Rustem B.

Posted 2018-09-01T15:26:09.880

Reputation: 51

Whis is my first golfcode :) – Rustem B. – 2018-09-01T19:11:00.477

Welcome to PPCG! – Giuseppe – 2018-09-01T19:45:56.310

@Giuseppe thanks – Rustem B. – 2018-09-01T19:49:55.117

1Save a byte using <1 rather than ==0. – Mr. Xcoder – 2018-09-01T20:50:37.490

2

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!

Kirill L.

Posted 2018-09-01T15:26:09.880

Reputation: 6 693

2

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.

Neil

Posted 2018-09-01T15:26:09.880

Reputation: 95 035

2

Perl 6, 61 bytes

{tail kv $_,{$1%2??$0/$1+$0%$1+|0!!$0*2+1}...{!/(.+)(.)/}: 2}

Try it online!

Returns (r, o).

nwellnhof

Posted 2018-09-01T15:26:09.880

Reputation: 10 037

2

Swift 4, 112 104 100 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)

paper1111

Posted 2018-09-01T15:26:09.880

Reputation: 121

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). – Mr. Xcoder – 2018-09-03T12:24:32.027

2

Brachylog, 43 bytes

Feels messy and bad but here it is anyway

;0{hl1&|⟨{h⟨k{t%₂0&h×₂+₁|⟨÷+%⟩}t⟩}↰₁{t+₁}⟩}

Try it online!

Kroppeb

Posted 2018-09-01T15:26:09.880

Reputation: 1 558

2

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)

seadoggie01

Posted 2018-09-01T15:26:09.880

Reputation: 181

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 – Taylor Scott – 2019-02-15T19:28:10.570

1

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
        add ax, dx
        loop next
done:   neg cx

user5434231

Posted 2018-09-01T15:26:09.880

Reputation: 1 576

1

Jelly, 19 bytes

dd/Sɗ:Ḥ‘ɗḂ?Ƭ⁵ṖṖṪ,LƊ

Try it online!

Erik the Outgolfer

Posted 2018-09-01T15:26:09.880

Reputation: 38 134

1

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)))))

user82200

Posted 2018-09-01T15:26:09.880

Reputation:

1Welcome to the site =D – Luis felipe De jesus Munoz – 2018-09-02T12:48:06.707

Thankyou very much, Luis. – None – 2018-09-03T08:46:35.657

1

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!

nimi

Posted 2018-09-01T15:26:09.880

Reputation: 34 639

1

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!

More readable:

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 ]
]

Galen Ivanov

Posted 2018-09-01T15:26:09.880

Reputation: 13 815

-2

C++, 65 bytes

Assume that n, r and o variables are already defined above :)

r=0;do{int m=n/10,d=n%10;n=n&1?m/d+m%d:m*2+1;++r;}while(n>9);o=n;

p.s. Sorry for such design of post, I have not figured out how to do it well yet...

Jin X

Posted 2018-09-01T15:26:09.880

Reputation: 1

1

Hello and welcome to PPCG. As it stands, your answer is not conforming to our default I/O, which essentially calls for either full programs or functions. Take a look at other C++ submissions to get a feel for how to construct these wrappers. Also, please do not be discouraged by your one downvote -- not knowing our rule set on your first post is fine and should not be punished. They may remove it if you fix your answer!

– Jonathan Frech – 2018-09-01T23:37:46.263

You may also find TIO a useful tool to both test your submission and auto-generate your post.

– Jonathan Frech – 2018-09-01T23:41:47.137

Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it. – Jonathan Frech – 2018-09-05T13:24:12.483