import time from math import * N = 145234533 t = time.time() if N == 1 : print('') for i in range(2, N+1): if N % i == 0: while N % i == 0: N /= i print(i) print("걸린시간:",time.time()-t)


소인수분해 구하는 코드 구글링한거라는 거 그대로 올려서 돌려봄


viewimage.php?id=3dafdf21f7d335ab67b1d1&no=24b0d769e1d32ca73deb87fa11d02831de04ca5aee4f7f339edb1d2bd94278358081a47962bc8f06098b9a8b391ba9abe8f79777a6a29ead2673aa4b54eb3f067398bd720759e2



23초 정도 나온다



근데 같은 숫자로 N의 루트까지만 돌리면 훨씬 빠름



import time from math import * N = 145234533 t = time.time() for i in range(2, int(sqrt(N))+1): if N % i == 0: while N % i == 0: N /= i print(i) if N == 1 : print('') else: print(int(N)) print("걸린시간:",time.time()-t)





viewimage.php?id=3dafdf21f7d335ab67b1d1&no=24b0d769e1d32ca73deb87fa11d02831de04ca5aee4f7f339edb1d2bd94278358081a47962bc8f06098b9a8b391ba9abe8f79777a6a29ead2673aa1a57bc6a5c8e3fcff534120a



엄청 차이남




그리고 생각해보면 N이 계속 나누어지고 있는데


처음의 N만큼 가야할 이유도 없음..


중간에 나누어진 N의 루트 까지만 구해도 됨


그래서 나누어질때마다 범위 줄어들도록 while문으로 바꿔주면...



import time from math import * N = 14523453342223532 t = time.time() for i in range(2, int(sqrt(N))+1): if N % i == 0: while N % i == 0: N /= i print(i) if N == 1 : print('') else: print(int(N)) print("걸린시간:",time.time()-t)
import time from math import * N = 14523453342223532 t = time.time() i = 2 K = int(sqrt(N))+1 while i < K: if N % i == 0: while N % i == 0: N /= i print(i) K = int(sqrt(N))+1 i += 1 if N == 1 : print('') else: print(int(N)) print("걸린시간:",time.time()-t)



(숫자 더 크게 바꿨음)





viewimage.php?id=3dafdf21f7d335ab67b1d1&no=24b0d769e1d32ca73deb87fa11d02831de04ca5aee4f7f339edb1d2bd9437835f87fdc6bfe36499058a57f95d69ebd69d5f4681081af41532275788803b5e68c777b17322dbab2


viewimage.php?id=3dafdf21f7d335ab67b1d1&no=24b0d769e1d32ca73deb87fa11d02831de04ca5aee4f7f339edb1d2bd9437835f87fdc6bfe36499058a57f95d69ebd69d5f4681081af4153227578dc00e2b1d6c94b838b8f96de













인수가 많을수록 효율적이라고 생각함