FastPowering

package
v0.0.0-...-09db93b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 1 Imported by: 0

README

Fast Powering Algorithm

The power of a number says how many times to use the number in a multiplication.

Naive Algorithm Complexity

To find a raised to the power b we multiply a to itself, b times. That is, a^b = a * a * a * ... * a (b occurrences of a).

This operation will take O(n) time since we need to do multiplication operation exactly n times.

Fast Power Algorithm

We can get better execution time by using a divide and conquer approach to compute power which can before up to O(log(n)).

The idea behind the algorithm is based on the fact that:

For even Y:

X^Y = X^(Y/2) * X^(Y/2) 

For odd Y:

X^Y = X^(Y/2) * X^(Y/2) * X

For example

2^4 = (2 * 2) * (2 * 2) = (2^2) * (2^2)
2^5 = (2 * 2) * (2 * 2) * 2 = (2^2) * (2^2) * (2)

Time Complexity

O(log(n))

Implementation

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FastPowering

func FastPowering(base float64, power int) float64

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL