Python prime numbers | while [duplicate]

0

I have the following problem:

Write the largest_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

Note that

largest_primo (100) should return 97

largest_primo (7) should return 7

Tip : write a function ePrimo (k) 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.

My solution would be:

def éPrimo (k):
    div = 2
    while k % div != 0 and div <= k:   
        div = div + 1
        if k % div == 0:
            k = k - 1
        else:
            print (k)

I am very convinced that I am right, I am still at a very beginner level and if anyone can give me a light on these basic parameters would be of great help.

    
asked by anonymous 17.10.2017 / 17:09

1 answer

2
def ehPrimo(x):
    if x >= 2:
        for y in range( 2, x ):
            if not ( x % y ):
                return False
    else:
        return False

    return True

num = int(input("Entre com um numero: "))

for n in range( num, 0, -1 ):
    if ehPrimo(n) :
        print n
        break
    
17.10.2017 / 18:08