0

I created this function to return the closest prime to x (equal or above):

def primo(x):
i=2
prime=True
if x>=0:
    while i<=x**(1/2) and prime==True:
        if x%i==0:
            prime=False
        else:
            i+=1
    if (prime==True or x==2 or x==3) and x>1:
        return x
    else:
        primo(x+1)

else:
    primo(x+1)

But it only returns when x is already prime.

I have tried to put a print(x) on the same line as the return, but if x is not prime (12, for example), it only prints the number at the end, it does not return the value.

I would like the code to return the prime in every case, most especially if x is not prime (it would go around the code until x is prime).

Thanks.

pringi
  • 3,987
  • 5
  • 35
  • 45
Daniel B.
  • 9
  • 1

1 Answers1

5

You need to return the value of your recursive function call

else:
    return primo(x+1)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218