[Codewars] Perimeter of squares in a rectangle

Perimeter of squares in a rectangle

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
typedef unsigned long long ull;
class SumFct
{
public:
static ull perimeter(int n) {
if(n == 0) return 1ull;
ull res = 0ull, fibo1 = 1, fibo2 = 1;
for(ull i = 1; i < n; i++) {
res += fibo1 * 4;
ull fibo3 = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibo3;
}
return res + 4 * fibo1 + 4 * fibo2;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/01/PS/Codewars/perimeter-of-squares-in-a-rectangle/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.