Created
August 22, 2014 16:37
-
-
Save ldong/808d5403c5e3b19f2f05 to your computer and use it in GitHub Desktop.
find the next prime number in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Optimize it!!
Thanks a lot
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()
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)
Thanks a lot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sir when u are using print statements why are u not using brackets. ?