Algorithms

Exponentiation: fold a sheet 10 times and count 1,024 layers

Cover of the article Exponentiation: fold a sheet 10 times and count 1,024 layers
Focused reading

Understand the context.
Put it into practice.

Leia este artigo em português

In this article
  1. How does exponentiation work?
  2. How do you calculate a power with a loop?
  3. Now you ask: what about exponent 0 and exponent 1?
  4. Do I need to write the loop every time?
  5. What is exponential growth?
  6. Where does the programmer find powers of 2?
  7. Now you ask: what about the way back?
  8. What are the most common mistakes with powers?
  9. Now it’s your turn!

Exponentiation is the multiplication of equal factors: the same number multiplied by itself several times. The number that repeats is the base, and the number of times it appears is the exponent.

In the article about loops, you made the computer repeat a sum. Today the loop repeats a multiplication, and the result grows in a way that surprises. If the words power and exponential remind you of a hard class at school, relax! You only need one idea: multiplying by the same number, again and again. The rest of this article comes from it, and so does the logarithm, which comes next.

Think of a sheet of paper. Fold it in half: 2 layers. Fold it again: 4. Once more: 8. Notice that you didn’t gain 2 layers with each fold. The whole amount doubled! Each fold multiplies the number of layers by 2.

Three-panel comic: a person folds a sheet of paper and counts 2 layers with one fold, 4 with two and 8 with three
Three folds: 2, 4 and 8 layers. Each fold multiplies the amount by 2.

How does exponentiation work?

After three folds, the count of layers is 2 × 2 × 2 = 8. That’s three factors equal to 2. Exponentiation is just a short way of writing that calculation:

  • Base: the number that repeats. Here, the 2.
  • Exponent: how many times the base appears in the multiplication. Here, the 3.
  • Power: the result. Here, the 8.

In math, you write 23 = 8, with the exponent small and raised, and you read it as “2 to the power of 3”. Don’t be scared of the notation: it just says “multiply 2 by itself, using three factors”.

IMPORTANT: 23 isn’t 2 × 3. The first is 2 × 2 × 2, which is 8. The second is 6. And the order matters: 23 is 8, but 32 is 3 × 3, which is 9. Got it?

Six stacks of layers of a sheet of paper folded 0 to 5 times: 1, 2, 4, 8, 16 and 32 layers, each one double the previous one
From 0 to 5 folds: the number of layers is always the previous one multiplied by 2.

How do you calculate a power with a loop?

For the programmer, a power is a loop that multiplies. You start with 1 and multiply by the base once on each lap. The exponent says how many laps to do. Here it is in Python:

# Power calculated with a loop that multiplies

def power(base, exponent):
    result = 1
    for lap in range(exponent):
        result = result * base   # multiplies by the base one more time
    return result

for exponent in range(0, 11):
    print("2 to the power of", exponent, "=", power(2, exponent))

In PHP:

<?php
// Power calculated with a loop that multiplies

function power($base, $exponent) {
    $result = 1;
    for ($lap = 0; $lap < $exponent; $lap++) {
        $result = $result * $base;   // multiplies by the base one more time
    }
    return $result;
}

for ($exponent = 0; $exponent <= 10; $exponent++) {
    echo "2 to the power of $exponent = " . power(2, $exponent) . "\n";
}

In C:

// Power calculated with a loop that multiplies
#include <stdio.h>

int power(int base, int exponent) {
    int result = 1;
    for (int lap = 0; lap < exponent; lap++) {
        result = result * base;   // multiplies by the base one more time
    }
    return result;
}

int main(void) {
    for (int exponent = 0; exponent <= 10; exponent++) {
        printf("2 to the power of %d = %d\n", exponent, power(2, exponent));
    }
    return 0;
}

The three languages showed the same output:

2 to the power of 0 = 1
2 to the power of 1 = 2
2 to the power of 2 = 4
2 to the power of 3 = 8
2 to the power of 4 = 16
2 to the power of 5 = 32
2 to the power of 6 = 64
2 to the power of 7 = 128
2 to the power of 8 = 256
2 to the power of 9 = 512
2 to the power of 10 = 1024

Focus on the concept, not the language! In all three, the function has the same shape: a variable that starts at 1 and a loop that multiplies it by the base. That output is the table of powers of 2. Keep this table in mind: it shows up again in this article and in the next one.

ExponentMultiplicationResult
0none1
122
22 × 24
32 × 2 × 28
42 × 2 × 2 × 216
52 × 2 × 2 × 2 × 232
10ten factors equal to 21,024
The table of powers of 2: the exponent counts the factors.

Now you ask: what about exponent 0 and exponent 1?

Look at the first two lines of the output. With exponent 1, the base appears only once, and the result is the base itself: 21 = 2. With exponent 0, the loop doesn’t do a single lap, and the function returns the initial value: 20 = 1.

It looks strange, but the table shows why math made that choice. Reading from the bottom up, each line is the previous one divided by 2: 8, 4, 2… and the next one is 1. With 1 on the line for exponent 0, the pattern keeps working. It’s a convention, made so that the rules of powers don’t break.

Do I need to write the loop every time?

In everyday work, no. Python and PHP have a power operator, **. C doesn’t have an operator for this: you use the pow function, from the math.h library, which works with real numbers. In Python:

# The power operator and the ^ trap

print("2 ** 10 =", 2 ** 10)
print("3 ** 2 =", 3 ** 2)
print("2 ** 3 =", 2 ** 3)
print("7 ** 1 =", 7 ** 1)
print("7 ** 0 =", 7 ** 0)
print("2 ^ 10 =", 2 ^ 10)   # IMPORTANT: ^ is not power

In PHP:

<?php
// The power operator and the ^ trap

echo "2 ** 10 = " . (2 ** 10) . "\n";
echo "3 ** 2 = " . (3 ** 2) . "\n";
echo "2 ** 3 = " . (2 ** 3) . "\n";
echo "7 ** 1 = " . (7 ** 1) . "\n";
echo "7 ** 0 = " . (7 ** 0) . "\n";
echo "2 ^ 10 = " . (2 ^ 10) . "\n";   // IMPORTANT: ^ is not power

In C:

// The pow function and the ^ trap
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("pow(2, 10) = %.0f\n", pow(2, 10));
    printf("pow(3, 2) = %.0f\n", pow(3, 2));
    printf("pow(2, 3) = %.0f\n", pow(2, 3));
    printf("pow(7, 1) = %.0f\n", pow(7, 1));
    printf("pow(7, 0) = %.0f\n", pow(7, 0));
    printf("2 ^ 10 = %d\n", 2 ^ 10);   // IMPORTANT: ^ is not power
    return 0;
}

Output in Python and in PHP:

2 ** 10 = 1024
3 ** 2 = 9
2 ** 3 = 8
7 ** 1 = 7
7 ** 0 = 1
2 ^ 10 = 8

Output in C:

pow(2, 10) = 1024
pow(3, 2) = 9
pow(2, 3) = 8
pow(7, 1) = 7
pow(7, 0) = 1
2 ^ 10 = 8

IMPORTANT: look at the last line of the outputs. In typed text, it’s common to write 2^10 for “2 to the power of 10”. In code, no! In all three languages, 2 ^ 10 answered 8. In Python, as in PHP and C, ^ is another operator: the exclusive or, which works with the bits of the numbers. In C, the GCC compiler warned about the mistake at compile time:

operator.c:11:31: warning: result of '2^10' is 8; did you mean '1 << 10' (1024)? [-Wxor-used-as-pow]

What is exponential growth?

It’s the growth of a value that is multiplied by the same number at each step. When the value gets the same sum at each step, the growth is called linear. The difference between the two is huge, and the program below shows it: one variable gains 2 on each lap, and the other is multiplied by 2.

# Add 2 on each lap or multiply by 2 on each lap?

adding = 0
doubling = 1
print("lap | adding 2 | multiplying by 2")
for lap in range(1, 11):
    adding = adding + 2
    doubling = doubling * 2
    print(lap, "|", adding, "|", doubling)

Output:

lap | adding 2 | multiplying by 2
1 | 2 | 2
2 | 4 | 4
3 | 6 | 8
4 | 8 | 16
5 | 10 | 32
6 | 12 | 64
7 | 14 | 128
8 | 16 | 256
9 | 18 | 512
10 | 20 | 1024
Bar chart with ten laps of a loop: adding 2 on each lap, the value goes from 2 to 20, and multiplying by 2, it goes from 2 to 1,024
Ten laps: adding 2, the value reaches 20; multiplying by 2, it reaches 1,024.

On the first two laps, the two values are equal. On lap 10, one is at 20, and the other at 1,024. I’ll repeat it because it’s important: in linear growth, you add the same amount at each step; in exponential growth, you multiply by the same factor at each step. That’s why exponential growth starts slowly and then takes off.

The name comes from the exponent. The number of laps, which is what changes, sits in the exponent of the power: after n laps, the value is 2n.

See the animation of the value doubling on each lap
Animation of a loop that multiplies the value by 2 on each lap: the little squares go from 1 to 1,024 in ten laps
From 1 to 1,024 in ten laps: each lap doubles the number of little squares.

Where does the programmer find powers of 2?

Almost everywhere! Two examples:

  • Bits. A bit stores 2 values, 0 or 1. Each extra bit doubles the number of combinations. That’s why, with n bits, you can represent 2n different values: 8 bits, a byte, give 256.
  • Binary search. Each question of binary search cuts the possibilities in half, which is the reverse of doubling. Since 220 is more than 1 million, 20 questions are enough to find a value among 1 million.
# How many values fit in n bits? 2 to the power of n

for bits in [1, 2, 3, 8, 16, 32]:
    print(bits, "bits:", 2 ** bits, "values")

print("2 to the power of 20 =", 2 ** 20)

Output:

1 bits: 2 values
2 bits: 4 values
3 bits: 8 values
8 bits: 256 values
16 bits: 65536 values
32 bits: 4294967296 values
2 to the power of 20 = 1048576

With 32 bits, that’s already more than 4 billion values. And it only took 32 multiplications by 2 to get there!

Now you ask: what about the way back?

Until now, you knew the number of folds and wanted the layers. The opposite question also exists: if the folded sheet has 1,024 layers, how many folds were there? You can answer with the table, looking for 1,024 in the results column: 10. And you can answer with a loop that counts the folds until it reaches the target:

# The way back: how many folds to reach the target?

def count_folds(target):
    layers = 1
    folds = 0
    while layers < target:
        layers = layers * 2
        folds = folds + 1
    return folds

print("up to 8 layers:", count_folds(8), "folds")
print("up to 1024 layers:", count_folds(1024), "folds")
print("up to 1000000 layers:", count_folds(1000000), "folds")

Output:

up to 8 layers: 3 folds
up to 1024 layers: 10 folds
up to 1000000 layers: 20 folds

1 million isn’t an exact power of 2, and that’s why the function returned 20: it’s the number of folds needed to reach 1 million or go past it.

That opposite question has a name: logarithm. The logarithm of 1,024 in base 2 is 10, because 2 to the power of 10 is 1,024. There’s no mystery: it’s the same table, read from right to left. That’s the topic of the next article.

What are the most common mistakes with powers?

  • Mixing up power and multiplication. 23 is 8, not 6.
  • Swapping the base and the exponent. 23 is 8, and 32 is 9.
  • Using ^ in code. In Python, PHP and C, it doesn’t calculate a power.
  • Starting the loop at 0. Zero times any number is zero, and the result never moves. The initial value is 1.
  • Forgetting exponent 0. Base 2 raised to 0 is 1, and the same goes for the other positive bases.
  • Thinking that doubling is adding 2. Adding, the value grows 2 by 2. Doubling, it takes off.

Now it’s your turn!

On paper:

  • What is 26? And 27? Use the table and then check by multiplying.
  • What is 34? Write the multiplication before doing the math.
  • A sheet was folded and ended up with 64 layers. How many folds were there?
  • How many different values fit in 10 bits?

On the computer:

  • Use the power function to show the table of powers of 3, from exponent 0 to 5.
  • Change the function to count how many multiplications it did.
  • Write a program that finds the first lap on which the doubling value goes past 1 million.

Try it on your own, without asking anyone for the ready-made answer, not even AI!

You saw that always multiplying by the same number makes the value take off, and that the table of powers can be read backward. In the next article, that backward reading gets a name and a use: the logarithm, which explains why binary search is so fast. See you there!

Stay confident!!!

Sources:

  • OpenStax, Contemporary Mathematics. 3.8 Exponents, with the base, the exponent and exponents as a shorthand for repeated multiplication.
  • OpenStax, Algebra 1. 5.1.3 Using Zero Exponent Property and Negative Exponents, with the rule that any non-zero number raised to the zero power is 1.
  • OpenStax, Precalculus 2e. 4.1 Exponential Functions, with the table that puts 2x next to 2x and shows exponential growth leaving linear growth far behind.
  • OpenStax, Precalculus 2e. 4.3 Logarithmic Functions, with the logarithm as the exponent to which the base must be raised to get a number.
  • University of Toronto, ECE243 Computer Organization (Andreas Moshovos). Using Bits to Represent Things, with the rule that N bits can represent up to 2N different things.
  • University of Wisconsin–Madison, CS 354 (Mark Hill). Lecture notes, chapter 4, with n bits representing 2n values, 256 for 8 bits and 4,294,967,296 for 32 bits.
  • Kansas State University, Introduction to Python. New Operators, with the ** exponentiation operator and the warning that ^ is the bitwise XOR.
  • University of Vermont, CS 1210 course book (Clayton Cafiero). Exponentiation, with the syntax that changes between languages and C, which has no operator and uses the pow() function.
  • PHP, official manual. Arithmetic Operators, with $a ** $b, the exponentiation operator.
  • FreeBSD, manual pages. powf(3): exponential and power functions, with the pow functions of math.h, which compute x to the exponent y.
  • GCC Bugzilla. Bug 90885: GCC should warn about 2^16 and 2^32 and 2^64 [-Wxor-used-as-pow], with the origin of the warning that GCC shows when ^ looks like it was meant as a power.
  • The examples were run on Python 3.13, PHP 8.3 and GCC 13.

Share this article

About the author

Walmir Silva

Hi! I'm Walmir, a software engineer with an MBA in Software Engineering, the mind behind GrowthCode and the author of the book "Além do Código" (in Portuguese).

If you think programming is just about writing code, get ready to broaden your horizons. Here, we go beyond the code and explore the fascinating intersections between technology, business, the arts and philosophy.

Are you looking for career growth? Do you want to stand out in a competitive market? Are you after a life richer in knowledge and fulfillment? Then you've come to the right place. At GrowthCode, we offer deep insights, proven strategies and a touch of philosophical wisdom to fuel your personal and professional growth.

See all articles →

Continue the conversation

Share your perspective

Your email address will not be published. Required fields are marked *

We keep the IP address and browser of commenters for 6 months, to block spam and because Brazil’s Marco Civil da Internet requires access records to be kept for that period. See how we handle your data (in Portuguese).

Find your next answer

What do you want to learn?

Cookie preferences

Optional cookies start off. You can change your choice at any time on this screen.

NecessaryAlways on

They store your cookie choice and its record code, and keep the site working. They are not used for advertising.

They count visits and show how the site is used: Google Analytics and Microsoft Clarity, loaded by Google Tag Manager.

They show Google AdSense ads without personalization. The cookies in this category limit how often ads repeat and measure their results.

Uses your browsing to choose ads and build ad audiences: Google ads chosen by your interests and Google signals in Analytics. When off, ads are shown without personalization. Requires Advertising.

You can also block cookies in your browser settings. Details in the Privacy policy (in Portuguese).