This is very simple question

wes weston wweston at att.net
Wed Apr 21 14:20:11 EDT 2004


Eric wrote:
> 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

import math

def foo(n):
     list = []
     while n > 0:
         x = int(math.log(n,2))
         pow = int(math.pow(2,x))
         list.append(pow)
         n -= pow
     return list

for x in [13,5,7,15,17,33]:
     print x, foo(x)


 >>>
13 [8, 4, 1]
5 [4, 1]
7 [4, 2, 1]
15 [8, 4, 2, 1]
17 [16, 1]
33 [32, 1]
 >>>




More information about the Python-list mailing list