with(numtheory):
fjv6:=proc(n,m)
local i,j,k,l,x,x1,y,y1,z,z1,w,stopp,s,t,u,v,A,F,G,out;
i:=n;stopp:=0;
x1:=2^(m*i+6)-1; x:=x1 mod i;j:=0;
while stopp=0 and j<=m*i+5 do
l:=j;
while stopp=0 and l<=m*i+4 do
k:=l;
while stopp=0 and k<=m*i+3 do
s:=k;
while stopp=0 and s<=m*i+2 do
t:=s;
while stopp=0 and t<=m*i+1 do
v:=t;
while stopp=0 and v<=m*i do y1:=2^(m*i+5-j)+2^(m*i+4-l)+2^(m*i+3-k)+2^(m*i+2-s)+2^(m*i+1-t)+2^(m*i-v); y:=y1 mod i;
if y=x then z:=(x1-y1)/i;out:=[m*i, z];
stopp:=1;
fi;
v:=v+1;od;t:=t+1;od;s:=s+1;od; k:=k+1; od;l:=l+1;od;j:=j+1;od;
if stopp=0 then out:=[m*i,0];fi;
out;
end:
formula:=proc(n)
local x,y,B,expon,outputis, theOddFactor;
x:=n+1;B:=ifactors(x);expon:=B[2][1][2];theOddFactor:=(n+1)/2^expon;
y:=isprime(n);
if theOddFactor=1 and y=true then outputis:=[n,(2^(n+expon-1)+2^n-2^(n-expon)-1)/n];fi;
if theOddFactor>1 or y=false then outputis:=fjv6(n,1);fi;
lprint(outputis[1],outputis[2]);
end:
fjfromis6:=proc(n,m)
local k,B,expon, theoddfac,par,stopp,av,sub;
av:=0;for k from n to m do
par:=k mod 2;
if par=0 then B:=ifactors(k);expon:=B[2][1][2];theoddfac:=k/2^expon;
sub:=fjv6(theoddfac,2^expon);
lprint(sub[1], sub[2]); fi;
stopp:=0;
if par=1 then formula(k); fi;
od;
end:
fjfromis6(1,185);
# Alternative:
F:= proc(k,x,n,dmax)
option remember;
local d,z,v;
if k = 0 then
if x = 0 then return 0 else return infinity fi
end;
for d from k-1 to dmax do
v:= procname(k-1,(x - 2^d) mod n,n, d-1) ;
if v < 2^d then return v + 2^d fi
od;
infinity;
end proc:
seq(F(n,0,n,infinity)/n, n=1..100); # Robert Israel, Aug 26 2015
F[k_, x_, n_, dMax_] := F[k, x, n, dMax] = Module[{d, z, v}, If[k == 0, If[x == 0, Return[0], Return[Infinity]]]; For[d = k - 1, d <= dMax, d++, v = F[k - 1, Mod[x - 2^d, n], n, d - 1]; If[v < 2^d, Return[v + 2^d]]]; Infinity];
Table[F[n, 0, n, Infinity]/n, {n, 1, 32}] (* Jean-François Alcover, Jun 22 2020, after Robert Israel *)
Comments