GarbageProgrammer347
GarbageProgrammer347

Reputation: 17

Making a list of a geometric progression when the ratio and range are given

Given the positive integer ratio greater than 1, and the non-negative integer n, create a list consisting of the geometric progression of numbers between (and including) 1 and n with a common ratio of ratio. For example, if ratio is 2 and n is 8, the list would be [1, 2, 4, 8].

Associate the list with the variable geom_prog.

I have tried the following code:

r= ratio
geom_prog = []
for i in range(1, n+1):
    i *= r
    geom_prog.append(i)

For ratio 2 and n = 8:

Expected result: [1, 2, 4, 8]

What I got: [2, 4, 6, 8, 10, 12, 14, 16]

More than anything I'm just wondering what the correct algorithm for getting the correct elements would be. Or if there is a more efficient way to do this problem.

Upvotes: 0

Views: 1634

Answers (7)

rdas
rdas

Reputation: 21275

r = ratio
geom_prog = []
x = 1
while x <= n:
    geom_prog.append(x)
    x *= r

Upvotes: 0

chris
chris

Reputation: 2063

By looping over range(1, n+1), you're making n passes (8 in your case). The termination criteria you're looking for is when the newest element of your set hits (or exceeds) n. Try a while loop:

>>> def geom(ratio, n):
...     series = [1]
...     while series[-1] < n:
...         series.append( series[-1] * ratio )
...     return series
...
>>>
>>> geom(2, 8)
[1, 2, 4, 8]

Probably want to add some code to check this will terminate for your parameters (e.g. a ratio of 1 with n > 1 will never terminate), but this should get you started.

Upvotes: 1

Aureliano Guedes
Aureliano Guedes

Reputation: 785

If I understand

r = 2 # set here the factor
geom_prog = []
x = 1 # first element and var to update
n = 8 # last element
for i in range(x, n+1):
    geom_prog.append(x)
    x *= r

EDIT:

Or more pythonic

[start * ratio**i for i in range(n)] ref: Python: Generate a geometric progression using list comprehension

Upvotes: 1

Siong Thye Goh
Siong Thye Goh

Reputation: 3586

Suppose you want to know how many terms will be involved in advance.

Think of the following question, we want ratio^m <= n where we want to solve for m.

then we have m <= log(n)/log(ratio), since m is an integer, m <= int(log(n)/log(ratio))

import math

n=8
ratio = 2
r= ratio
geom_prog = [1]
for i in range(1, int(math.log(n)/math.log(ratio))+1):
    geom_prog.append(geom_prog[-1] * r)

print(geom_prog)

Upvotes: 0

nickyfot
nickyfot

Reputation: 2019

A few good answers have already been posted, but adding this one as well. you can use the math library to calculate the for loop upper limit as well as each element in the progression without changing your logic too much.

import math
r= 2
geom_prog = []
n = 8
n = int(math.log(n, r))
for i in range(0, n+1):
    k = math.pow(r,i)
    geom_prog.append(k)

Upvotes: 0

Netwave
Netwave

Reputation: 42678

Use a simple while loop:

>>> r = 2
>>> n = 8
>>> e = 1
>>> geom_prog = []
>>> while e <= n:
...     geom_prog.append(e)
...     e *= r
... 
>>> geom_prog
[1, 2, 4, 8]

Upvotes: 0

noobie
noobie

Reputation: 19

The problem is instead of restricting values till 8,

for i in range(1, n+1): 

this is telling the program to run the loop for n times.

try this instead:

n = 8
r = 2
geom_prog = []

i = 1 ;
while(i*r <= n):
    geom_prog.append(i*r)
    i+=1 ;

#print(geom_prog) #returns [2, 4, 6, 8]

Upvotes: 0

Related Questions