## Algorithm

Problem Name: beecrowd | 2166

# Square Root of 2

By M.C. Pinto, UNILA Brazil

Timelimit: 1

The method of periodic continued fractions is one of the many ways to calculate the square root of a natural number. This method uses as denominator a repetition for fractions. This repetition can be done by a fixed number of times.

For example, by repeating 2 times the continued fraction to calculate the square root of 2, we have the following equation.

Your task is to calculate the approximate value of square root of 2 given the number N of repetitions.

## Input

The input is a natural number N (0 ≤ N ≤ 100) that indicates the quantity of denominator repetitions in the continued fraction.

## Output

The output is the approximate value of the square root with 10 decimal places.

 Input Samples Output Samples 0 1.0000000000

 1 1.5

 5 1.41429

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
#include <stdio.h>

int main(void)
{
int n;
double ans=0.0;

scanf("%i", &n);

while (n)
{
ans+=2.0;
ans=1.0/ans;
--n;
}

ans+=1.0;

printf("%.10lf\n", ans);
return 0;
}
``````
Copy The Code &

Output

cmd
1.0000000000

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````
#include <cmath>
#include <ios>
#include <iostream>

int main(){
double n, sqrt2 = 1;
int i, j;
std::cin >> n;
for(i = 1,j = 1;i  <  n;i++,j++) {
sqrt2 += i + j / i;
if (i == 2) {
j --;
}
}
std::cout.precision(10);
std::cout.setf(std::ios::fixed);
std::cout << sqrt2 << std::endl;

return 0;
}
``````
Copy The Code &

Output

cmd
1.0000000000

### #3 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
const repetitions = parseInt(prompt());
var fraction = 0;
var SR2;

for (let i = 0; i  <  repetitions; i++) {
fraction = 1 / (2 + fraction);
}

SR2 = 1 + fraction;
console.log(SR2.toFixed(10));

``````
Copy The Code &

Output

cmd
1.0000000000

### #4 Code Example with Python Programming

```Code - Python Programming```

``````
def r2(n):
if n == 0:
return 2
x = 2+1/r2(n-1)
return x

n = int(input())
x = r2(n)-1
print('%.10f' % x)
``````
Copy The Code &

Output

cmd
1.0000000000