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

Add solution to Project Euler Problem 016 #213

Merged
merged 4 commits into from
Oct 29, 2023
Merged
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/project_euler/ProjectEuler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export problem_012
export problem_013
export problem_014
export problem_015
export problem_016

include("../math/divisors.jl")
include("../math/sieve_of_eratosthenes.jl")
Expand All @@ -38,5 +39,6 @@ include("problem_012.jl")
include("problem_013.jl")
include("problem_014.jl")
include("problem_015.jl")
include("problem_016.jl")

end
28 changes: 28 additions & 0 deletions src/project_euler/problem_016.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Power Digit Sum

2^15 = 32768 and the sum of its digits is 3+2+7+6+8=26.
What is the sum of the digits of the number 2^1000?
grid?

# Input parameters:
- `a` : base
- `n` : exponent

# Examples/Tests:
```julia
problem_016(1, 1) # returns 1
problem_016(2, 15) # returns 26
problem_016(2, 1000) # returns 1366
problem_016(2, -4) # throws DomainError
```

# Reference
- https://projecteuler.net/problem=15

Contributed by: [Praneeth Jain](https://www.github.com/PraneethJain)
"""
function problem_016(a::T, n::T) where {T<:Integer}
(a <= 0 || n <= 0) && throw(DomainError("a and n must be greater than 1"))
return sum(parse(Int, digit) for digit in string(big(a)^n))
end
7 changes: 7 additions & 0 deletions test/project_euler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,11 @@ using TheAlgorithms.ProjectEuler
@test_throws DomainError problem_015(0, 5)
@test_throws DomainError problem_015(-3, 0)
end

@testset "Project Euler: Problem 016" begin
@test problem_016(1, 1) == 1
@test problem_016(2, 15) == 26
@test problem_016(2, 1000) == 1366
@test_throws DomainError problem_016(2, -4)
end
end
Loading