5
I was writing a Python code that received a number and returned the first prime number smaller than or equal to that number.
I created a function called maior_primo
, which does the following:
maior_primo(100)
- returns 97.maior_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 that? The code goes below:
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
This answers your question? Get the list of prime numbers smaller than N
– hkotsubo