Skip to content
Advertisement

How do i loop over a geometric sequence. i need to loop some function over 1, 2, 4, 8, 16

My code is

T=np.empty()
for N in range ("some gemetric numbers: 1,2,4,8,16):
    T[N]= trap(f1,a,b,N)

This is what the program looks like

Advertisement

Answer

With generators:

def geom_generator(max_number):
  i = 1
  while i < max_number:
    yield i
    i = i*2

for i in geom_generator(max_number=1000): # just a random sample
  # do something
  print (i)

Output:

1
2
4
8
16
32
64
128
256
512
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement