Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
geom_prog = list(range(1, n+1, what?))


I am trying to make is so that if ratio = 2 and n = 8, then the list would be [1,2,4,8]

So somehow I have to put where I put 'what?' a step that is equal to the
(previous number in the list)*ratio???
Posted
Comments
Matt T Heffron 14-Oct-15 16:40pm    
the range() function doesn't support what you are trying to do.

1 solution

You need something like:
Python
geom_prog = [x for x in range(1, 9) if (x % 2 == 0) | (x == 1)]

Although that gets all even numbers after 1, so you need to modify the expression to get only powers of 2.
 
Share this answer
 
Comments
phil.o 15-Oct-15 5:22am    
"the expression to get only powers of 2"
Something like (x & (x - 1) == 0) ? :)
Richard MacCutchan 15-Oct-15 5:37am    
Cool!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900