Last active
May 9, 2020 21:14
-
-
Save codingscode/2fbc76f44d0061a97496f6b61fa16936 to your computer and use it in GitHub Desktop.
Programando em javascript para encontrar o maior divisor comum de uma lista de números
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var delta = 0 | |
var mdc = 0 | |
var resposta = 0 | |
const minha_lista = [20, 16, 60] // | |
function maior(matriz) { | |
return matriz.reduce(function (a, b) { | |
return Math.max(a, b) | |
}) | |
} | |
function resto0(numero, matriz, p) { | |
if (numero % (maior(matriz) - p) == 0) { | |
return true | |
} | |
else { | |
return false | |
} | |
} | |
function achar_mdc(matriz, p) { | |
return (matriz).every((cada, indice) => { | |
return resto0(cada, matriz, p) | |
}) | |
} | |
function tentativas(matriz, p) { | |
while (achar_mdc(matriz, p) == false) { | |
mdc = maior(matriz) - p | |
p += 1 | |
} | |
return mdc | |
} | |
tentativas(minha_lista, delta) | |
resposta = mdc - 1 | |
console.log('maior elemento : ' + maior(minha_lista)) | |
console.log('mdc: ' + resposta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment