这是一个非常简单的问题

编程入门 行业动态 更新时间:2024-10-11 07:29:45
本文介绍了这是一个非常简单的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在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]

更多推荐

这是一个非常简单的问题

本文发布于:2023-11-10 07:14:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这是一个   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!