Created
December 8, 2012 17:34
-
-
Save lewtds/4241119 to your computer and use it in GitHub Desktop.
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 python3 | |
import itertools # Thư viện chuẩn của Python để xử lý các vấn đề lặp, tổ hợp, | |
# chỉnh hợp,... | |
def is_prime(num): | |
if num < 2: | |
return False | |
if num == 2: | |
return True | |
if not num % 2: | |
return False | |
i = 3 | |
while i ** 2 <= num: | |
if not num % i: | |
return False | |
i += 2 | |
return True | |
def main(): | |
# Lặp qua tất cả các chỉnh hợp chập 9 của cái cục kia. Chú ý là thứ tự | |
# chỉnh hợp của hàm permutations đưa ra đã được sort sẵn | |
for i in itertools.permutations( | |
['9', '8', '7', '6', '5', '4', '3', '2', '1', '0'], 9): | |
if i[-1] in ['0', '2', '4', '5', '6', '8']: # ký tự cuối cùng | |
continue | |
num = int(''.join(i)) # Nối string thành int | |
if is_prime(num): | |
print(num) | |
break | |
if __name__ == '__main__': | |
main() |
Cái header đấy anh để mặc định cho hết đống python code của anh mà :)
Giờ vẫn xài python2 nhiều, Ubuntu/Debian/CentOS vẫn python2 mà :p
Mấy cái thuật toán Prime này là đợt anh làm Project Euler nên mới biết.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Em không giỏi thuật toán lắm, nhất là cái số nguyên tố thì lại càng tệ :">
Mà em xem qua code của anh rồi. Đang Python 3 đẹp đẽ, chả cần "-*- coding" tự dưng nhảy về Python 2 trông buồn quá :v