Skip to main content
2 of 3
added 2 characters in body
DavidC
  • 25.5k
  • 2
  • 53
  • 106

#Mathematica 138

Not as streamlined as alephalpha's procedure, but it was the best I've been able to produce so far.

q_~r~k_:=Nest[#+Sign@k/(2Denominator@# )&,q,Abs@k] g@d_:= Module[{l=ContinuedFraction@d,p=-1}, l[[-1]]-=1; (p=-p;# p)&/@l] h[q_]:=Fold[r,1/2,g@q] 

Testing

h[2/3] h[4/7] h[1] 

3/8
9/32
1/2

DavidC
  • 25.5k
  • 2
  • 53
  • 106