From 6b42ae42bcdd4ea8e70139966a69df2c914aecb3 Mon Sep 17 00:00:00 2001 From: JPHITA Date: Sat, 25 Mar 2023 20:59:54 -0500 Subject: [PATCH] . --- src/factorial.js | 6 ++++-- src/fibonacci.js | 8 +++++++- src/primalidad.js | 9 ++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/factorial.js b/src/factorial.js index 4f3ae70..3689cc8 100644 --- a/src/factorial.js +++ b/src/factorial.js @@ -1,5 +1,7 @@ -const factorial = (number) => { - // your code here +const factorial = (number, r=1) => { + if (number <= 1) return r; + + return factorial(number - 1, r * number); } module.exports = factorial; \ No newline at end of file diff --git a/src/fibonacci.js b/src/fibonacci.js index ea3270f..fa48efc 100644 --- a/src/fibonacci.js +++ b/src/fibonacci.js @@ -1,5 +1,11 @@ const fibonacci = (n) => { - // your code here + let r = [0, 1]; + + for (let i = 2; i <= n; i++) { + r.push( r.at(-1) + r.at(-2) ); + } + + return r.slice(1); } module.exports = fibonacci; \ No newline at end of file diff --git a/src/primalidad.js b/src/primalidad.js index 8bdb849..9e0157b 100644 --- a/src/primalidad.js +++ b/src/primalidad.js @@ -1,5 +1,12 @@ const trialDivision = (number) => { - // your code here + if (number == 2) return true; + if (number <= 1 || number%2 == 0) return false; + + for (let i = 3; i <= Math.ceil(Math.sqrt(number)); i += 2) { + if (number % i == 0) return false; + } + + return true; } module.exports = trialDivision; \ No newline at end of file