Back
Close

continued fractions

Statement

 Goal

A simple continued fraction is a way to represent any real number with nested fractions like this :

                 1

N = a0 +--------------------

                      1

           a1 + ------------

                         1

                  a2 + -----

                        a3+..


where the ai are strictly positive integers, except a0 which can be negative if N is negative or be zero if 0<=N<1 .

Now an alternative representation of N is [a0,a1,a2,....]

Irrational numbers like "π" or "e" have an infinite number of "ai", whereas rational could be finite.

For example here is the building of π :

π = 3.141592...
π = 3 + 0.141592...
π = 3 + 1 / 7.062513...
π = 3 + 1 / ( 7 + 0.062513...)
π = 3 + 1 / ( 7 + 1 / 15.99...)
π = 3 + 1 / ( 7 + 1 / (15 + 0.99...))

So π = [3;7,15,...]

Providing a real number N and an integer L, you must return the truncated sequence of length L representing the number N
Input
Line 1: a double-precision floating point number N
Line 2: the length L of the sequence to return
Output
Line 1: the truncated sequence respecting format : [1,2,3,...]
Constraints
-10 < N < 10
0 < L <= 10
The sequence does not terminate sooner than L.
Example
Input
3.1415926535
5
Output
[3,7,15,1,292]

Game modes
Fastest, Shortest

Test cases
pi Test
Input
3.1415926535 5
Output
[3,7,15,1,292]

Validator 1 Validator
Input
3.14159265359 9
Output
[3,7,15,1,292,1,1,1,2]

square root of 2 Test
Input
1.41421356237 5
Output
[1,2,2,2,2]

Validator 2 Validator
Input
1.41421356237 9
Output
[1,2,2,2,2,2,2,2,2]

fraction Test
Input
0.33333333333 2
Output
[0,3]

Validator 3 Validator
Input
0.17910447761 6
Output
[0,5,1,1,2,2]

zero Test
Input
0 1
Output
[0]

Validator 4 Validator
Input
0 1
Output
[0]

negative Test
Input
-0.33333333333 3
Output
[-1,1,2]

Validator 5 Validator
Input
-1.41666666667 6
Output
[-2,1,1,2,1,1]

Solution language

Solution

Stub generator input