# Doubling the steps in for-loop

Page 1 of 1

## 6 Replies - 693 Views - Last Post: 11 December 2015 - 04:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=385913&amp;s=9ea7bb97da21cda9f279c9d78398668b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 HelenaEngstrom

Reputation: 0
• Posts: 36
• Joined: 05-October 15

# Doubling the steps in for-loop

Posted 10 December 2015 - 01:00 PM

As the topic says, how do I write a for-loop which double the number of steps each iteration?

This is what I've tried:

```for N in range(1,100,2):

h=(b-a)/N
s=0.5*f(a)+0.5*f(B)/>
```

But this only adds 2 to each new step. I would like to have an intervall in the range 1,2,4,8, etc.

Thankful for any help!
Is This A Good Question/Topic? 0

## Replies To: Doubling the steps in for-loop

### #2 DK3250

• Pythonian

Reputation: 562
• Posts: 1,798
• Joined: 27-December 13

## Re: Doubling the steps in for-loop

Posted 10 December 2015 - 01:38 PM

Something like:
```for i in range(N):
ex = 2 ** i  # will generate the values 1, 2, 4, 8, 16, ...
h = (b - a) / ex
```

### #3 atraub

• Pythoneer

Reputation: 835
• Posts: 2,267
• Joined: 23-December 08

## Re: Doubling the steps in for-loop

Posted 10 December 2015 - 01:38 PM

This little snippet should prove helpful.
```for i in range(10):
print(2**i)

```

EDIT:
Ahhh he was just a moment faster than me!

This post has been edited by atraub: 10 December 2015 - 01:40 PM

### #4 matttm

Reputation: 0
• Posts: 30
• Joined: 04-September 15

## Re: Doubling the steps in for-loop

Posted 10 December 2015 - 04:56 PM

atraub, on 10 December 2015 - 01:38 PM, said:

This little snippet should prove helpful.
```for i in range(10):
print(2**i)

```

EDIT:
Ahhh he was just a moment faster than me!

I believe that he wants the actual loop's counter to double.

EDIT: I see said the blind man. Sry

This post has been edited by matttm: 10 December 2015 - 04:57 PM

### #5 HelenaEngstrom

Reputation: 0
• Posts: 36
• Joined: 05-October 15

## Re: Doubling the steps in for-loop

Posted 11 December 2015 - 04:16 AM

Thank you for your answers, but it's still not working. I tried to run this code:

```for N in range(40):
ex=2*N

h=(b-a)/(ex+1) # +1 bc otherwise I get zerodivisonerror
s=0.5*f(a)+0.5*f(B)/>

```

but it still prints 2,4,6,8,..

### #6 andrewsw

Reputation: 6829
• Posts: 28,311
• Joined: 12-December 12

## Re: Doubling the steps in for-loop

Posted 11 December 2015 - 04:33 AM

That isn't the same code. Compare * and **. Look for ** here, the exponential operator.

### #7 HelenaEngstrom

Reputation: 0
• Posts: 36
• Joined: 05-October 15

## Re: Doubling the steps in for-loop

Posted 11 December 2015 - 04:57 AM

Now I see it, thank you!