<p>With a\b representing integer division and a%b the remainder of this division:</p>

<pre><code>  blocks_per_epoch = 210000;
  initial_reward = 5 * 10^9;

  current_epoch = current_block_height \ blocks_per_epoch;
  blocks_in_current_epoch = (current_block_height % blocks_per_epoch) + 1;
  current_reward = initial_reward / 2 ^ (current_epoch);

  total_coins = 2 * blocks_per_epoch * (initial_reward - current_reward) +
     blocks_in_current_epoch * current_reward;
</code></pre>

<p>Note that this can be off by a few satoshis due to a different rounding.</p>

<p>If you work with integers for the reward, the division for current_reward can be replaced with a bitshift. If not, you can change initial_reward to 50 to give result in BTC instead of satoshis.</p>

CC BY-SA 3.0
2014-01-20T15:37:46.860Z
Hmmmm - doesn't seem to work. Given that block `147751` is used as the current height (I know we're past that - but work with me here).

current_epoch = 147751 \ 210000 = 0
blocks_in_current_epoch = 147752;
current_reward = 25
total_coins = 14193800

This is definitely off
Shamoon
CC BY-SA 3.0
2014-01-20T15:49:09.813Z
@Shamoon: current_reward in this case is 50, not 25.
Meni Rosenfeld
CC BY-SA 3.0
2014-01-20T15:50:42.247Z
How? if `current_epoch` is 0, then initial_reward / 2^0 would be 50 / 1 = 50
Shamoon
CC BY-SA 3.0
2014-01-20T15:52:47.187Z
@Shamoon - Yes.
Meni Rosenfeld
CC BY-SA 3.0
2014-01-20T16:57:47.907Z
Actually - this is off by one block. For block `38,330`, it's `1,916,550 BTC`, but it should be: `1916500`
Shamoon
CC BY-SA 3.0
2014-01-20T17:03:11.350Z
@Shamoon - No. The first block has height 0. So if the current block height (as in: The height of the last block found) is 0, there are 50 BTC. If the height is 38,330, there are 1916550 BTC. If you want current_block_height to represent the number of existing blocks ( = the height of the next block) instead, remove the +1 for blocks_in_current_epoch.
Meni Rosenfeld
CC BY-SA 3.0
2014-01-20T13:19:49.600Z
2014-01-20T13:19:49.600Z
Meni Rosenfeld
<p>Here's what I have so far:</p>

<pre><code>total_epochs = current_block_height % 210000
total_coins = 0;

for(i = 1; i &lt;= total_epochs; i++) {
  reward = 100 / 2^(i * 210000 / 210000);
  total_coins += 210000 * reward;
}
</code></pre>

<p>But this overaccounts for the total coins. Any simpler strategy that works?</p>

<block><mining-reward>
What is short pseudocode for determining the total number of bitcoins mined?
Shamoon
# What is short pseudocode for determining the total number of bitcoins mined?

(Asked by: username_0 on 2014-01-20)

Here's what I have so far:
total_epochs = current_block_height % 210000
total_coins = 0;

for(i = 1; i <= total_epochs; i++) {
  reward = 100 / 2^(i * 210000 / 210000);
  total_coins += 210000 * reward;
}

But this overaccounts for the total coins. Any simpler strategy that works?

**Tags:** block, mining-reward


---

## 1 answers

### Answer by username_1 on 2014-01-20. Score: 2 | Accepted answer

With a\b representing integer division and a%b the remainder of this division:
  blocks_per_epoch = 210000;
  initial_reward = 5 * 10^9;

  current_epoch = current_block_height \ blocks_per_epoch;
  blocks_in_current_epoch = (current_block_height % blocks_per_epoch) + 1;
  current_reward = initial_reward / 2 ^ (current_epoch);

  total_coins = 2 * blocks_per_epoch * (initial_reward - current_reward) +
     blocks_in_current_epoch * current_reward;

Note that this can be off by a few satoshis due to a different rounding.
If you work with integers for the reward, the division for current_reward can be replaced with a bitshift. If not, you can change initial_reward to 50 to give result in BTC instead of satoshis.

> **Comments:**
> **username_0**: Hmmmm - doesn't seem to work. Given that block `147751` is used as the current height (I know we're past that - but work with me here).  current_epoch = 147751 \ 210000 = 0 blocks_in_current_epoch = 147752; current_reward = 25 total_coins = 14193800  This is definitely off  
> **username_1**: @username_0: current_reward in this case is 50, not 25.  
> **username_0**: How? if `current_epoch` is 0, then initial_reward / 2^0 would be 50 / 1 = 50  
> **username_1**: @username_0 - Yes.  
> **username_0**: Actually - this is off by one block. For block `38,330`, it's `1,916,550 BTC`, but it should be: `1916500`  
> **username_1**: @username_0 - No. The first block has height 0. So if the current block height (as in: The height of the last block found) is 0, there are 50 BTC. If the height is 38,330, there are 1916550 BTC. If you want current_block_height to represent the number of existing blocks ( = the height of the next block) instead, remove the +1 for blocks_in_current_epoch.  

---

