From efe8be63b933395dcea01b9e00ce4b8f3ab4e149 Mon Sep 17 00:00:00 2001 From: Nikola-Mircic Date: Tue, 26 Nov 2024 22:55:08 +0100 Subject: [PATCH 1/4] Added algorithm --- src/math/Math.jl | 2 ++ src/math/exp_logn.jl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/math/exp_logn.jl diff --git a/src/math/Math.jl b/src/math/Math.jl index f8c1eb0a..ed5ec2bd 100644 --- a/src/math/Math.jl +++ b/src/math/Math.jl @@ -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 @@ -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") diff --git a/src/math/exp_logn.jl b/src/math/exp_logn.jl new file mode 100644 index 00000000..349fe285 --- /dev/null +++ b/src/math/exp_logn.jl @@ -0,0 +1,44 @@ +""" + 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 +``` + +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 + +println(exp_logn(0, 5)) +println(exp_logn(3.14, 0)) +println(exp_logn(2, 5)) +println(exp_logn(2, -2)) +println(exp_logn(2, -5)) \ No newline at end of file From 76524105310d8f5e3bad6f3f36e78a1582d1f6dc Mon Sep 17 00:00:00 2001 From: Nikola-Mircic Date: Tue, 26 Nov 2024 22:55:18 +0100 Subject: [PATCH 2/4] Added tests --- test/math.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/math.jl b/test/math.jl index ce6b5ab6..539aa0fd 100644 --- a/test/math.jl +++ b/test/math.jl @@ -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 From 2fecb19ab05c97ffd6f9d22b459066d22e85ac0a Mon Sep 17 00:00:00 2001 From: Nikola-Mircic Date: Tue, 26 Nov 2024 23:08:35 +0100 Subject: [PATCH 3/4] Added implementation to docstring --- src/math/exp_logn.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/math/exp_logn.jl b/src/math/exp_logn.jl index 349fe285..d66e52eb 100644 --- a/src/math/exp_logn.jl +++ b/src/math/exp_logn.jl @@ -16,6 +16,27 @@ 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) """ From 76e7974b75e0422163b4432079f3760b5eddf046 Mon Sep 17 00:00:00 2001 From: Nikola-Mircic Date: Wed, 27 Nov 2024 23:23:25 +0100 Subject: [PATCH 4/4] Removed println calls --- src/math/exp_logn.jl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/math/exp_logn.jl b/src/math/exp_logn.jl index d66e52eb..fb4b8fa1 100644 --- a/src/math/exp_logn.jl +++ b/src/math/exp_logn.jl @@ -57,9 +57,3 @@ function exp_logn(x::Number, n::Int) end end end - -println(exp_logn(0, 5)) -println(exp_logn(3.14, 0)) -println(exp_logn(2, 5)) -println(exp_logn(2, -2)) -println(exp_logn(2, -5)) \ No newline at end of file