Extract and Divide
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
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.
code-golf number arithmetic division
 |Â
show 1 more comment
up vote
7
down vote
favorite
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.
code-golf number arithmetic division
I'm pretty sure that most answers from here can be trivially modified to answer this question.
– Rushabh Mehta
Sep 1 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.
– DimChtz
Sep 1 at 16:06
1
Do we enter step 2 if n<10?
– AlexRacer
Sep 1 at 16:55
@Arnauld That is not the same for n=2 and n=4.
– AlexRacer
Sep 1 at 17:00
1
Yes. It's much clearer now. :)
– Arnauld
Sep 1 at 23:42
 |Â
show 1 more comment
up vote
7
down vote
favorite
up vote
7
down vote
favorite
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.
code-golf number arithmetic division
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.
code-golf number arithmetic division
edited Sep 2 at 12:12
asked Sep 1 at 15:26


DimChtz
601111
601111
I'm pretty sure that most answers from here can be trivially modified to answer this question.
– Rushabh Mehta
Sep 1 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.
– DimChtz
Sep 1 at 16:06
1
Do we enter step 2 if n<10?
– AlexRacer
Sep 1 at 16:55
@Arnauld That is not the same for n=2 and n=4.
– AlexRacer
Sep 1 at 17:00
1
Yes. It's much clearer now. :)
– Arnauld
Sep 1 at 23:42
 |Â
show 1 more comment
I'm pretty sure that most answers from here can be trivially modified to answer this question.
– Rushabh Mehta
Sep 1 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.
– DimChtz
Sep 1 at 16:06
1
Do we enter step 2 if n<10?
– AlexRacer
Sep 1 at 16:55
@Arnauld That is not the same for n=2 and n=4.
– AlexRacer
Sep 1 at 17:00
1
Yes. It's much clearer now. :)
– Arnauld
Sep 1 at 23:42
I'm pretty sure that most answers from here can be trivially modified to answer this question.
– Rushabh Mehta
Sep 1 at 16:00
I'm pretty sure that most answers from here can be trivially modified to answer this question.
– Rushabh Mehta
Sep 1 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.– DimChtz
Sep 1 at 16:06
@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
Sep 1 at 16:06
1
1
Do we enter step 2 if n<10?
– AlexRacer
Sep 1 at 16:55
Do we enter step 2 if n<10?
– AlexRacer
Sep 1 at 16:55
@Arnauld That is not the same for n=2 and n=4.
– AlexRacer
Sep 1 at 17:00
@Arnauld That is not the same for n=2 and n=4.
– AlexRacer
Sep 1 at 17:00
1
1
Yes. It's much clearer now. :)
– Arnauld
Sep 1 at 23:42
Yes. It's much clearer now. :)
– Arnauld
Sep 1 at 23:42
 |Â
show 1 more comment
17 Answers
17
active
oldest
votes
up vote
6
down vote
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!
2
Save some bytes by getting rid of second "if", reversingn<10
condition and using%/%
in d definition : TIO
– Kirill L.
Sep 1 at 20:24
add a comment |Â
up vote
4
down vote
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!
1
Since2
divides10
, isn%10%2
not equivalent ton%2
?
– Jonathan Frech
Sep 1 at 23:47
1
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)
– mathmandan
Sep 2 at 21:16
Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP.
– Mr. Xcoder
Sep 2 at 21:17
add a comment |Â
up vote
3
down vote
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 ]
add a comment |Â
up vote
3
down vote
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!
add a comment |Â
up vote
3
down vote
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)
Whis is my first golfcode :)
– Rustem B.
Sep 1 at 19:11
Welcome to PPCG!
– Giuseppe
Sep 1 at 19:45
@Giuseppe thanks
– Rustem B.
Sep 1 at 19:49
1
Save a byte using<1
rather than==0
.
– Mr. Xcoder
Sep 1 at 20:50
add a comment |Â
up vote
2
down vote
Ruby, 61 bytes
f=->n,r=0n>9?(a=n%10;n/=10;f[a%2<1?n-~n:n/a+n%a,r+1]):[n,r]
Try it online!
add a comment |Â
up vote
2
down vote
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.
add a comment |Â
up vote
2
down vote
Perl 6, 61 bytes
tail kv $_,0!!$0*2+1...!/(.+)(.)/: 2
Try it online!
Returns (r, o).
add a comment |Â
up vote
2
down vote
Brachylog, 43 bytes
Feels messy and bad but here it is anyway
;0hl1&
Try it online!
add a comment |Â
up vote
2
down vote
Swift 4, 112 104 100 93 bytes
var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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>9n=[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
Sep 3 at 12:24
@Mr.Xcoder Thanks!
– paper1111
Sep 4 at 12:28
add a comment |Â
up vote
1
down vote
Jelly, 19 bytes
dd/Sɗ:Ḥ‘ɗḂ?ƬâµṖṖṪ,LƊ
Try it online!
add a comment |Â
up vote
1
down vote
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)))))
1
Welcome to the site =D
– Luis felipe De jesus Munoz
Sep 2 at 12:48
Thankyou very much, Luis.
– theemacsshibe
Sep 3 at 8:46
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
up vote
1
down vote
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)
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
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 ]
]
add a comment |Â
up vote
-2
down vote
C++, 65 bytes
Assume that n, r and o variables are already defined above :)
r=0;doint 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...
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
Sep 1 at 23:37
You may also find TIO a useful tool to both test your submission and auto-generate your post.
– Jonathan Frech
Sep 1 at 23:41
Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it.
– Jonathan Frech
Sep 5 at 13:24
add a comment |Â
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
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!
2
Save some bytes by getting rid of second "if", reversingn<10
condition and using%/%
in d definition : TIO
– Kirill L.
Sep 1 at 20:24
add a comment |Â
up vote
6
down vote
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!
2
Save some bytes by getting rid of second "if", reversingn<10
condition and using%/%
in d definition : TIO
– Kirill L.
Sep 1 at 20:24
add a comment |Â
up vote
6
down vote
up vote
6
down vote
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!
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!
answered Sep 1 at 19:28
duckmayr
43115
43115
2
Save some bytes by getting rid of second "if", reversingn<10
condition and using%/%
in d definition : TIO
– Kirill L.
Sep 1 at 20:24
add a comment |Â
2
Save some bytes by getting rid of second "if", reversingn<10
condition and using%/%
in d definition : TIO
– Kirill L.
Sep 1 at 20:24
2
2
Save some bytes by getting rid of second "if", reversing
n<10
condition and using %/%
in d definition : TIO– Kirill L.
Sep 1 at 20:24
Save some bytes by getting rid of second "if", reversing
n<10
condition and using %/%
in d definition : TIO– Kirill L.
Sep 1 at 20:24
add a comment |Â
up vote
4
down vote
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!
1
Since2
divides10
, isn%10%2
not equivalent ton%2
?
– Jonathan Frech
Sep 1 at 23:47
1
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)
– mathmandan
Sep 2 at 21:16
Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP.
– Mr. Xcoder
Sep 2 at 21:17
add a comment |Â
up vote
4
down vote
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!
1
Since2
divides10
, isn%10%2
not equivalent ton%2
?
– Jonathan Frech
Sep 1 at 23:47
1
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)
– mathmandan
Sep 2 at 21:16
Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP.
– Mr. Xcoder
Sep 2 at 21:17
add a comment |Â
up vote
4
down vote
up vote
4
down vote
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!
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!
edited Sep 2 at 21:19
answered Sep 1 at 16:20


Mr. Xcoder
30.3k758193
30.3k758193
1
Since2
divides10
, isn%10%2
not equivalent ton%2
?
– Jonathan Frech
Sep 1 at 23:47
1
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)
– mathmandan
Sep 2 at 21:16
Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP.
– Mr. Xcoder
Sep 2 at 21:17
add a comment |Â
1
Since2
divides10
, isn%10%2
not equivalent ton%2
?
– Jonathan Frech
Sep 1 at 23:47
1
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)
– mathmandan
Sep 2 at 21:16
Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP.
– Mr. Xcoder
Sep 2 at 21:17
1
1
Since
2
divides 10
, is n%10%2
not equivalent to n%2
?– Jonathan Frech
Sep 1 at 23:47
Since
2
divides 10
, is n%10%2
not equivalent to n%2
?– Jonathan Frech
Sep 1 at 23:47
1
1
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)
– mathmandan
Sep 2 at 21:16
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)
– mathmandan
Sep 2 at 21:16
Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP.
– Mr. Xcoder
Sep 2 at 21:17
Somehow Jonathan's message did not appear in my inbox... Indeed, you're right, editing ASAP.
– Mr. Xcoder
Sep 2 at 21:17
add a comment |Â
up vote
3
down vote
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 ]
add a comment |Â
up vote
3
down vote
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 ]
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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 ]
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 ]
edited Sep 1 at 16:45
answered Sep 1 at 15:58


Arnauld
63.6k580268
63.6k580268
add a comment |Â
add a comment |Â
up vote
3
down vote
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!
add a comment |Â
up vote
3
down vote
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!
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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!
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!
edited Sep 1 at 17:37
answered Sep 1 at 16:51
Okx
12k27100
12k27100
add a comment |Â
add a comment |Â
up vote
3
down vote
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)
Whis is my first golfcode :)
– Rustem B.
Sep 1 at 19:11
Welcome to PPCG!
– Giuseppe
Sep 1 at 19:45
@Giuseppe thanks
– Rustem B.
Sep 1 at 19:49
1
Save a byte using<1
rather than==0
.
– Mr. Xcoder
Sep 1 at 20:50
add a comment |Â
up vote
3
down vote
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)
Whis is my first golfcode :)
– Rustem B.
Sep 1 at 19:11
Welcome to PPCG!
– Giuseppe
Sep 1 at 19:45
@Giuseppe thanks
– Rustem B.
Sep 1 at 19:49
1
Save a byte using<1
rather than==0
.
– Mr. Xcoder
Sep 1 at 20:50
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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)
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)
edited Sep 1 at 21:33
answered Sep 1 at 19:09
Rustem B.
513
513
Whis is my first golfcode :)
– Rustem B.
Sep 1 at 19:11
Welcome to PPCG!
– Giuseppe
Sep 1 at 19:45
@Giuseppe thanks
– Rustem B.
Sep 1 at 19:49
1
Save a byte using<1
rather than==0
.
– Mr. Xcoder
Sep 1 at 20:50
add a comment |Â
Whis is my first golfcode :)
– Rustem B.
Sep 1 at 19:11
Welcome to PPCG!
– Giuseppe
Sep 1 at 19:45
@Giuseppe thanks
– Rustem B.
Sep 1 at 19:49
1
Save a byte using<1
rather than==0
.
– Mr. Xcoder
Sep 1 at 20:50
Whis is my first golfcode :)
– Rustem B.
Sep 1 at 19:11
Whis is my first golfcode :)
– Rustem B.
Sep 1 at 19:11
Welcome to PPCG!
– Giuseppe
Sep 1 at 19:45
Welcome to PPCG!
– Giuseppe
Sep 1 at 19:45
@Giuseppe thanks
– Rustem B.
Sep 1 at 19:49
@Giuseppe thanks
– Rustem B.
Sep 1 at 19:49
1
1
Save a byte using
<1
rather than ==0
.– Mr. Xcoder
Sep 1 at 20:50
Save a byte using
<1
rather than ==0
.– Mr. Xcoder
Sep 1 at 20:50
add a comment |Â
up vote
2
down vote
Ruby, 61 bytes
f=->n,r=0n>9?(a=n%10;n/=10;f[a%2<1?n-~n:n/a+n%a,r+1]):[n,r]
Try it online!
add a comment |Â
up vote
2
down vote
Ruby, 61 bytes
f=->n,r=0n>9?(a=n%10;n/=10;f[a%2<1?n-~n:n/a+n%a,r+1]):[n,r]
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Ruby, 61 bytes
f=->n,r=0n>9?(a=n%10;n/=10;f[a%2<1?n-~n:n/a+n%a,r+1]):[n,r]
Try it online!
Ruby, 61 bytes
f=->n,r=0n>9?(a=n%10;n/=10;f[a%2<1?n-~n:n/a+n%a,r+1]):[n,r]
Try it online!
edited Sep 1 at 18:21
answered Sep 1 at 18:10
Kirill L.
2,4061116
2,4061116
add a comment |Â
add a comment |Â
up vote
2
down vote
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.
add a comment |Â
up vote
2
down vote
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.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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.
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.
answered Sep 1 at 20:50
Neil
75.1k744170
75.1k744170
add a comment |Â
add a comment |Â
up vote
2
down vote
Perl 6, 61 bytes
tail kv $_,0!!$0*2+1...!/(.+)(.)/: 2
Try it online!
Returns (r, o).
add a comment |Â
up vote
2
down vote
Perl 6, 61 bytes
tail kv $_,0!!$0*2+1...!/(.+)(.)/: 2
Try it online!
Returns (r, o).
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Perl 6, 61 bytes
tail kv $_,0!!$0*2+1...!/(.+)(.)/: 2
Try it online!
Returns (r, o).
Perl 6, 61 bytes
tail kv $_,0!!$0*2+1...!/(.+)(.)/: 2
Try it online!
Returns (r, o).
edited Sep 1 at 22:44
answered Sep 1 at 22:01
nwellnhof
3,503714
3,503714
add a comment |Â
add a comment |Â
up vote
2
down vote
Brachylog, 43 bytes
Feels messy and bad but here it is anyway
;0hl1&
Try it online!
add a comment |Â
up vote
2
down vote
Brachylog, 43 bytes
Feels messy and bad but here it is anyway
;0hl1&
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Brachylog, 43 bytes
Feels messy and bad but here it is anyway
;0hl1&
Try it online!
Brachylog, 43 bytes
Feels messy and bad but here it is anyway
;0hl1&
Try it online!
answered Sep 2 at 9:10


Kroppeb
90628
90628
add a comment |Â
add a comment |Â
up vote
2
down vote
Swift 4, 112 104 100 93 bytes
var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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>9n=[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
Sep 3 at 12:24
@Mr.Xcoder Thanks!
– paper1111
Sep 4 at 12:28
add a comment |Â
up vote
2
down vote
Swift 4, 112 104 100 93 bytes
var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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>9n=[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
Sep 3 at 12:24
@Mr.Xcoder Thanks!
– paper1111
Sep 4 at 12:28
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Swift 4, 112 104 100 93 bytes
var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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)
Swift 4, 112 104 100 93 bytes
var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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)
edited Sep 4 at 12:27
answered Sep 2 at 7:52


paper1111
1214
1214
Very nice first answer (+1)!var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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
Sep 3 at 12:24
@Mr.Xcoder Thanks!
– paper1111
Sep 4 at 12:28
add a comment |Â
Very nice first answer (+1)!var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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
Sep 3 at 12:24
@Mr.Xcoder Thanks!
– paper1111
Sep 4 at 12:28
Very nice first answer (+1)!
var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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
Sep 3 at 12:24
Very nice first answer (+1)!
var n=Int(readLine()!)!,r=0,p=n%10;while n>9n=[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
Sep 3 at 12:24
@Mr.Xcoder Thanks!
– paper1111
Sep 4 at 12:28
@Mr.Xcoder Thanks!
– paper1111
Sep 4 at 12:28
add a comment |Â
up vote
1
down vote
Jelly, 19 bytes
dd/Sɗ:Ḥ‘ɗḂ?ƬâµṖṖṪ,LƊ
Try it online!
add a comment |Â
up vote
1
down vote
Jelly, 19 bytes
dd/Sɗ:Ḥ‘ɗḂ?ƬâµṖṖṪ,LƊ
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Jelly, 19 bytes
dd/Sɗ:Ḥ‘ɗḂ?ƬâµṖṖṪ,LƊ
Try it online!
Jelly, 19 bytes
dd/Sɗ:Ḥ‘ɗḂ?ƬâµṖṖṪ,LƊ
Try it online!
answered Sep 1 at 21:08


Erik the Outgolfer
29.4k42698
29.4k42698
add a comment |Â
add a comment |Â
up vote
1
down vote
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)))))
1
Welcome to the site =D
– Luis felipe De jesus Munoz
Sep 2 at 12:48
Thankyou very much, Luis.
– theemacsshibe
Sep 3 at 8:46
add a comment |Â
up vote
1
down vote
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)))))
1
Welcome to the site =D
– Luis felipe De jesus Munoz
Sep 2 at 12:48
Thankyou very much, Luis.
– theemacsshibe
Sep 3 at 8:46
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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)))))
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)))))
edited Sep 2 at 6:56
answered Sep 2 at 6:50


theemacsshibe
214
214
1
Welcome to the site =D
– Luis felipe De jesus Munoz
Sep 2 at 12:48
Thankyou very much, Luis.
– theemacsshibe
Sep 3 at 8:46
add a comment |Â
1
Welcome to the site =D
– Luis felipe De jesus Munoz
Sep 2 at 12:48
Thankyou very much, Luis.
– theemacsshibe
Sep 3 at 8:46
1
1
Welcome to the site =D
– Luis felipe De jesus Munoz
Sep 2 at 12:48
Welcome to the site =D
– Luis felipe De jesus Munoz
Sep 2 at 12:48
Thankyou very much, Luis.
– theemacsshibe
Sep 3 at 8:46
Thankyou very much, Luis.
– theemacsshibe
Sep 3 at 8:46
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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!
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!
answered Sep 2 at 20:48
nimi
29.9k31881
29.9k31881
add a comment |Â
add a comment |Â
up vote
1
down vote
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)
add a comment |Â
up vote
1
down vote
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)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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)
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)
edited Sep 2 at 23:42
answered Sep 2 at 21:27
seadoggie01
1316
1316
add a comment |Â
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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
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
edited Sep 3 at 3:24
answered Sep 1 at 17:00
user5434231
1,556512
1,556512
add a comment |Â
add a comment |Â
up vote
1
down vote
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 ]
]
add a comment |Â
up vote
1
down vote
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 ]
]
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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 ]
]
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 ]
]
answered Sep 4 at 9:13
Galen Ivanov
4,7571829
4,7571829
add a comment |Â
add a comment |Â
up vote
-2
down vote
C++, 65 bytes
Assume that n, r and o variables are already defined above :)
r=0;doint 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...
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
Sep 1 at 23:37
You may also find TIO a useful tool to both test your submission and auto-generate your post.
– Jonathan Frech
Sep 1 at 23:41
Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it.
– Jonathan Frech
Sep 5 at 13:24
add a comment |Â
up vote
-2
down vote
C++, 65 bytes
Assume that n, r and o variables are already defined above :)
r=0;doint 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...
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
Sep 1 at 23:37
You may also find TIO a useful tool to both test your submission and auto-generate your post.
– Jonathan Frech
Sep 1 at 23:41
Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it.
– Jonathan Frech
Sep 5 at 13:24
add a comment |Â
up vote
-2
down vote
up vote
-2
down vote
C++, 65 bytes
Assume that n, r and o variables are already defined above :)
r=0;doint 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...
C++, 65 bytes
Assume that n, r and o variables are already defined above :)
r=0;doint 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...
edited Sep 1 at 23:01
answered Sep 1 at 22:42
Jin X
11
11
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
Sep 1 at 23:37
You may also find TIO a useful tool to both test your submission and auto-generate your post.
– Jonathan Frech
Sep 1 at 23:41
Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it.
– Jonathan Frech
Sep 5 at 13:24
add a comment |Â
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
Sep 1 at 23:37
You may also find TIO a useful tool to both test your submission and auto-generate your post.
– Jonathan Frech
Sep 1 at 23:41
Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it.
– Jonathan Frech
Sep 5 at 13:24
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
Sep 1 at 23:37
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
Sep 1 at 23:37
You may also find TIO a useful tool to both test your submission and auto-generate your post.
– Jonathan Frech
Sep 1 at 23:41
You may also find TIO a useful tool to both test your submission and auto-generate your post.
– Jonathan Frech
Sep 1 at 23:41
Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it.
– Jonathan Frech
Sep 5 at 13:24
Your answer in its current form is invalid. Please either edit it to conform to our default I/O or delete it.
– Jonathan Frech
Sep 5 at 13:24
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f171555%2fextract-and-divide%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I'm pretty sure that most answers from here can be trivially modified to answer this question.
– Rushabh Mehta
Sep 1 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.– DimChtz
Sep 1 at 16:06
1
Do we enter step 2 if n<10?
– AlexRacer
Sep 1 at 16:55
@Arnauld That is not the same for n=2 and n=4.
– AlexRacer
Sep 1 at 17:00
1
Yes. It's much clearer now. :)
– Arnauld
Sep 1 at 23:42