|
I made some additional math api I found useful. Place these functions in the math class and do whatever you want with them. Please report bugs and possible optimizations.
function int gcd(int a, int b){
var int sign, result, tmp;
if(a = 0){ return b; }
if(b = 0){ return a; }
if(((a < 0) & (b < 0)) | ((a > 0) & (b > 0))){ let sign = 1; }
else{ let sign = -1; }
let a = Math.abs(a);
let b = Math.abs(b);
if(b > a) {
let tmp = b;
let b = a;
let a = tmp;
}
while(~(b = 0)){
let tmp = b;
let b = Math.mod(a,b);
let a = tmp;
}
return a;
}
function int lcm(int a, int b){
return b * (a/Math.gcd(a,b));
}
function int mod(int x, int y){
var int i, q, r,sign;
let sign = 1;
if((x < 0) & (y < 0)){
let sign = 1;
}else{
if((x < 0) | (y < 0)){
let sign = -1;
}
}
let x = Math.abs(x);
let y = Math.abs(y);
if(y = 0){
return 0;
}
if(y > x){ return x; }
if(x = y){return 0;}
let i = 14;
while(~((powers_two[i] & x) = 0)){ let i = i - 1; }
while(~(i < 0)){
let r = r+r;
if(~((powers_two[i] & x) = 0)){
let r = r | 1;
}
else{
let r = r & -2;
}
if(~(r < y)){
let r = r - y;
let q = q | powers_two[i];
}
let i = i - 1;
}
if(sign = -1){ return -r;}
return r;
}
|