Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…algorithms
  • Loading branch information
iAlgebra committed Nov 29, 2019
1 parent 95b1a75 commit b971a1a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tareas/clase-4/algoritmo-26.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Desafío de programación #26: Crear una función que reciba dos arrays de números
y retorne un nuevo array con los elementos que se encuentren en el primer array,
pero no en el segundo
Nota; Esto se llama "resta" entre conjuntos
Ejemplo:
[5,1,2,3,4] y [3,4] debería devolver [5,1,2]
*/

let arr1 = [1, 2, 3, 10, 5, 3, 14];
let arr2 = [-1, 4, 5, 6, 14];

// let arr1 = [5, 1, 2, 3, 4];
// let arr2 = [3, 4];

function subtraction(arr1, arr2) {
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr2[j] === arr1[i]) {
arr1.splice(i, 1);
}
}
}
return arr1;
}

console.log(subtraction(arr1, arr2));
26 changes: 26 additions & 0 deletions tareas/clase-4/algoritmo-27.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Desafío de programación #27: Crear una función que reciba un array de números
como argumento y retorne un array con los elementos distintos
Ejemplo:
[1,2,3,4,5,4,3,2,1,0] debería retornar [1,2,3,4,5,0]
*/

const list = [1, 2, 3, 4, 5, 4, 3, 2, 1, 0];
const list2 = [1, 2, 3, 6, -1, 2, 9, 7, 10, -1, 100];

function removeRepeated(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (i === j) {
continue;
}

if (arr[i] === arr[j]) {
arr.splice(j, 1);
}
}
}
return arr;
}

console.log(removeRepeated(list2));
34 changes: 34 additions & 0 deletions tareas/clase-4/algoritmo-28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Desafío de programación #28: Calculate the sum of first 100 prime numbers
*/

function isPrimeNumber(n) {
if (n < 2) {
return false;
} else if (n === 2) {
return true;
} else {
for (let i = 2; i < n; i++) {
if (n % i === 0) {
return false;
}
}
return true;
}
}

function sumPrimeNumbers(numbersToSum) {
let primeCounter = 0;
let n = 0;
let sumResult = 0;
while (primeCounter < numbersToSum) {
if (isPrimeNumber(n)) {
primeCounter++;
sumResult += n;
}
n++;
}
return sumResult;
}

console.log(sumPrimeNumbers(100));
44 changes: 44 additions & 0 deletions tareas/clase-4/algoritmo-29.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Desafío de programación #29: Print the distance between the first 100 prime numbers
*/

function isPrimeNumber(n) {
if (n < 2) {
return false;
} else if (n === 2) {
return true;
} else {
for (let i = 2; i < n; i++) {
if (n % i === 0) {
return false;
}
}
return true;
}
}

function printPrimeNumbers(numbersToPrint) {
let primeCounter = 0;
let n = 0;
let primeNumbers = [];
while (primeCounter < numbersToPrint) {
if (isPrimeNumber(n)) {
primeCounter++;
primeNumbers.push(n);
}
n++;
}
return primeNumbers;
}

console.log(printPrimeNumbers(100));

function distances() {
const primeNumbers = printPrimeNumbers(100);
for (let i = 0; i < primeNumbers.length - 1; i++) {
console.log(primeNumbers[i + 1] + ' - ' + primeNumbers[i] + ' = ' +
(primeNumbers[i + 1] - primeNumbers[i]));
}
}

distances();

0 comments on commit b971a1a

Please sign in to comment.