Skip to content

Instantly share code, notes, and snippets.

@ConorOBrien-Foxx
Created April 17, 2016 01:20
Show Gist options
  • Save ConorOBrien-Foxx/8abf40613d31e1a949065bd3420cc445 to your computer and use it in GitHub Desktop.
Save ConorOBrien-Foxx/8abf40613d31e1a949065bd3420cc445 to your computer and use it in GitHub Desktop.
I don't even - one of my first coding attempts?
function NumberError(msg){
this.name = "NumberError";
this.msg = msg|"";
}
NumberError.prototype=Object.create(Error.prototype);
NumberError.prototype.constructor=NumberError;
Array.prototype.bigUnion = function(){
var a=[];
for(var z=0;z<this.length;z++)a=a.concat(this[z]);
return a;
}
function s(x){
for(var i=2;i<=Math.sqrt(x);i++) if(!(x%i)) return i;
return x;
}
function l(x){// largest divisor of x
for(var i=Math.ceil(Math.sqrt(x));i>=2;i--) if(!(x%i)) return x/i;
return x;
}
function isPrime(x){
return s(x)==x;
}
var memoized = [];
function isB(x){ // Baumic number check
if(memoized[x]) return memoized[x];
memoized[x]=(new Tree(x)).isB();
return memoized[x];
}
function Tree(x,f1=s){
this.top = new node(x);
var i=0;
var c=this.top;
while(!isPrime(c.body)){
c.attach(f1(c.body),c.body/f1(c.body));
c=c.children[1];
}
}
Tree.prototype.disp = function(){
return this.top.disp();
}
Tree.prototype.isB = function(){
var yF=this.family();
return!!(yF.indexOf(yF.length)+1);
}
Tree.prototype.family = function(self){
return this.disp().replace(/ => \(/g,",").replace(/\)/g,"").split(",").map(Number);
}
function node(x){
if(isNaN(parseInt(x))) throw new NumberError("Input ("+x+") not a number")
this.body = x;
this.children = [];
}
function nodify(type,input,z){
switch(type){
case "array":
return input.map(function(e){return e?z?(new node(e)).factor():new node(e):""});
break;
default:
return new node(input);
break;
}
}
function n(input){
return input.map(function(e){return (new node(e)).factor()});
}
node.prototype.disp = function(){
return this.body + (this.children.length?" => ("+this.children.map(function(e){return e?e.disp():"?"})+")":"");
}
node.prototype.right = function(){
if(this.children.length) return this.children[1].right();
return this.body;
}
node.prototype.attach = function(){
a = Array.from(arguments);
for(var _i=0;_i<a.length;_i++){
y=a[_i];
if(!(y instanceof node)) try {y=new node(y)}catch(e){if(e instanceof NumberError)throw Error("Invalid node")}
this.children.push(y);
}
}
var g=0;
node.prototype.factor = function(f=l){
ui=[f(this.body),this.body/f(this.body)];
if(!isPrime(this.body)){
nu=n(ui);
console.log(ui,this.body,nu)
this.children = nu;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment