Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > This is very simple question

Reply
Thread Tools

This is very simple question

 
 
Eric
Guest
Posts: n/a
 
      04-21-2004
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
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-21-2004
but a lousy subject.

"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]


>>> 8*4*1

32

> I would appreciate a fuction which would do this.


def func(x): return [2**i for i in range(3,0,-1) if x & 2**i]

</F>




 
Reply With Quote
 
 
 
 
Brian Quinlan
Guest
Posts: n/a
 
      04-21-2004

> 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.


If we told you it wouldn't be fair to the other students in your class

Cheers,
Brian


 
Reply With Quote
 
wes weston
Guest
Posts: n/a
 
      04-21-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


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]
>>>


#------------------------------------------------------------------


import math

#For example 13=[8,4,1], 5=[4,1], 7=[4,2,1], 15=[8,4,2,1]
#For example 13=[8,4,1], 5=[4,1], 7=[4,2,1], 17=[16,1]

def LargestPrimeLessThanOrEqualTo(n):
i = 0
while 1:
x = int(math.pow(2,i))
if x == n:
return x
if x > n:
return prevx
prevx = x
i += 1

def foo(x):
list = []
temp = x
while(temp > 0):
z = LargestPrimeLessThanOrEqualTo(temp)
temp -= z
list.append(z)
return list

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

wes



 
Reply With Quote
 
Noah from IT Goes Click
Guest
Posts: n/a
 
      04-21-2004
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:
> 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

 
Reply With Quote
 
Gyro Funch
Guest
Posts: n/a
 
      04-21-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



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]
 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      04-21-2004
In article <mailman.840.1082563771.20120.python->,
Brian Quinlan <> 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.

>
>If we told you it wouldn't be fair to the other students in your class

.
.
.
I'm surprised only one of the responses I've seen so far has
objected to what sure seems like an inappropriate response to
classwork. I'm astonished that no one is reading this question
as I do. I leave as an exercise for readers this generalization
of what *I* think is going on:

def positional_addends(positive_integer, base = 2):
result = []
current = 1
while positive_integer:
remainder = positive_integer % (base * current)
if remainder:
result.append(remainder)
positive_integer -= remainder
current *= base
result.reverse()
return result

print positional_addends(13)
print positional_addends(5)
print positional_addends(7)
print positional_addends(15)
print positional_addends(78904, 10)

The real exercise is to convert this to a readable one-liner,
at least for the binary case.
--

Cameron Laird <>
Business: http://www.Phaseit.net
 
Reply With Quote
 
wes weston
Guest
Posts: n/a
 
      04-21-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


Eric,
To be less illustrative and somewhat more efficient.
wes

import math

def ListOfPowersLessThan(n):
list = []
i = 0
while True:
x = int(math.pow(2,i))
if x <= n:
list.append(x)
else:
break
i += 1
return list

TEST = [13,5,7,15,17,33]
powers = ListOfPowersLessThan( max(TEST) )
powers.reverse()

for x in TEST:
sum = 0
list = []
for s in powers:
if (sum + s) <= x:
sum += s
list.append(s)
if sum >= x:
break
print x,list

>>>

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


 
Reply With Quote
 
wes weston
Guest
Posts: n/a
 
      04-21-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]
>>>


 
Reply With Quote
 
A. Lloyd Flanagan
Guest
Posts: n/a
 
      04-21-2004
(Eric) wrote in message news:<. com>...
> 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


Minor quibble: you don't mean factors. The only factors of a prime
number are the number itself and one; that's the definition of a prime
number.

Now, do you want a list of all powers of 2 less than a prime number,
OR do you want a list of powers of 2 such that the sum of the numbers
is the given prime number? For Mersienne primes, which are of the
form (2**n-1) the answer is the same either way. All your examples
are Mersienne primes.
But look at 11:
11 = 8 + 2 + 1, not 8 + 4 + 2 + 1

Of course, any odd number can be expressed as 2 + 2 + ... + 1, so what
you would really want is the shortest list of powers of 2 that add up
to the prime.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help running a very very very simple code olivier.melcher Java 8 05-12-2008 07:51 PM
CGI.PM Very very simple question AMT2K5 Perl Misc 3 12-02-2005 05:18 PM
Very very very basic question Peter C Programming 14 02-14-2005 09:46 AM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57