7
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I'm pretty sure that most answers from here can be trivially modified to answer this question. \$\endgroup\$ – Don Thousand Sep 1 '18 at 16:00
  • \$\begingroup\$ @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. \$\endgroup\$ – DimChtz Sep 1 '18 at 16:06
  • 1
    \$\begingroup\$ Do we enter step 2 if n<10? \$\endgroup\$ – AlexRacer Sep 1 '18 at 16:55
  • \$\begingroup\$ @Arnauld That is not the same for n=2 and n=4. \$\endgroup\$ – AlexRacer Sep 1 '18 at 17:00
  • 1
    \$\begingroup\$ Yes. It's much clearer now. :) \$\endgroup\$ – Arnauld Sep 1 '18 at 23:42

17 Answers 17

6
\$\begingroup\$

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!

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

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ Since 2 divides 10, is n%10%2 not equivalent to n%2? \$\endgroup\$ – Jonathan Frech Sep 1 '18 at 23:47
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ – mathmandan Sep 2 '18 at 21:16
  • \$\begingroup\$ Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP. \$\endgroup\$ – Mr. Xcoder Sep 2 '18 at 21:17
3
\$\begingroup\$

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 ]
\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Whis is my first golfcode :) \$\endgroup\$ – Rustem B. Sep 1 '18 at 19:11
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Giuseppe Sep 1 '18 at 19:45
  • \$\begingroup\$ @Giuseppe thanks \$\endgroup\$ – Rustem B. Sep 1 '18 at 19:49
  • 1
    \$\begingroup\$ Save a byte using <1 rather than ==0. \$\endgroup\$ – Mr. Xcoder Sep 1 '18 at 20:50
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Perl 6, 61 bytes

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

Try it online!

Returns (r, o).

\$\endgroup\$
2
\$\begingroup\$

Brachylog, 43 bytes

Feels messy and bad but here it is anyway

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Taylor Scott Feb 15 at 19:28
2
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Mr. Xcoder Sep 3 '18 at 12:24
1
\$\begingroup\$

Jelly, 19 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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)))))
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site =D \$\endgroup\$ – Luis felipe De jesus Munoz Sep 2 '18 at 12:48
  • \$\begingroup\$ Thankyou very much, Luis. \$\endgroup\$ – user82200 Sep 3 '18 at 8:46
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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 ]
]
\$\endgroup\$
-2
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – Jonathan Frech Sep 1 '18 at 23:37
  • \$\begingroup\$ You may also find TIO a useful tool to both test your submission and auto-generate your post. \$\endgroup\$ – Jonathan Frech Sep 1 '18 at 23:41
  • \$\begingroup\$ Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it. \$\endgroup\$ – Jonathan Frech Sep 5 '18 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.