最簡短的 Python codes 算因數 (Matlab, R)

兩種用. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值