Last active
April 5, 2019 14:56
-
-
Save davidsonbrsilva/39f2e19e043552170b2da923bc16b9ef to your computer and use it in GitHub Desktop.
Implementação de testes de primalidade: fermat
This file contains 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
def fermat(number, trials=20): | |
if number > 2: | |
# Repete o teste até o número de tentativas especificado. | |
for trial in range(trials): | |
# Gera um número aleatório entre 2 e number. | |
random_int = random.randint(2, number - 2 + 1) | |
# Calcula o resto da divisão de random_int elevado a number - 1 por number. | |
remaind = random_int**(number - 1) % number | |
# number é primo se o resto da divisão for diferente de 1. | |
if remaind != 1: | |
return False | |
return True | |
elif number == 2: | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment