兩種用. numpy.nonzeros等
一種用列表解析式list comprehension
np的寫法是解析式寫法的15倍快!
Using Python to explore Number Theory :The very short codes of Pyhton, Matlab and R to compute factors of a given integer number N
高雄師範大學應數一整數論202005
The method using numpy d[(N%d) ==0] is about 15 times fast than the method using list comprehension [i for i in d if N%i==0]
‘’’
The very short codes to compute factors of a given integer number N
provided by Prof. P-J Lai MATH NKNU
2005: Pyhton codes, two version using numpy, a third version using list comprehension.
Comparing the run time.
the time for np.nonzero(N%d==0)[0]+1’, 0.20684411727086172
the time for d[(N%d)==0]’, 0.19434772928593702
the time for [i for i in d if N%i==0]’, 12.376007614048945
20200520 run
Python執行2億_因數分解出現無法負荷???
改成2千萬就可以
N=20000000
>>>
= RESTART: C:,,,,,timeForVectorMethodFactor.py
the time for np.nonzero(N%d==0)[0]+1 0.3749055862426758
the time for d[(N%d)==0] 0.4058408737182617
the time for [i for i in d if N%i==0] 6.094330072402954
################################################################
Python time.clock在Python3.3废弃,在Python3.8中将被移除
DeprecationWarning: time.clock has been deprecated in Python 3.3
and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
— — — — — — — — — — — — — — — —
以上提醒: time.clock()自 Pyhton33 已棄置, 出自於以下文章:
本文为CSDN博主「mxxxkuku」的原创文章,遵循CC 4.0 BY-SA版权协议,
原文链接:https://blog.csdn.net/mxxxkuku/java/article/details/95784259
link
import time
import numpy as np
N=20000000
start=time.time()
d=np.arange(1,(N+1)/2)
np.nonzero((N%d)==0)[0]+1
print(‘the time for np.nonzero(N%d==0)[0]+1’, time.time()-start)
#start=time.clock()
start=time.time()
d=np.arange(1,(N+1)/2)
d[(N%d)==0]
print(‘the time for d[(N%d)==0]’, time.time()-start)
start=time.time()
[i for i in d if N%i==0]
print(‘the time for [i for i in d if N%i==0]’, time.time()-start)
‘’’The very short Matlab codes to compute factors of a given integer number N
provided by Prof. P-J Lai MATH NKNU 2003:
>>a=1:10;b=rem(20,a);find(~b) ( or just: a=1:10;b=rem(20,a) a(b==0) )’’’
‘’’The very short R codes to compute factors of a given integer number N
provided by Prof. P-J Lai MATH NKNU 2016:
> a<-1:10
> b<-20%%a;b
[1] 0 0 2 0 0 2 6 4 2 0
> b==0
[1] TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE
> a[b==0]
[1] 1 2 4 5 10
‘’’
import time
import numpy as np
N=20000000
start=time.time()
d=np.arange(1,(N+1)/2)
np.nonzero((N%d)==0)[0]+1
print(‘the time for np.nonzero(N%d==0)[0]+1’, time.time()-start)
#start=time.clock()
start=time.time()
d=np.arange(1,(N+1)/2)
d[(N%d)==0]
print(‘the time for d[(N%d)==0]’, time.time()-start)
start=time.time()
[i for i in d if N%i==0]
print(‘the time for [i for i in d if N%i==0]’, time.time()-start)
Matlab codes to compute factors
The very short Matlab codes to compute factors of a given integer number N
provided by Prof. P-J Lai MATH NKNU 2003:
>>a=1:10;b=rem(20,a);find(~b)
>% ( or just: a=1:10;b=rem(20,a) a(b==0) )
R codes to compute factors
The very short R codes to compute factors of a given integer number N
provided by Prof. P-J Lai MATH NKNU 2016:
> a<-1:10
> b<-20%%a;b
[1] 0 0 2 0 0 2 6 4 2 0
> b==0
[1] TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE
> a[b==0]
[1] 1 2 4 5 10