Type the maior_primo
function that receives an integer greater than or equal to 2 as a parameter and returns the largest prime number less than or equal to the number passed to the function.
Examples of Python shell execution:
>>> maior_primo(100)
97
>>> maior_primo(7)
7
Tip: write a éPrimo(k)
function and make a loop by traversing the numbers to the given number by checking whether the number is prime or not; if it is, save to a variable. At the end of the loop, the value stored in the variable is the largest prime found.
For the IDE I did it in a way and it worked, but the exercise forces it to be shell and it does not work.
My code looks like this:
#-----------------------------------------------------
def eh_primo(n):
primo = True
i = 2
while i <= n/2 and primo:
if n % i == 0:
primo = False
n = int(input("Digite um inteiro: "))
i += 1
return primo
#-----------------------------------------------------
def maior_primo(x, y):
if eh_primo(x > y):
return x
else:
return y