Sha256: c0783c727fe6937228db5bad9a583f979a27619f0b211badce48d360bb658087

Contents?: true

Size: 778 Bytes

Versions: 135

Compression:

Stored size: 778 Bytes

Contents

"""Calculate the number of grains on square `square`."""
function on_square(square)
    check_square_input(square)
    2 ^ (square - 1)
end

"""Calculate the total number of grains after square `square`."""
function total_after(square)
    check_square_input(square)
    sum(map(on_square, 1:square))
end

"""Validate an arbitrary square."""
function check_square_input(square)
    square == 0 && throw(DomainError(square, "Square input of zero is invalid."))
    square <  0 && throw(DomainError(square, "Negative square input is invalid."))
    square > 64 && throw(DomainError(square, "Square input greater than 64 is invalid."))
end

if VERSION < v"0.7"  # backwards compatibility
    Base.DomainError(val) = DomainError()
    Base.DomainError(val, msg) = DomainError()
end

Version data entries

135 entries across 135 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.118 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.117 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.116 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.115 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.114 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.113 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.111 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.110 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.109 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.108 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.107 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.106 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.105 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.104 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.103 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.102 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.101 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.100 tracks/julia/exercises/grains/example.jl
trackler-2.2.1.99 tracks/julia/exercises/grains/example.jl