Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
30
Task/Arithmetic-Rational/Zkl/arithmetic-rational-1.zkl
Normal file
30
Task/Arithmetic-Rational/Zkl/arithmetic-rational-1.zkl
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
class Rational{ // Weenie Rational class, can handle BigInts
|
||||
fcn init(_a,_b){ var a=_a, b=_b; normalize(); }
|
||||
fcn toString{
|
||||
if(b==1) a.toString()
|
||||
else "%d//%d".fmt(a,b)
|
||||
}
|
||||
var [proxy] isZero=fcn{ a==0 };
|
||||
fcn normalize{ // divide a and b by gcd
|
||||
g:= a.gcd(b);
|
||||
a/=g; b/=g;
|
||||
if(b<0){ a=-a; b=-b; } // denominator > 0
|
||||
self
|
||||
}
|
||||
fcn abs { a=a.abs(); self }
|
||||
fcn __opNegate{ a=-a; self } // -Rat
|
||||
fcn __opAdd(n){
|
||||
if(Rational.isChildOf(n)) self(a*n.b + b*n.a, b*n.b); // Rat + Rat
|
||||
else self(b*n + a, b); // Rat + Int
|
||||
}
|
||||
fcn __opSub(n){ self(a*n.b - b*n.a, b*n.b) } // Rat - Rat
|
||||
fcn __opMul(n){
|
||||
if(Rational.isChildOf(n)) self(a*n.a, b*n.b); // Rat * Rat
|
||||
else self(a*n, b); // Rat * Int
|
||||
}
|
||||
fcn __opDiv(n){ self(a*n.b,b*n.a) } // Rat / Rat
|
||||
fcn __opEQ(r){ // Rat==Rat, Rat==n
|
||||
if(Rational.isChildOf(r)) a==r.a and b=r.b;
|
||||
else b==1 and a==r;
|
||||
}
|
||||
}
|
||||
8
Task/Arithmetic-Rational/Zkl/arithmetic-rational-2.zkl
Normal file
8
Task/Arithmetic-Rational/Zkl/arithmetic-rational-2.zkl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
foreach p in ([2 .. (2).pow(19)]){
|
||||
sum,limit := Rational(1,p), p.toFloat().sqrt();
|
||||
foreach factor in ([2 .. limit]){
|
||||
if(p%factor == 0) sum+=Rational(1,factor) + Rational(factor,p);
|
||||
}
|
||||
if(sum.b==1) println("Sum of recipr. factors of %6s = %s exactly%s"
|
||||
.fmt(p, sum, (sum==1) and ", perfect." or "."));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue