-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
intfuncs.jl
890 lines (754 loc) · 22.7 KB
/
intfuncs.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# This file is a part of Julia. License is MIT: https://julialang.org/license
## number-theoretic functions ##
"""
gcd(x,y)
Greatest common (positive) divisor (or zero if `x` and `y` are both zero).
# Examples
```jldoctest
julia> gcd(6,9)
3
julia> gcd(6,-9)
3
```
"""
function gcd(a::T, b::T) where T<:Integer
while b != 0
t = b
b = rem(a, b)
a = t
end
checked_abs(a)
end
# binary GCD (aka Stein's) algorithm
# about 1.7x (2.1x) faster for random Int64s (Int128s)
function gcd(a::T, b::T) where T<:Union{Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128}
a == 0 && return abs(b)
b == 0 && return abs(a)
za = trailing_zeros(a)
zb = trailing_zeros(b)
k = min(za, zb)
u = unsigned(abs(a >> za))
v = unsigned(abs(b >> zb))
while u != v
if u > v
u, v = v, u
end
v -= u
v >>= trailing_zeros(v)
end
r = u << k
# T(r) would throw InexactError; we want OverflowError instead
r > typemax(T) && __throw_gcd_overflow(a, b)
r % T
end
@noinline __throw_gcd_overflow(a, b) = throw(OverflowError("gcd($a, $b) overflows"))
"""
lcm(x,y)
Least common (non-negative) multiple.
# Examples
```jldoctest
julia> lcm(2,3)
6
julia> lcm(-2,3)
6
```
"""
function lcm(a::T, b::T) where T<:Integer
# explicit a==0 test is to handle case of lcm(0,0) correctly
if a == 0
return a
else
return checked_abs(a * div(b, gcd(b,a)))
end
end
gcd(a::Integer) = a
lcm(a::Integer) = a
gcd(a::Integer, b::Integer) = gcd(promote(a,b)...)
lcm(a::Integer, b::Integer) = lcm(promote(a,b)...)
gcd(a::Integer, b::Integer...) = gcd(a, gcd(b...))
lcm(a::Integer, b::Integer...) = lcm(a, lcm(b...))
lcm(abc::AbstractArray{<:Integer}) = reduce(lcm, abc; init=one(eltype(abc)))
function gcd(abc::AbstractArray{<:Integer})
a = zero(eltype(abc))
for b in abc
a = gcd(a,b)
if a == 1
return a
end
end
return a
end
# return (gcd(a,b),x,y) such that ax+by == gcd(a,b)
"""
gcdx(x,y)
Computes the greatest common (positive) divisor of `x` and `y` and their Bézout
coefficients, i.e. the integer coefficients `u` and `v` that satisfy
``ux+vy = d = gcd(x,y)``. ``gcdx(x,y)`` returns ``(d,u,v)``.
# Examples
```jldoctest
julia> gcdx(12, 42)
(6, -3, 1)
julia> gcdx(240, 46)
(2, -9, 47)
```
!!! note
Bézout coefficients are *not* uniquely defined. `gcdx` returns the minimal
Bézout coefficients that are computed by the extended Euclidean algorithm.
(Ref: D. Knuth, TAoCP, 2/e, p. 325, Algorithm X.)
For signed integers, these coefficients `u` and `v` are minimal in
the sense that ``|u| < |y/d|`` and ``|v| < |x/d|``. Furthermore,
the signs of `u` and `v` are chosen so that `d` is positive.
For unsigned integers, the coefficients `u` and `v` might be near
their `typemax`, and the identity then holds only via the unsigned
integers' modulo arithmetic.
"""
function gcdx(a::T, b::T) where T<:Integer
# a0, b0 = a, b
s0, s1 = oneunit(T), zero(T)
t0, t1 = s1, s0
# The loop invariant is: s0*a0 + t0*b0 == a
while b != 0
q = div(a, b)
a, b = b, rem(a, b)
s0, s1 = s1, s0 - q*s1
t0, t1 = t1, t0 - q*t1
end
a < 0 ? (-a, -s0, -t0) : (a, s0, t0)
end
gcdx(a::Integer, b::Integer) = gcdx(promote(a,b)...)
# multiplicative inverse of n mod m, error if none
"""
invmod(x,m)
Take the inverse of `x` modulo `m`: `y` such that ``x y = 1 \\pmod m``,
with ``div(x,y) = 0``. This is undefined for ``m = 0``, or if
``gcd(x,m) \\neq 1``.
# Examples
```jldoctest
julia> invmod(2,5)
3
julia> invmod(2,3)
2
julia> invmod(5,6)
5
```
"""
function invmod(n::T, m::T) where T<:Integer
g, x, y = gcdx(n, m)
g != 1 && throw(DomainError((n, m), "Greatest common divisor is $g."))
m == 0 && throw(DomainError(m, "`m` must not be 0."))
# Note that m might be negative here.
# For unsigned T, x might be close to typemax; add m to force a wrap-around.
r = mod(x + m, m)
# The postcondition is: mod(r * n, m) == mod(T(1), m) && div(r, m) == 0
r
end
invmod(n::Integer, m::Integer) = invmod(promote(n,m)...)
# ^ for any x supporting *
to_power_type(x) = convert(promote_op(*, typeof(x), typeof(x)), x)
@noinline throw_domerr_powbysq(::Any, p) = throw(DomainError(p,
string("Cannot raise an integer x to a negative power ", p, '.',
"\nConvert input to float.")))
@noinline throw_domerr_powbysq(::Integer, p) = throw(DomainError(p,
string("Cannot raise an integer x to a negative power ", p, '.',
"\nMake x a float by adding a zero decimal (e.g., 2.0^$p instead ",
"of 2^$p), or write 1/x^$(-p), float(x)^$p, or (x//1)^$p")))
@noinline throw_domerr_powbysq(::AbstractMatrix, p) = throw(DomainError(p,
string("Cannot raise an integer matrix x to a negative power ", p, '.',
"\nMake x a float matrix by adding a zero decimal ",
"(e.g., [2.0 1.0;1.0 0.0]^$p instead ",
"of [2 1;1 0]^$p), or write float(x)^$p or Rational.(x)^$p")))
function power_by_squaring(x_, p::Integer)
x = to_power_type(x_)
if p == 1
return copy(x)
elseif p == 0
return one(x)
elseif p == 2
return x*x
elseif p < 0
isone(x) && return copy(x)
isone(-x) && return iseven(p) ? one(x) : copy(x)
throw_domerr_powbysq(x, p)
end
t = trailing_zeros(p) + 1
p >>= t
while (t -= 1) > 0
x *= x
end
y = x
while p > 0
t = trailing_zeros(p) + 1
p >>= t
while (t -= 1) >= 0
x *= x
end
y *= x
end
return y
end
power_by_squaring(x::Bool, p::Unsigned) = ((p==0) | x)
function power_by_squaring(x::Bool, p::Integer)
p < 0 && !x && throw_domerr_powbysq(x, p)
return (p==0) | x
end
^(x::T, p::T) where {T<:Integer} = power_by_squaring(x,p)
^(x::Number, p::Integer) = power_by_squaring(x,p)
# x^p for any literal integer p is lowered to Base.literal_pow(^, x, Val(p))
# to enable compile-time optimizations specialized to p.
# However, we still need a fallback that calls the function ^ which may either
# mean Base.^ or something else, depending on context.
# We mark these @inline since if the target is marked @inline,
# we want to make sure that gets propagated,
# even if it is over the inlining threshold.
@inline literal_pow(f, x, ::Val{p}) where {p} = f(x,p)
# Restrict inlining to hardware-supported arithmetic types, which
# are fast enough to benefit from inlining.
const HWReal = Union{Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64,Float32,Float64}
const HWNumber = Union{HWReal, Complex{<:HWReal}, Rational{<:HWReal}}
# Core.Compiler has complicated logic to inline x^2 and x^3 for
# numeric types. In terms of Val we can do it much more simply.
# (The first argument prevents unexpected behavior if a function ^
# is defined that is not equal to Base.^)
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{0}) = one(x)
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{1}) = x
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{2}) = x*x
@inline literal_pow(::typeof(^), x::HWNumber, ::Val{3}) = x*x*x
# don't use the inv(x) transformation here since float^p is slightly more accurate
@inline literal_pow(::typeof(^), x::AbstractFloat, ::Val{p}) where {p} = x^p
@inline literal_pow(::typeof(^), x::AbstractFloat, ::Val{-1}) = inv(x)
# for other types, define x^-n as inv(x)^n so that negative literal powers can
# be computed in a type-stable way even for e.g. integers.
@inline @generated function literal_pow(f::typeof(^), x, ::Val{p}) where {p}
if p < 0
:(literal_pow(^, inv(x), $(Val{-p}())))
else
:(f(x,$p))
end
end
# note: it is tempting to add optimized literal_pow(::typeof(^), x, ::Val{n})
# methods here for various n, but this easily leads to method ambiguities
# if anyone has defined literal_pow(::typeof(^), x::T, ::Val).
# b^p mod m
"""
powermod(x::Integer, p::Integer, m)
Compute ``x^p \\pmod m``.
# Examples
```jldoctest
julia> powermod(2, 6, 5)
4
julia> mod(2^6, 5)
4
julia> powermod(5, 2, 20)
5
julia> powermod(5, 2, 19)
6
julia> powermod(5, 3, 19)
11
```
"""
function powermod(x::Integer, p::Integer, m::T) where T<:Integer
p < 0 && return powermod(invmod(x, m), -p, m)
p == 0 && return mod(one(m),m)
(m == 1 || m == -1) && return zero(m)
b = oftype(m,mod(x,m)) # this also checks for divide by zero
t = prevpow(2, p)
r::T = 1
while true
if p >= t
r = mod(widemul(r,b),m)
p -= t
end
t >>>= 1
t <= 0 && break
r = mod(widemul(r,r),m)
end
return r
end
# optimization: promote the modulus m to BigInt only once (cf. widemul in generic powermod above)
powermod(x::Integer, p::Integer, m::Union{Int128,UInt128}) = oftype(m, powermod(x, p, big(m)))
_nextpow2(x::Unsigned) = oneunit(x)<<((sizeof(x)<<3)-leading_zeros(x-oneunit(x)))
_nextpow2(x::Integer) = reinterpret(typeof(x),x < 0 ? -_nextpow2(unsigned(-x)) : _nextpow2(unsigned(x)))
_prevpow2(x::Unsigned) = one(x) << unsigned((sizeof(x)<<3)-leading_zeros(x)-1)
_prevpow2(x::Integer) = reinterpret(typeof(x),x < 0 ? -_prevpow2(unsigned(-x)) : _prevpow2(unsigned(x)))
"""
ispow2(n::Integer) -> Bool
Test whether `n` is a power of two.
# Examples
```jldoctest
julia> ispow2(4)
true
julia> ispow2(5)
false
```
"""
ispow2(x::Integer) = x > 0 && count_ones(x) == 1
"""
nextpow(a, x)
The smallest `a^n` not less than `x`, where `n` is a non-negative integer. `a` must be
greater than 1, and `x` must be greater than 0.
# Examples
```jldoctest
julia> nextpow(2, 7)
8
julia> nextpow(2, 9)
16
julia> nextpow(5, 20)
25
julia> nextpow(4, 16)
16
```
See also [`prevpow`](@ref).
"""
function nextpow(a::Real, x::Real)
x <= 0 && throw(DomainError(x, "`x` must be positive."))
# Special case fast path for x::Integer, a == 2.
# This is a very common case. Constant prop will make sure that a call site
# specified as `nextpow(2, x)` will get this special case inlined.
a == 2 && isa(x, Integer) && return _nextpow2(x)
a <= 1 && throw(DomainError(a, "`a` must be greater than 1."))
x <= 1 && return one(a)
n = ceil(Integer,log(a, x))
p = a^(n-1)
# guard against roundoff error, e.g., with a=5 and x=125
p >= x ? p : a^n
end
"""
prevpow(a, x)
The largest `a^n` not greater than `x`, where `n` is a non-negative integer.
`a` must be greater than 1, and `x` must not be less than 1.
# Examples
```jldoctest
julia> prevpow(2, 7)
4
julia> prevpow(2, 9)
8
julia> prevpow(5, 20)
5
julia> prevpow(4, 16)
16
```
See also [`nextpow`](@ref).
"""
function prevpow(a::Real, x::Real)
x < 1 && throw(DomainError(x, "`x` must be ≥ 1."))
# See comment in nextpos() for a == special case.
a == 2 && isa(x, Integer) && return _prevpow2(x)
a <= 1 && throw(DomainError(a, "`a` must be greater than 1."))
n = floor(Integer,log(a, x))
p = a^(n+1)
p <= x ? p : a^n
end
## ndigits (number of digits) in base 10 ##
# decimal digits in an unsigned integer
const powers_of_ten = [
0x0000000000000001, 0x000000000000000a, 0x0000000000000064, 0x00000000000003e8,
0x0000000000002710, 0x00000000000186a0, 0x00000000000f4240, 0x0000000000989680,
0x0000000005f5e100, 0x000000003b9aca00, 0x00000002540be400, 0x000000174876e800,
0x000000e8d4a51000, 0x000009184e72a000, 0x00005af3107a4000, 0x00038d7ea4c68000,
0x002386f26fc10000, 0x016345785d8a0000, 0x0de0b6b3a7640000, 0x8ac7230489e80000,
]
function ndigits0z(x::Base.BitUnsigned64)
lz = (sizeof(x)<<3)-leading_zeros(x)
nd = (1233*lz)>>12+1
nd -= x < powers_of_ten[nd]
end
function ndigits0z(x::UInt128)
n = 0
while x > 0x8ac7230489e80000
x = div(x,0x8ac7230489e80000)
n += 19
end
return n + ndigits0z(UInt64(x))
end
ndigits0z(x::BitSigned) = ndigits0z(unsigned(abs(x)))
ndigits0z(x::Integer) = ndigits0zpb(x, 10)
## ndigits with specified base ##
# The suffix "nb" stands for "negative base"
function ndigits0znb(x::Integer, b::Integer)
# precondition: b < -1 && !(typeof(x) <: Unsigned)
d = 0
while x != 0
x = cld(x,b)
d += 1
end
return d
end
# do first division before conversion with signed here, which can otherwise overflow
ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(-signed(fld(x, -b)), b) + (x != 0)
ndigits0znb(x::Bool, b::Integer) = x % Int
# The suffix "pb" stands for "positive base"
# TODO: allow b::Integer
function ndigits0zpb(x::Base.BitUnsigned, b::Int)
# precondition: b > 1
x == 0 && return 0
b < 0 && return ndigits0znb(signed(x), b)
b == 2 && return sizeof(x)<<3 - leading_zeros(x)
b == 8 && return (sizeof(x)<<3 - leading_zeros(x) + 2) ÷ 3
b == 16 && return sizeof(x)<<1 - leading_zeros(x)>>2
b == 10 && return ndigits0z(x)
d = 0
while x > typemax(Int)
x = div(x,b)
d += 1
end
x = div(x,b)
d += 1
m = 1
while m <= x
m *= b
d += 1
end
return d
end
ndigits0zpb(x::Base.BitSigned, b::Integer) = ndigits0zpb(unsigned(abs(x)), Int(b))
ndigits0zpb(x::Base.BitUnsigned, b::Integer) = ndigits0zpb(x, Int(b))
ndigits0zpb(x::Bool, b::Integer) = x % Int
# The suffix "0z" means that the output is 0 on input zero (cf. #16841)
"""
ndigits0z(n::Integer, b::Integer=10)
Return 0 if `n == 0`, otherwise compute the number of digits in
integer `n` written in base `b` (i.e. equal to `ndigits(n, base=b)`
in this case).
The base `b` must not be in `[-1, 0, 1]`.
# Examples
```jldoctest
julia> Base.ndigits0z(0, 16)
0
julia> Base.ndigits(0, base=16)
1
julia> Base.ndigits0z(0)
0
julia> Base.ndigits0z(10, 2)
4
julia> Base.ndigits0z(10)
2
```
See also [`ndigits`](@ref).
"""
function ndigits0z(x::Integer, b::Integer)
if b < -1
ndigits0znb(x, b)
elseif b > 1
ndigits0zpb(x, b)
else
throw(DomainError(b, "The base must not be in `[-1, 0, 1]`."))
end
end
"""
ndigits(n::Integer; base::Integer=10, pad::Integer=1)
Compute the number of digits in integer `n` written in base `base`
(`base` must not be in `[-1, 0, 1]`), optionally padded with zeros
to a specified size (the result will never be less than `pad`).
# Examples
```jldoctest
julia> ndigits(12345)
5
julia> ndigits(1022, base=16)
3
julia> string(1022, base=16)
"3fe"
julia> ndigits(123, pad=5)
5
```
"""
ndigits(x::Integer; base::Integer=10, pad::Int=1) = max(pad, ndigits0z(x, base))
## integer to string functions ##
function bin(x::Unsigned, pad::Int, neg::Bool)
i = neg + max(pad,sizeof(x)<<3-leading_zeros(x))
a = StringVector(i)
while i > neg
@inbounds a[i] = 48+(x&0x1)
x >>= 1
i -= 1
end
if neg; @inbounds a[1]=0x2d; end
String(a)
end
function oct(x::Unsigned, pad::Int, neg::Bool)
i = neg + max(pad,div((sizeof(x)<<3)-leading_zeros(x)+2,3))
a = StringVector(i)
while i > neg
@inbounds a[i] = 48+(x&0x7)
x >>= 3
i -= 1
end
if neg; @inbounds a[1]=0x2d; end
String(a)
end
function dec(x::Unsigned, pad::Int, neg::Bool)
i = neg + ndigits(x, base=10, pad=pad)
a = StringVector(i)
while i > neg
@inbounds a[i] = 48+rem(x,10)
x = oftype(x,div(x,10))
i -= 1
end
if neg; @inbounds a[1]=0x2d; end
String(a)
end
function hex(x::Unsigned, pad::Int, neg::Bool)
i = neg + max(pad,(sizeof(x)<<1)-(leading_zeros(x)>>2))
a = StringVector(i)
while i > neg
d = x & 0xf
@inbounds a[i] = 48+d+39*(d>9)
x >>= 4
i -= 1
end
if neg; @inbounds a[1]=0x2d; end
String(a)
end
const base36digits = ['0':'9';'a':'z']
const base62digits = ['0':'9';'A':'Z';'a':'z']
function _base(b::Int, x::Integer, pad::Int, neg::Bool)
(x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `b` must be negative."))
2 <= abs(b) <= 62 || throw(ArgumentError("base must satisfy 2 ≤ abs(base) ≤ 62, got $b"))
digits = abs(b) <= 36 ? base36digits : base62digits
i = neg + ndigits(x, base=b, pad=pad)
a = StringVector(i)
@inbounds while i > neg
if b > 0
a[i] = digits[1+rem(x,b)]
x = div(x,b)
else
a[i] = digits[1+mod(x,-b)]
x = cld(x,b)
end
i -= 1
end
if neg; a[1]='-'; end
String(a)
end
split_sign(n::Integer) = unsigned(abs(n)), n < 0
split_sign(n::Unsigned) = n, false
"""
string(n::Integer; base::Integer = 10, pad::Integer = 1)
Convert an integer `n` to a string in the given `base`,
optionally specifying a number of digits to pad to.
```jldoctest
julia> string(5, base = 13, pad = 4)
"0005"
julia> string(13, base = 5, pad = 4)
"0023"
```
"""
function string(n::Integer; base::Integer = 10, pad::Integer = 1)
if base == 2
(n_positive, neg) = split_sign(n)
bin(n_positive, pad, neg)
elseif base == 8
(n_positive, neg) = split_sign(n)
oct(n_positive, pad, neg)
elseif base == 10
(n_positive, neg) = split_sign(n)
dec(n_positive, pad, neg)
elseif base == 16
(n_positive, neg) = split_sign(n)
hex(n_positive, pad, neg)
else
_base(Int(base), base > 0 ? unsigned(abs(n)) : convert(Signed, n), Int(pad), (base>0) & (n<0))
end
end
string(b::Bool) = b ? "true" : "false"
"""
bitstring(n)
A string giving the literal bit representation of a number.
# Examples
```jldoctest
julia> bitstring(4)
"0000000000000000000000000000000000000000000000000000000000000100"
julia> bitstring(2.2)
"0100000000000001100110011001100110011001100110011001100110011010"
```
"""
function bitstring end
bitstring(x::Union{Bool,Int8,UInt8}) = string(reinterpret(UInt8,x), pad = 8, base = 2)
bitstring(x::Union{Int16,UInt16,Float16}) = string(reinterpret(UInt16,x), pad = 16, base = 2)
bitstring(x::Union{Char,Int32,UInt32,Float32}) = string(reinterpret(UInt32,x), pad = 32, base = 2)
bitstring(x::Union{Int64,UInt64,Float64}) = string(reinterpret(UInt64,x), pad = 64, base = 2)
bitstring(x::Union{Int128,UInt128}) = string(reinterpret(UInt128,x), pad = 128, base = 2)
"""
digits([T<:Integer], n::Integer; base::T = 10, pad::Integer = 1)
Return an array with element type `T` (default `Int`) of the digits of `n` in the given
base, optionally padded with zeros to a specified size. More significant digits are at
higher indices, such that `n == sum([digits[k]*base^(k-1) for k=1:length(digits)])`.
# Examples
```jldoctest
julia> digits(10, base = 10)
2-element Array{Int64,1}:
0
1
julia> digits(10, base = 2)
4-element Array{Int64,1}:
0
1
0
1
julia> digits(10, base = 2, pad = 6)
6-element Array{Int64,1}:
0
1
0
1
0
0
```
"""
digits(n::Integer; base::Integer = 10, pad::Integer = 1) =
digits(typeof(base), n, base = base, pad = pad)
function digits(T::Type{<:Integer}, n::Integer; base::Integer = 10, pad::Integer = 1)
digits!(zeros(T, ndigits(n, base=base, pad=pad)), n, base=base)
end
"""
hastypemax(T::Type) -> Bool
Return `true` if and only if `typemax(T)` is defined.
"""
hastypemax(::Base.BitIntegerType) = true
hastypemax(::Type{T}) where {T} = applicable(typemax, T)
"""
digits!(array, n::Integer; base::Integer = 10)
Fills an array of the digits of `n` in the given base. More significant digits are at higher
indices. If the array length is insufficient, the least significant digits are filled up to
the array length. If the array length is excessive, the excess portion is filled with zeros.
# Examples
```jldoctest
julia> digits!([2,2,2,2], 10, base = 2)
4-element Array{Int64,1}:
0
1
0
1
julia> digits!([2,2,2,2,2,2], 10, base = 2)
6-element Array{Int64,1}:
0
1
0
1
0
0
```
"""
function digits!(a::AbstractVector{T}, n::Integer; base::Integer = 10) where T<:Integer
2 <= abs(base) || throw(ArgumentError("base must be ≥ 2 or ≤ -2, got $base"))
hastypemax(T) && abs(base) - 1 > typemax(T) &&
throw(ArgumentError("type $T too small for base $base"))
isempty(a) && return a
if base > 0
for i in eachindex(a)
n, d = divrem(n, base)
a[i] = d
end
else
# manually peel one loop iteration for type stability
n, d = fldmod(n, -base)
a[firstindex(a)] = d
n = -signed(n)
for i in firstindex(a)+1:lastindex(a)
n, d = fldmod(n, -base)
a[i] = d
n = -n
end
end
return a
end
"""
isqrt(n::Integer)
Integer square root: the largest integer `m` such that `m*m <= n`.
```jldoctest
julia> isqrt(5)
2
```
"""
isqrt(x::Integer) = oftype(x, trunc(sqrt(x)))
function isqrt(x::Union{Int64,UInt64,Int128,UInt128})
x==0 && return x
s = oftype(x, trunc(sqrt(x)))
# fix with a Newton iteration, since conversion to float discards
# too many bits.
s = (s + div(x,s)) >> 1
s*s > x ? s-1 : s
end
"""
factorial(n::Integer)
Factorial of `n`. If `n` is an [`Integer`](@ref), the factorial is computed as an
integer (promoted to at least 64 bits). Note that this may overflow if `n` is not small,
but you can use `factorial(big(n))` to compute the result exactly in arbitrary precision.
# Examples
```jldoctest
julia> factorial(6)
720
julia> factorial(21)
ERROR: OverflowError: 21 is too large to look up in the table
Stacktrace:
[...]
julia> factorial(big(21))
51090942171709440000
```
# See also
* [`binomial`](@ref)
# External links
* [Factorial](https://en.wikipedia.org/wiki/Factorial) on Wikipedia.
"""
function factorial(n::Integer)
n < 0 && throw(DomainError(n, "`n` must be nonnegative."))
f::typeof(n*n) = 1
for i::typeof(n*n) = 2:n
f *= i
end
return f
end
"""
binomial(n::Integer, k::Integer)
The _binomial coefficient_ ``\\binom{n}{k}``, being the coefficient of the ``k``th term in
the polynomial expansion of ``(1+x)^n``.
If ``n`` is non-negative, then it is the number of ways to choose `k` out of `n` items:
```math
\\binom{n}{k} = \\frac{n!}{k! (n-k)!}
```
where ``n!`` is the [`factorial`](@ref) function.
If ``n`` is negative, then it is defined in terms of the identity
```math
\\binom{n}{k} = (-1)^k \\binom{k-n-1}{k}
```
# Examples
```jldoctest
julia> binomial(5, 3)
10
julia> factorial(5) ÷ (factorial(5-3) * factorial(3))
10
julia> binomial(-5, 3)
-35
```
# See also
* [`factorial`](@ref)
# External links
* [Binomial coeffient](https://en.wikipedia.org/wiki/Binomial_coefficient) on Wikipedia.
"""
function binomial(n::T, k::T) where T<:Integer
n0, k0 = n, k
k < 0 && return zero(T)
sgn = one(T)
if n < 0
n = -n + k -1
if isodd(k)
sgn = -sgn
end
end
k > n && return zero(T)
(k == 0 || k == n) && return sgn
k == 1 && return sgn*n
if k > (n>>1)
k = (n - k)
end
x::T = nn = n - k + 1
nn += 1
rr = 2
while rr <= k
xt = div(widemul(x, nn), rr)
x = xt % T
x == xt || throw(OverflowError("binomial($n0, $k0) overflows"))
rr += 1
nn += 1
end
convert(T, copysign(x, sgn))
end