-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pentagonal and hexagonal numbers
- Loading branch information
Showing
11 changed files
with
203 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export | ||
ishexagonal, | ||
nthhexagonal, nhexagonal, | ||
allhexagonal, somehexagonal, exacthexagonal | ||
|
||
ishexagonal(n::Integer) = begin | ||
t = 8*n + 1 | ||
r = isqrt(t) | ||
(r^2 == t) && (r % 4 == 3) | ||
end | ||
|
||
nthhexagonal(n::Integer) = n * (2*n - 1) | ||
nhexagonal(n::Integer, T::Type = Int) = exacthexagonal(n, T) |> collect | ||
|
||
allhexagonal(T::Type = Int) = HexagonalIterator{T}() | ||
somehexagonal{T<:Integer}(xmax::T) = @pipe allhexagonal(T) |> takewhile(@anon(x -> x <= xmax), _) | ||
exacthexagonal(n::Integer, T::Type = Int) = @pipe allhexagonal(T) |> take(_, n) | ||
|
||
immutable HexagonalIterator{T<:Integer} | ||
end | ||
|
||
Base.start{T<:Integer}(::HexagonalIterator{T}) = zero(T), one(T) | ||
Base.next{T<:Integer}(::HexagonalIterator{T}, state) = begin | ||
s, n = state | ||
s += 4*n - 3 | ||
s, (s, n + 1) | ||
end | ||
Base.done(::HexagonalIterator, _) = false | ||
|
||
Base.eltype(it::HexagonalIterator) = Base.eltype(typeof(it)) | ||
Base.eltype{T<:Integer}(::Type{HexagonalIterator{T}}) = T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export | ||
ispentagonal, | ||
nthpentagonal, npentagonal, | ||
allpentagonal, somepentagonal, exactpentagonal | ||
|
||
ispentagonal(n::Integer) = begin | ||
t = 24*n + 1 | ||
r = isqrt(t) | ||
(r^2 == t) && (r % 6 == 5) | ||
end | ||
|
||
nthpentagonal(n::Integer) = div(n * (3n-1), 2) | ||
npentagonal(n::Integer, T::Type = Int) = exactpentagonal(n, T) |> collect | ||
|
||
allpentagonal(T::Type = Int) = PentagonalIterator{T}() | ||
somepentagonal{T<:Integer}(xmax::T) = @pipe allpentagonal(T) |> takewhile(@anon(x -> x <= xmax), _) | ||
exactpentagonal(n::Integer, T::Type = Int) = @pipe allpentagonal(T) |> take(_, n) | ||
|
||
immutable PentagonalIterator{T<:Integer} | ||
end | ||
|
||
Base.start{T<:Integer}(::PentagonalIterator{T}) = zero(T), one(T) | ||
Base.next{T<:Integer}(::PentagonalIterator{T}, state) = begin | ||
s, n = state | ||
s += 3*n - 2 | ||
s, (s, n + 1) | ||
end | ||
Base.done(::PentagonalIterator, _) = false | ||
|
||
Base.eltype(it::PentagonalIterator) = Base.eltype(typeof(it)) | ||
Base.eltype{T<:Integer}(::Type{PentagonalIterator{T}}) = T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function test_hexagon_ishexagonal() | ||
@test find([ishexagonal(i) for i = 1:1000]) == [1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 946] | ||
end | ||
|
||
function test_hexagon_nthhexagonal() | ||
@test nthhexagonal(1) == 1 | ||
@test nthhexagonal(2) == 6 | ||
@test nthhexagonal(10) == 190 | ||
end | ||
|
||
function test_hexagon_nhexagonal() | ||
@test nhexagonal(10) == [1, 6, 15, 28, 45, 66, 91, 120, 153, 190] | ||
@test eltype(nhexagonal(10)) == Int | ||
@test eltype(nhexagonal(10, BigInt)) == BigInt | ||
end | ||
|
||
function test_hexagon_somehexagonal() | ||
@test collect(somehexagonal(90)) == [1, 6, 15, 28, 45, 66] | ||
@test collect(somehexagonal(91)) == [1, 6, 15, 28, 45, 66, 91] | ||
@test eltype(somehexagonal(91)) == Int | ||
@test eltype(somehexagonal(big(91))) == BigInt | ||
end | ||
|
||
function test_hexagon_exacthexagonal() | ||
@test collect(exacthexagonal(10)) == [1, 6, 15, 28, 45, 66, 91, 120, 153, 190] | ||
@test eltype(exacthexagonal(10)) == Int | ||
@test eltype(exacthexagonal(10, BigInt)) == BigInt | ||
end | ||
|
||
function test_hexagon_all() | ||
print(rpad("Math.NumberTheory.Hexagon...", 50, ' ')) | ||
|
||
test_hexagon_ishexagonal() | ||
test_hexagon_nthhexagonal() | ||
test_hexagon_nhexagonal() | ||
test_hexagon_somehexagonal() | ||
test_hexagon_exacthexagonal() | ||
|
||
println("PASS") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function test_pentagon_ispentagonal() | ||
@test find([ispentagonal(i) for i = 1:1000]) == [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210, 247, 287, 330, 376, 425, 477, 532, 590, 651, 715, 782, 852, 925] | ||
end | ||
|
||
function test_pentagon_nthpentagonal() | ||
@test nthpentagonal(1) == 1 | ||
@test nthpentagonal(2) == 5 | ||
@test nthpentagonal(10) == 145 | ||
end | ||
|
||
function test_pentagon_npentagonal() | ||
@test npentagonal(10) == [1, 5, 12, 22, 35, 51, 70, 92, 117, 145] | ||
@test eltype(npentagonal(10)) == Int | ||
@test eltype(npentagonal(10, BigInt)) == BigInt | ||
end | ||
|
||
function test_pentagon_somepentagonal() | ||
@test collect(somepentagonal(91)) == [1, 5, 12, 22, 35, 51, 70] | ||
@test collect(somepentagonal(92)) == [1, 5, 12, 22, 35, 51, 70, 92] | ||
@test eltype(somepentagonal(92)) == Int | ||
@test eltype(somepentagonal(big(92))) == BigInt | ||
end | ||
|
||
function test_pentagon_exactpentagonal() | ||
@test collect(exactpentagonal(10)) == [1, 5, 12, 22, 35, 51, 70, 92, 117, 145] | ||
@test eltype(exactpentagonal(10)) == Int | ||
@test eltype(exactpentagonal(10, BigInt)) == BigInt | ||
end | ||
|
||
function test_pentagon_all() | ||
print(rpad("Math.NumberTheory.Pentagon...", 50, ' ')) | ||
|
||
test_pentagon_ispentagonal() | ||
test_pentagon_nthpentagonal() | ||
test_pentagon_npentagonal() | ||
test_pentagon_somepentagonal() | ||
test_pentagon_exactpentagonal() | ||
|
||
println("PASS") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters