diff --git a/src/factorial.js b/src/factorial.js index 4f3ae70..f16f3c5 100644 --- a/src/factorial.js +++ b/src/factorial.js @@ -1,5 +1,18 @@ const factorial = (number) => { - // your code here + // Excepcion para 0 + if(number == 0){ + return 1; + } + + var res = number; + + while(number>1){ + res = res*(number-1); + number--; + } + + return res; + // \_0> } module.exports = factorial; \ No newline at end of file diff --git a/src/fibonacci.js b/src/fibonacci.js index ea3270f..0744afa 100644 --- a/src/fibonacci.js +++ b/src/fibonacci.js @@ -1,5 +1,17 @@ const fibonacci = (n) => { - // your code here + var res = []; + var a = b = 1; + + res.push(a) + + for(var i=0; i<(n-1); i++){ + b = a+b; + a = b-a; + res.push(a); + } + + return res; + // \_0> } module.exports = fibonacci; \ No newline at end of file diff --git a/src/primalidad.js b/src/primalidad.js index 8bdb849..9f0d112 100644 --- a/src/primalidad.js +++ b/src/primalidad.js @@ -1,5 +1,27 @@ const trialDivision = (number) => { - // your code here + // Excepcion para <=1 y float + if((number <= 1) || (number%1 !== 0)){ + return false; + } + + // Probar para (n/2)+1 no tiene sentido. + if(number == 2){ + return true; + } + + var esPrimo = true; + var n = Math.floor(number/2) + + while(n>1){ + if(number%n == 0){ + esPrimo = false; + break; + } + n--; + } + + return esPrimo + // \_0> } module.exports = trialDivision; \ No newline at end of file