Jump to content

Hi guys,

I'm learning python and having a little trouble with my program.

It's supposed to generate the next prime number when the user hits enter. Instead it is just returning the odd numbers.

 


number = 5

def generate_prime(num):
    for i in range(2,num):
        if (num % i) == 0:
            num += 1
            generate_prime(num)
        return(num)

while True:
    input("Hit enter to get the next prime number.")
    print(generate_prime(number))
    number = generate_prime(number) + 1

 

I've looked at prime number check examples and the formula seems correct. Any pointers would be helpful as i'm stuck and a good hint might get me going.

 

Thanks.

Link to comment
https://linustechtips.com/topic/700233-python-prime-number-generator/
Share on other sites

Link to post
Share on other sites

You have num +=1 which changes the value of num and you are returning that changed value; hence always odd.

change

18 minutes ago, nickspacemonkey said:

num += 1            

generate_prime(num)

to

18 minutes ago, nickspacemonkey said:

generate_prime(num + 1)

 

 

             ☼

ψ ︿_____︿_ψ_   

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×