Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exponentiation in O(log n) #225

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/math/Math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export combination
export divisors
export eratosthenes
export euler_method
export exp_logn
export is_mersenne_prime
export totient
export factorial_iterative
Expand Down Expand Up @@ -101,6 +102,7 @@ include("combination.jl")
include("divisors.jl")
include("euler_method.jl")
include("eulers_totient.jl")
include("exp_logn.jl")
include("factorial.jl")
include("fibonacci.jl")
include("floor.jl")
Expand Down
59 changes: 59 additions & 0 deletions src/math/exp_logn.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
exp_logn(x::Number, n::Int)

Recursive function which calculates 'x' to the power of 'n' in O(log n) complexity.

# Arguments:
- `x` : Base
- `n` : Exponent

# Examples/Tests
```julia
exp_logn(0, 5) # returns 0
exp_logn(3.14, 0) # returns 1
exp_logn(2, 5) # returns 32
exp_logn(2, -2) # returns 0.25
exp_logn(2, -5) # returns 0.03125
```

# Implementation
```julia
function exp_logn(x::Number, n::Int)
if n == 0
return 1
elseif n < 0
return 1/exp_logn(x, -n)
else
tmp = exp_logn(x, div(n,2))

if n%2 == 0
# If n is even, x^n = x^(n/2) * x^(n/2)
return tmp * tmp
else
# else, x^n = x^(n/2) * x^(n/2) * x
return tmp * tmp * x
end
end
end
```

Contributed by [Nikola Mircic](https://www.github.com/Nikola-Mircic)
"""

function exp_logn(x::Number, n::Int)
if n == 0
return 1
elseif n < 0
return 1/exp_logn(x, -n)
else
tmp = exp_logn(x, div(n,2))

if n%2 == 0
# If n is even, x^n = x^(n/2) * x^(n/2)
return tmp * tmp
else
# else, x^n = x^(n/2) * x^(n/2) * x
return tmp * tmp * x
end
end
end
8 changes: 8 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ using TheAlgorithms.Math
@test_throws DomainError totient(-1)
end

@testset "Math: Exponentiation in O(log n)" begin
@test exp_logn(0, 5) == 0
@test exp_logn(3.14, 0) == 1
@test exp_logn(2, 5) == 32
@test exp_logn(2, -2) == 0.25
@test exp_logn(2, -5) == 0.03125
end

@testset "Math: Factorial Related" begin
@test factorial_iterative(5) == 120
@test factorial_iterative(0) == 1
Expand Down
Loading