June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,2 @@
USING: math.algebra prettyprint ;
{ 2 3 2 } { 3 5 7 } chinese-remainder .

View file

@ -0,0 +1,26 @@
function chineseremainder(n::Array{Int}, a::Array{Int})
sum = 0
prd = prod(n)
for (ni, ai) in zip(n, a)
p = prd ÷ ni
sum += ai * mulinv(p, ni) * p
end
return sum % prd
end
function mulinv(a::Int, b::Int)
@assert(a % b != 0, "$a is multiple of $b")
@assert(b % a != 0, "$b is multiple of $a")
b0 = b
x0, x1 = 0, 1
if b == 1 return 1 end
while a > 1
q = a ÷ b
a, b = b, a % b
x0, x1 = x1 - q * x0, x0
end
if x1 < 0 x1 += b0 end
return x1
end
@show chineseremainder([3, 5, 7], [2, 3, 2])

View file

@ -1,18 +1,20 @@
func mul_inv(a, b) {
b == 1 && return 1;
var (b0, x0, x1) = (0, 0, 1);
b == 1 && return 1
var (b0, x0, x1) = (0, 0, 1)
while (a > 1) {
(a, b, x0, x1) = (b, a % b, x1 - x0*int(a / b), x0);
};
x1 < 0 ? x1+b0 : x1;
(a, b, x0, x1) = (b, a % b, x1 - x0*int(a / b), x0)
}
x1 < 0 ? x1+b0 : x1
}
func chinese_remainder(*n) {
var N = n«*»;
var N = n«*»
func (*a) {
n.range.map { |i|
var p = int(N / n[i]);
a[i] * mul_inv(p, n[i]) * p;
var p = int(N / n[i])
a[i] * mul_inv(p, n[i]) * p
}.sum
}
}
say chinese_remainder(3, 5, 7)(2, 3, 2)