문제 : 소수를 크기 순으로 나열하면 2, 3, 5, 7, 11, 13, ... 과 같이 됩니다.
이 때 10,001번째의 소수를 구하세요.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import time def modSearch(idx): i=2 cnt = 1 List = [] while cnt <= idx: boolean = False for j in range(1, len(List)+1): if i % List[j-1] == 0: boolean = True break if boolean == True: i +=1 continue modCnt = 0 for j in range(1, i+1): if i%j == 0: modCnt +=1 """if modCnt == 2: if i != j: modCnt +=1 break""" if modCnt == 2: print "place no.%d mod : %d" % ((cnt), i) List.append(i) cnt +=1 i+=1 start = time.time() idx = raw_input("idx : ") modSearch(int(idx)) end =time.time() -start print end | cs |
~
place no.9991 mod : 104677
place no.9992 mod : 104681
place no.9993 mod : 104683
place no.9994 mod : 104693
place no.9995 mod : 104701
place no.9996 mod : 104707
place no.9997 mod : 104711
place no.9998 mod : 104717
place no.9999 mod : 104723
place no.10000 mod : 104729
place no.10001 mod : 104743
49.4559998512
아쉬운 느낌(modCnt 2이상을 안가게 짰더니 시간이 더나와...)은 들지만 오늘은 여기까지.
'* Computer Science > Project Euler' 카테고리의 다른 글
project euler 9. problem (0) | 2016.07.19 |
---|---|
project euler 8. problem (0) | 2016.07.18 |
project euler 6. problem (0) | 2016.07.16 |
project euler 5. problem (0) | 2016.07.15 |
project euler 4. problem (0) | 2016.07.14 |