Skip to Content
Mastering JavaScript Functional Programming - Second Edition
book

Mastering JavaScript Functional Programming - Second Edition

by Federico Kereki
January 2020
Intermediate to advanced
470 pages
11h 13m
English
Packt Publishing
Content preview from Mastering JavaScript Functional Programming - Second Edition

Decrease and conquer – doing powers

Another classic example has to do with calculating the powers of numbers in an efficient way. If you want to calculate, say, 2 to the 13th power (213), then you can do this with 12 multiplications; however, you can do much better by writing 213 as the following:

= 2 times 212 = 2 times 46 = 2 times 163 = 2 times 16 times 162 = 2 times 16 times 2561 = 8192

This reduction in the total number of multiplications may not look very impressive, but, in terms of algorithmic complexity, it allows us to bring down the order of the calculations from O(n) to O(lg n). In some cryptographic-related methods, which have to raise numbers to really high exponents, this makes a very important difference. We can implement ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering JavaScript Functional Programming

Mastering JavaScript Functional Programming

Federico Kereki

Publisher Resources

ISBN: 9781839213069Supplemental Content