Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Power Up Your Programming With This Simple Equation
Jared Nielsen
Jared Nielsen

Posted on • Originally published atjarednielsen.com

     

Power Up Your Programming With This Simple Equation

You don’t need to be a math whiz to be a good programmer, but there are a handful of tricks you will want to add to your problem solving bag to improve the performance of your algorithms and make an impression in technical interviews. In this tutorial, you will learn how to sum consecutive powers of 2 with a simple and easy to remember equation. Knowing this equation will help you understand recursive runtimes and quickly calculate Big O time and space complexities.

_This article originally published onjarednielsen.com

How to Sum Consecutive Powers of 2

How would you add these numbers?

2^0 + 2^1 + 2^2 + 2^3

Was your first thought to take the ‘brute force’ approach?

2^0 = 12^1 = 2, + 1 = 32^2 = 4, + 3 = 72^3 = 8, + 7 = 15

Nothing wrong with that and you probably didn’t need a pen and paper or a calculator to get there.

What if the final power was not 2^3 but 2^30? Or 2^300?

Brute force would be brutal.

What if you were presented with this situation?

2^0 + 2^1 + 2^2 + … + 2^n = ?

How would you solve this?

Programming is Problem Solving

What is programming?

Programming is problem solving.

What problems do we solve?

There are two primary categories of problems we solve as programmers:

  • Automation
  • Algorithms

We could write a for loop to automate the addition of our powers of 2:

constsumPowers2=power=>{letsum=0;for(leti=0;i<power;i++){sum+=2**i;}returnsum;}

Will it scale?

What’s the Big O?

O(n).

Why?

Our function needs to perform one operation for every input, so the order of our algorithm isO(n) or linear time complexity.

There must be a better way!

Rather than automate the brute force approach, how can we solve this problemalgorithmically?

Math O’Clock 🧮 🕐

I’m about to blow your mind.

Check this out:

1 = 1

😐

Bear with me.

🐻

If1 is equal to1, then it follows that

1 = 2 - 1

And if

1 + 2 = 3

Then it follows that

1 + 2 = 4 - 1

Let’s take it one more step. If

1 + 2 + 4 = 7

Then

1 + 2 + 4 = 8 - 1

Cool?

😎

Let’s power up!

What isx in this equation?

2^x = 8

Or, in plain English, “How many 2s multiplied together does it take to get 8?”

We could also write it as a logarithm:

log2(8) = 3

We could say, “To what power do we raise 2 for a product of 8?”

🧐

We know that2^2 = 4.

And2^1 = 2

And2^0 = 1.

“Wait, what?”

Why is2^0 = 1?

Table time! 🏓

Exponent==Power
2^38
2^2(2^3) / 28 / 24
2^1(2^2) / 24 / 22
2^0(2^1) / 22 / 21

See the pattern?

What is2^4?

16

What is the sum of the powers of2^4?

1 + 2 + 4 + 8 + 16 = 31

What’s another way we can describe31?

31 = 32 - 1

What is2^5?

32

Did you see what happened there?

The sum of the powers of two is one less than the product of the next power.

🤯

Let’s make another table! 🏓🏓

ExponentPowerSum of Powers
2^01n/a
2^123
2^247
2^3815
2^41631
2^53263

What’s the next exponent?

2^6

What’s2^6?

64

So what is the sum of the powers of2^6?

🤔

Let’s convert this pattern to an equation to find out.

What if our exponent is unknown, orn?

2^n

What is the sum of2^n?

☝️ The sum of the powers of two is one less than the product of the next power.

If our power isn, what’s the next power?

n + 1

Ifn is equal to1, then it follows

2^n = 22^(n + 1) = 4

And ifn is equal to2, then it follows

2^n = 42^(n + 1) = 8

Lookin’ good!

How do we getone less than the product of the next power?

We simply subtract1:

2^(n + 1) - 1

🎉 There's our equation!

Programming is Problem Solving

Let’s take another look at our function from above. How can we refactor this to improve its time complexity?

constsumPowers2=power=>{letsum=0;for(leti=0;i<power;i++){sum+=2**i;}returnsum;}

We simply translate our equation into JavaScript!

constsumPowers2=power=>2**(power+1)-1;

What’s the order of our new function?

O(1).

Regardless of the size of the input, our function will always perform the same number of operations.

How to Sum Consecutive Powers of 2

You don’t need to be a math whiz to be a good programmer, but there are a handful of equations you will want to add to your problem solving toolbox. In this tutorial, you learned how to sum consecutive powers of 2 with a simple and easy to remember equation. Knowing this equation will help you understand recursive runtimes and quickly calculate Big O time and space complexities.


Want to level up your problem solving skills? I write a weekly newsletter about programming, problem solving and lifelong learning.

Sign up for The Solution


Top comments(8)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
axelledrouge profile image
AxelleDRouge
Hi, I'm a developer with three year of experience. I am trained in Java/J2e but I am mostly a Javascript/Typescript lover <3 currently working in GIS, with ReactJS and LeafletJS

Nice, clean and simple. Was I too young or too stupid in school to not see the fascinating aspect of maths ? Or did I need the coding aspect to replace the confusing x with a named variable, less abstracted ?
Python wasn't yet taught in high-school, but if it had been, what a change it could have made in my life... mehh too bad, I regret nothing. I am now fully able to understand and enjoy simple equations like this. And that's good :)

CollapseExpand
 
dividedbynil profile image
Kane Ong
Founder, Software Architect, Fullstack Developer, Mobile App Developer, Data Engineer, Data Scientist.I don't have Instagram.
  • Joined

If you like to save 4 mins of reading time, here's the summary:

Given an integer n, find the sum of 20 + 21 + ... + 2n.

If you speak binary, and knowing that:
10 - 1 = 1
100 - 1 = 11
1000 - 1 = 111

if n = 4, the answer will be 24+1 - 1.

You're welcome.

CollapseExpand
 
axelledrouge profile image
AxelleDRouge
Hi, I'm a developer with three year of experience. I am trained in Java/J2e but I am mostly a Javascript/Typescript lover <3 currently working in GIS, with ReactJS and LeafletJS

Heh thanks for your answer, that's probably that.
Too bad, it comes after the end of my high school and college studies. By that time, I was already rejecting maths completely. It's only a few years ago that I went back to it, to improve my code

CollapseExpand
 
arik14 profile image
Faisal Arisandi
  • Joined

Hello.

Umm, just curious.
Why don't you just tell the reader that the example you just show is geometric series.

And there is formula (and derivation about the formula itself if you want) about how to calculate the n terms of geometric sequence, and the sum of this geometric series.

CollapseExpand
 
marianban profile image
Marian Ban
  • Joined
• Edited on• Edited

Thanks for a good explanation. This can be also visualized as a sum of nodes in a binary tree.

CollapseExpand
 
chidioguejiofor profile image
Chidiebere Ogujeiofor
I love challenging projects that make impact in society.
  • Location
    Nigeria
  • Work
    Mr. Chidiebere at Software engineer
  • Joined
• Edited on• Edited

Sum of GP in math presented with brilliance. Nice job

CollapseExpand
 
snatxo profile image
Marc Sancho Ros
  • Joined

Very well explained! Kudos^

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A short bio.
  • Joined

More fromJared Nielsen

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp