Skip to content

Commit

Permalink
digits: make it work for BigInts (by making ndigits0z work for them)
Browse files Browse the repository at this point in the history
Also, add test for Project Euler problem #16.
  • Loading branch information
StefanKarpinski committed Dec 16, 2013
1 parent 48e8037 commit 2b2b42c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), ($),
ndigits, promote_rule, rem, show, isqrt, string, isprime, powermod,
widemul, sum, trailing_zeros, trailing_ones, count_ones, base, parseint,
serialize, deserialize, bin, oct, dec, hex, isequal, invmod,
prevpow2, nextpow2
prevpow2, nextpow2, ndigits0z

type BigInt <: Integer
alloc::Cint
Expand Down Expand Up @@ -406,6 +406,8 @@ function base(b::Integer, n::BigInt)
end

ndigits(x::BigInt, base::Integer=10) = ccall((:__gmpz_sizeinbase,:libgmp), Culong, (Ptr{BigInt}, Int32), &x, base)
ndigits0z(x::BigInt, base::Integer=10) = x == 0 ? 0 : ndigits(x)

isprime(x::BigInt, reps=25) = ccall((:__gmpz_probab_prime_p,:libgmp), Cint, (Ptr{BigInt}, Cint), &x, reps) > 0

widemul(x::BigInt, y::BigInt) = x*y
Expand Down
2 changes: 2 additions & 0 deletions test/bigint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,5 @@ let
seek(b,0)
@test deserialize(b) == n
end

@test sum(digits(big(2)^1000)) == 1366

0 comments on commit 2b2b42c

Please sign in to comment.