Continued Fraction of a Rational Number
Ruby, 45 43 46 bytes
f=->n{x,y=n.to_r.divmod 1;[x,*y==0?[]:f[1/y]]}
Accepts input as a string.
All test cases pass:
llama@llama:~$ for n in 860438 3.245 -4.2 -114.7802 0/11 1/42 2/7 -18/17056 -17056/18; do ruby -e 'f=->n{x,y=n.to_r.divmod 1;[x,*y==0?[]:f[1/y]]}; p f["'$n'"]'; done
[860438]
[3, 4, 12, 4]
[-5, 1, 4]
[-115, 4, 1, 1, 4, 1, 1, 5, 1, 1, 4]
[0]
[0, 42]
[0, 3, 2]
[-1, 1, 946, 1, 1, 4]
[-948, 2, 4]
Thanks to Kevin Lau for 2 bytes!
Ruby, 50 48 bytes
I noticed @Doorknob♦ beat me to a Ruby answer right before posting, and theirs is also shorter! As such this is just here for posterity now. Takes in a string or an integer (floats have rounding issues, so decimal values need to be put in as strings)
f=->s{s=s.to_r;[s.floor]+(s%1!=0?f[1/(s%1)]:[])}
JavaScript (ES6), 52 bytes
f=(n,d)=>n%d?[r=Math.floor(n/d),...f(d,n-r*d)]:[n/d]
Accepts numerator and denominator and returns an array, e.g. f(-1147802, 1e4)
returns [-115, 4, 1, 1, 4, 1, 1, 5, 1, 1, 4]
.