From 42c6514828a873251e5067831f43a4c22e58ac7e Mon Sep 17 00:00:00 2001 From: Rhayllon Date: Mon, 31 Oct 2022 21:27:51 -0300 Subject: [PATCH] Refactor(Desafio2.4): Muda o codigo para calcular fibonacci de maneira mais peformatica --- 04-fibonacci/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/04-fibonacci/index.js b/04-fibonacci/index.js index 34e2e43..f316e95 100644 --- a/04-fibonacci/index.js +++ b/04-fibonacci/index.js @@ -3,5 +3,11 @@ export function fibonacci(value) { if(value <= 1){ return value } - return fibonacci(value - 1) + fibonacci(value - 2) + let v1 = 0, v2 = 1, v3 = 1; + for (let i = 2; i <= value ; i++) { + v3 = v1 + v2; + v1 = v2; + v2 = v3 + } + return v3; } -- 2.34.1