Skip to content

Instantly share code, notes, and snippets.

@ldong
Created August 22, 2014 16:37
Show Gist options
  • Save ldong/808d5403c5e3b19f2f05 to your computer and use it in GitHub Desktop.
Save ldong/808d5403c5e3b19f2f05 to your computer and use it in GitHub Desktop.
find the next prime number in python
#!/usr/bin/env python
def main():
n = input('Find the next prime number greater great than: ')
print find_next_prime(n+1)
def find_next_prime(n):
return find_prime_in_range(n, 2*n)
def find_prime_in_range(a, b):
for p in range(a, b):
for i in range(2, p):
if p % i == 0:
break
else:
return p
return None
if __name__ == '__main__':
main()
@rohanchavan160398
Copy link

sir when u are using print statements why are u not using brackets. ?

@anny17
Copy link

anny17 commented Aug 23, 2018

Optimize it!!

@gsvn32
Copy link

gsvn32 commented Aug 31, 2018

Thanks a lot

@Bharathkumarraju
Copy link

Bharathkumarraju commented May 20, 2021

may not be a lot of optimization this worked for me. Thanks a lot @ldong

def main():
    n = int(input('Find the next prime number greater great than: '))
    prime=find_next_prime(n+1, 2*n)
    print("next prime is: ", prime)

def find_next_prime(a, b):
    for p in range(a, b):
        for i in range(2, p):
            if p % i == 0:
                break
        else:
            return p
    return None

if __name__ == '__main__':
    main()

@magistau
Copy link

magistau commented Nov 4, 2021

sir when u are using print statements why are u not using brackets. ?

that's because they use Python 2 where print was used without brackets (it was a keyword rather than a function)

@nagenbiswal123
Copy link

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment