The classic factoring routine of Narayana
We first use modern computers to list a bunch of squares, just to save headache! Traditionally these would be calculated as needed!
> |
lis:=[seq(i^2,i=2..2500)];
|
> |
ch:=proc(n,t,s) local ans,tmp;
if n<>t^2-s then return(`bad start`);
fi;
ans:=[n,t+1,s+2*t+1];
if member(ans[3],lis) then print('success') else print('fails',evalf[2](t+1-sqrt(ans[3])));fi;
op(ans);end;
|
> |
chh:=proc(n,t,s) local ans,tmp,ct;
if n<>t^2-s then return(`bad start`);
fi;
if type(n,even) then RETURN("Must have odd number",n):fi:
ans:=[n,t+1,s+2*t+1];
for ct from 1 to 2000 do;
if ans[2]>n-2 then RETURN("It is prime",n);fi:
if (member(ans[3],lis))
then return(`success`,ans[1],ans[2]+sqrt(ans[3]),ans[2]-sqrt(ans[3]));
fi;
#print(member(ans[3],lis));
ans:=[ans[1],ans[2]+1,ans[3]+2*ans[2]+1];
#print(ans,ct);
od;
print(`it is prime`);ans;
end;
|
> |
ch2:=proc(n,t,s,m) local ans,tmp,ct;
if n<>t^2-s then return(`bad start`);
fi;
if type(n,even) then RETURN("Must have odd number",n):fi:
ans:=[n,t+1,s+2*t+1];
for ct from 1 to m do;
if ans[2]>n-2 then RETURN("It is prime",n);fi:
if (member(ans[3],lis))
then return(`success`,ans[1],ans[2]+sqrt(ans[3]),ans[2]-sqrt(ans[3]));
fi;
if ct>2500 then RETURN("Limit of 2500 exceeded",ct):fi:
#print(member(ans[3],lis));
ans:=[ans[1],ans[2]+1,ans[3]+2*ans[2]+1];
#print(ans,ct);
od;
print(`it is prime`);ans;
end;
|
> |
tch:=proc(n) local t,s;
t:=ceil(sqrt(n));s:=t^2-n;
ch2(n,t,s,n);end:
|
> |
tch(4211);ifactor(4211);
|
Error, (in ch2) invalid input: member received lis, which is not valid for its 2nd argument, s