-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerimeterOfSquaresInARectangle.js
More file actions
56 lines (51 loc) · 1.33 KB
/
Copy pathPerimeterOfSquaresInARectangle.js
File metadata and controls
56 lines (51 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Description:
*
* The drawing shows 6 squares the sides
* of which have a length of 1, 1, 2, 3, 5, 8. It's easy
* to see that the sum of the perimeters of these squares
* is : 4 * (1 + 1 + 2 + 3 + 5 + 8) = 4 * 20 = 80
*
* Could you give the sum of the perimeters
* of all the squares in a rectangle when there
* are n + 1 squares disposed in the same manner as in the drawing:
*
* alternative text
*
* Hint: See Fibonacci sequence
* Ref: http://oeis.org/A000045
*
* The function perimeter has for parameter
* n where n + 1 is the number of squares
* (they are numbered from 0 to n) and returns
* the total perimeter of all the squares.
*
* perimeter(5) should return 80
* perimeter(7) should return 216
*
* Kata URL: https://www.codewars.com/kata/559a28007caad2ac4e000083
*
*
*/
function perimeter(n) {
return FibonacciSum(n + 1) * 4;
}
function FibonacciSum(n) {
const FibonacciSequence = [];
let result = 0;
for (let i = 0; i < n; i++) {
if (i === 0) {
FibonacciSequence.unshift(parseInt(1));
} else if (i === 1) {
FibonacciSequence.unshift(parseInt(1));
} else {
FibonacciSequence.push(
FibonacciSequence[i - 2] + FibonacciSequence[i - 1]
);
}
}
for (let i = 0; i < FibonacciSequence.length; i++) {
result += FibonacciSequence[i];
}
return result;
}