Skip to content

Commit

Permalink
Add pentagonal and hexagonal numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Samayel committed Aug 6, 2015
1 parent 72516f9 commit 73b779d
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/DataStructure/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ nextstate(it::NestedIterator, state) = begin
outerit = it.source
outerstate, innerit, innerstate = state

if (outerstate == Nothing)
if outerstate == Nothing
outerstate = start(outerit)
end

done(outerit, outerstate) && return (outerstate, innerit, innerstate)

if (innerit == Nothing)
if innerit == Nothing
innerit, innerstate = createinner(it, outerstate)
else
_, innerstate = next(innerit, innerstate)
Expand Down
3 changes: 3 additions & 0 deletions src/Math/NumberTheory/factor.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export
isperfectsquare,
divisorcount, divisorsigma,
isperfect, isdeficient, isabundant,
factorization_sorted,
primefactors, factors,
least_number_with_d_divisors

isperfectsquare(n::Integer) = n == isqrt(n)^2

# https://oeis.org/wiki/Divisor_function
divisorcount(n::Integer) = begin
n > 0 || error("Argument 'n' must be an integer greater 0")
Expand Down
31 changes: 31 additions & 0 deletions src/Math/NumberTheory/hexagon.jl
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
2 changes: 2 additions & 0 deletions src/Math/NumberTheory/numbertheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ include("decimal.jl")
include("fibonacci.jl")
include("hailstone.jl")
include("triangle.jl")
include("pentagon.jl")
include("hexagon.jl")
include("genfunc.jl")

end
31 changes: 31 additions & 0 deletions src/Math/NumberTheory/pentagon.jl
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
34 changes: 19 additions & 15 deletions src/Math/NumberTheory/triangle.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
export
nthtriangle, ntriangle,
alltriangle, sometriangle, exacttriangle,
istriangular,
nthtriangular, ntriangular,
alltriangular, sometriangular, exacttriangular,
primitive_pythagorean_triples

nthtriangle(n::Integer) = div(n * (n+1), 2)
ntriangle(n::Integer, T::Type = Int) = exacttriangle(n, T) |> collect
istriangular(n::Integer) = isperfectsquare(8*n + 1)

alltriangle(T::Type = Int) = TriangleIterator{T}()
sometriangle{T<:Integer}(xmax::T) = @pipe alltriangle(T) |> takewhile(@anon(x -> x <= xmax), _)
exacttriangle(n::Integer, T::Type = Int) = @pipe alltriangle(T) |> take(_, n)
nthtriangular(n::Integer) = div(n * (n+1), 2)
ntriangular(n::Integer, T::Type = Int) = exacttriangular(n, T) |> collect

immutable TriangleIterator{T<:Integer}
alltriangular(T::Type = Int) = TriangularIterator{T}()
sometriangular{T<:Integer}(xmax::T) = @pipe alltriangular(T) |> takewhile(@anon(x -> x <= xmax), _)
exacttriangular(n::Integer, T::Type = Int) = @pipe alltriangular(T) |> take(_, n)

immutable TriangularIterator{T<:Integer}
end

Base.start{T<:Integer}(::TriangleIterator{T}) = zero(T), one(T)
Base.next{T<:Integer}(::TriangleIterator{T}, state) = begin
s = sum(state)
s, (s, state[2] + one(T))
Base.start{T<:Integer}(::TriangularIterator{T}) = zero(T), one(T)
Base.next{T<:Integer}(::TriangularIterator{T}, state) = begin
s, n = state
s += n
s, (s, n + 1)
end
Base.done(::TriangleIterator, _) = false
Base.done(::TriangularIterator, _) = false

Base.eltype(it::TriangleIterator) = Base.eltype(typeof(it))
Base.eltype{T<:Integer}(::Type{TriangleIterator{T}}) = T
Base.eltype(it::TriangularIterator) = Base.eltype(typeof(it))
Base.eltype{T<:Integer}(::Type{TriangularIterator{T}}) = T



Expand Down
5 changes: 5 additions & 0 deletions test/Math/NumberTheory/factor.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function test_factor_isperfectsquare()
@test find([isperfectsquare(i) for i = 1:100]) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
end

function test_factor_divisorcount()
@test divisorcount(1) == 1
@test divisorcount(2) == 2
Expand Down Expand Up @@ -74,6 +78,7 @@ end
function test_factor_all()
print(rpad("Math.NumberTheory.Factor...", 50, ' '))

test_factor_isperfectsquare()
test_factor_divisorcount()
test_factor_divisorsigma()
test_factor_isperfect()
Expand Down
40 changes: 40 additions & 0 deletions test/Math/NumberTheory/hexagon.jl
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
4 changes: 4 additions & 0 deletions test/Math/NumberTheory/numbertheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ include("decimal.jl")
include("fibonacci.jl")
include("hailstone.jl")
include("triangle.jl")
include("pentagon.jl")
include("hexagon.jl")
include("genfunc.jl")

function test_all()
Expand All @@ -24,6 +26,8 @@ function test_all()
test_fibonacci_all()
test_hailstone_all()
test_triangle_all()
test_pentagon_all()
test_hexagon_all()
test_genfunc_all()
end

Expand Down
40 changes: 40 additions & 0 deletions test/Math/NumberTheory/pentagon.jl
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
47 changes: 26 additions & 21 deletions test/Math/NumberTheory/triangle.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
function test_triangle_nthtriangle()
@test nthtriangle(1) == 1
@test nthtriangle(2) == 3
@test nthtriangle(10) == 55
function test_triangle_istriangular()
@test find([istriangular(i) for i = 1:400]) == [1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378]
end

function test_triangle_ntriangle()
@test ntriangle(10) == [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
@test eltype(ntriangle(10)) == Int
@test eltype(ntriangle(10, BigInt)) == BigInt
function test_triangle_nthtriangular()
@test nthtriangular(1) == 1
@test nthtriangular(2) == 3
@test nthtriangular(10) == 55
end

function test_triangle_sometriangle()
@test collect(sometriangle(35)) == [1, 3, 6, 10, 15, 21, 28]
@test collect(sometriangle(36)) == [1, 3, 6, 10, 15, 21, 28, 36]
@test eltype(sometriangle(36)) == Int
@test eltype(sometriangle(big(36))) == BigInt
function test_triangle_ntriangular()
@test ntriangular(10) == [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
@test eltype(ntriangular(10)) == Int
@test eltype(ntriangular(10, BigInt)) == BigInt
end

function test_triangle_exacttriangle()
@test collect(exacttriangle(10)) == [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
@test eltype(exacttriangle(10)) == Int
@test eltype(exacttriangle(10, BigInt)) == BigInt
function test_triangle_sometriangular()
@test collect(sometriangular(35)) == [1, 3, 6, 10, 15, 21, 28]
@test collect(sometriangular(36)) == [1, 3, 6, 10, 15, 21, 28, 36]
@test eltype(sometriangular(36)) == Int
@test eltype(sometriangular(big(36))) == BigInt
end

function test_triangle_exacttriangular()
@test collect(exacttriangular(10)) == [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
@test eltype(exacttriangular(10)) == Int
@test eltype(exacttriangular(10, BigInt)) == BigInt
end

function test_triangle_primitive_pythagorean_triples()
Expand Down Expand Up @@ -67,10 +71,11 @@ end
function test_triangle_all()
print(rpad("Math.NumberTheory.Triangle...", 50, ' '))

test_triangle_nthtriangle()
test_triangle_ntriangle()
test_triangle_sometriangle()
test_triangle_exacttriangle()
test_triangle_istriangular()
test_triangle_nthtriangular()
test_triangle_ntriangular()
test_triangle_sometriangular()
test_triangle_exacttriangular()

test_triangle_primitive_pythagorean_triples()

Expand Down

0 comments on commit 73b779d

Please sign in to comment.