Greetings, I was writing a Python code that received a number, and returned the first prime number less than or equal to that number.
So (I created a function called "major_primo"), which does the following:
largest_primo (100) - returns 97 highest_primo (23) - returns 23
The code works, but I think it got really big because I needed to create two additional functions, can someone show me a simpler way to do this? The code is as follows:
def primos_maiores_que_10(n):
i=2
while i<10:
if(n%i==0):
return "Não é primo."
i=i+1
return 1
def primos_menores_que_10(n):
if n==2 or n==3 or n==5 or n==7:
return 1
else:
return "Não é primo."
def maior_primo(n):
x=0
while n<2:
n=int(input("Digite o valor de n>= 2."))
while n>1:
if n>10:
x=primos_maiores_que_10(n)
if x==1:
return n
n=n-1
else:
x=primos_menores_que_10(n)
if x==1:
return n
n=n-1