I'm having trouble solving an exercise in logic, I think I'm thinking wrong:
What I need: Given an N number show the largest prime number up to N.
The code I've made so far is this:
def maior_primo(n):
print(ePrimo(n))
def ePrimo(k):
aux = k
i = 1
divisores = 0
while(aux >= 1):
while(i <= aux):
if(aux % i == 0):
divisores += 1
i += 1
else:
i += 1
if(divisores < 3):
return aux
else:
aux -= 1
But when I run it returns me:
maior_primo(7)
7
maior_primo(8)
None
I made the two% s of% s because 1 is checking the number I reported and reducing ex: while
, will test 8 see that it has more than two divisors so I will get the number I passed and subtract 1. p>
And the other maior_primo(8)
will check for each number that decreases how many divisors it has.
Note: I can only use two functions,
while
andwhile
.