我想在python中给出一个 质数的因子列表(2的倍数)。 例如13 = [8 ,4,1],5 = [4,1],7 = [4,2,1],15 = [8,4,2,1] 我会很感激这样做的功能。 Eric
I would want to obtain a list of factors (multiples of 2) given a prime number in python. For example 13=[8,4,1], 5=[4,1], 7=[4,2,1], 15=[8,4,2,1] I would appreciate a fuction which would do this. Eric
推荐答案Eric写道: 我想得到一个因子列表(2的倍数)给出一个在python中的素数。 例如13 = [8,4,1],5 = [ 4,1],7 = [4,2,1],15 = [8,4,2,1] 我很感激能做到这一点的功能。 Eric I would want to obtain a list of factors (multiples of 2) given a prime number in python. For example 13=[8,4,1], 5=[4,1], 7=[4,2,1], 15=[8,4,2,1] I would appreciate a fuction which would do this. Eric
Eric, 最后一次测试17 vs 15? 没有多少检查:
Eric, Is the last test 17 vs 15? With not much checking:
13 [8,4,1] 5 [4,1] 7 [4,2,1] 17 [16,1] 13 [8, 4, 1] 5 [4, 1] 7 [4, 2, 1] 17 [16, 1]
#-------------------------------------- ---------------------------- 导入数学 #例如13 = [8,4,1],5 = [4,1],7 = [4,2,1],15 = [8,4,2,1] #例如13 = [8,4,1],5 = [4,1],7 = [4,2,1],17 = [16,1] def LargestPrimeLessThanOrEqualTo(n): i = 0 而1: x = int(math.pow(2,i)) 如果x == n: 返回x 如果x> n: 返回prevx prevx = x i + = 1 def foo(x ): list = [] temp = x while(temp> 0): z = LargestPrimeLessThanOrEqualTo(临时) temp - = z list.append(z) 返回列表 $ b [13,5,7,17]中x为$ b: 打印x,foo(x) wes
我不确定这些因素是什么?但是再一次我从来都不擅长数学 因为13你可以算上两个,然后找到你可以制作的最大的 数。两个然后向下移动,直到你找到 下一个,然后加1表示你从13开始= [2,2,2,2,2,2,1]? 然后你知道你有[12,1] 12是2的倍数或者你想要 权力?如果你想要力量你可以进行基线逻辑调整 为什么你还想要这个呢? Eric写道: I''m not sure those are factors? But then again I never was good at math for say 13 you could just count up by two and then find the largest number you can make of the twos and then move down until you find the next one and then add 1 say you start with 13=[2,2,2,2,2,2,1] ? then you know you have [12,1] 12 is a multiple of 2 or do you want powers? if you want powers you could make the nessisary logic adjustments Why exactly do you want this anyways? Eric wrote: 我想获得一个因子列表(2的倍数)给出一个在python中的素数。 例如13 = [8,4,1],5 = [4,1],7 = [4,2,1],15 = [8,4,2,1] 我很感激能做到这一点的功能。 Eric I would want to obtain a list of factors (multiples of 2) given a prime number in python. For example 13=[8,4,1], 5=[4,1], 7=[4,2,1], 15=[8,4,2,1] I would appreciate a fuction which would do this. Eric
Eric写道: 我想得到一个因子列表(2的倍数)给出在python中的素数。 例如13 = [8,4,1],5 = [4,1],7 = [4,2,1],15 = [8,4,2,1] 我很感激能做到这一点。 Eric I would want to obtain a list of factors (multiples of 2) given a prime number in python. For example 13=[8,4,1], 5=[4,1], 7=[4,2,1], 15=[8,4,2,1] I would appreciate a fuction which would do this. Eric
下面是一个丑陋而低效的实现。 -g def i2b(i): s ='''' 而i: s =(i& 1和''1''或'0'')+ s i>> = 1 返回s或''0'' def find_factors(val): bstr = i2b(val) facs = [int(j)* 2 ** i for(i,j)in enumerate(bstr [:: - 1])如果 int(j)!= 0] facs.reverse() 返回facs if __name__ ==''__ main__'': print find_factors(13)== [8,4,1] 打印find_factors(5)== [4,1] print find_factors(7)== [4,2,1] 打印find_factors(15)== [8,4,2,1]
Below is an ugly and inefficient implementation. -g def i2b(i): s = '''' while i: s = (i & 1 and ''1'' or ''0'') + s i >>= 1 return s or ''0'' def find_factors(val): bstr = i2b(val) facs = [int(j)*2**i for (i,j) in enumerate(bstr[::-1]) if int(j) != 0] facs.reverse() return facs if __name__ == ''__main__'': print find_factors(13) == [8,4,1] print find_factors(5) == [4,1] print find_factors(7) == [4,2,1] print find_factors(15) == [8,4,2,1]
更多推荐
这是一个非常简单的问题
发布评论