2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
22
Task/Hailstone-sequence/PARI-GP/hailstone-sequence-2.pari
Normal file
22
Task/Hailstone-sequence/PARI-GP/hailstone-sequence-2.pari
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
\\ Get vector with Collatz sequence for the specified starting number.
|
||||
\\ Limit vector to the lim length, or less, if 1 (one) term is reached (when lim=0).
|
||||
\\ 3/26/2016 aev
|
||||
Collatz(n,lim=0)={
|
||||
my(c=n,e=0,L=List(n)); if(lim==0, e=1; lim=n*10^6);
|
||||
for(i=1,lim, if(c%2==0, c=c/2, c=3*c+1); listput(L,c); if(e&&c==1, break));
|
||||
return(Vec(L)); }
|
||||
Collatzmax(ns,nf)={
|
||||
my(V,vn,mxn=1,mx,im=1);
|
||||
print("Search range: ",ns,"..",nf);
|
||||
for(i=ns,nf, V=Collatz(i); vn=#V; if(vn>mxn, mxn=vn; im=i); kill(V));
|
||||
print("Hailstone/Collatz(",im,") has the longest length = ",mxn);
|
||||
}
|
||||
|
||||
{
|
||||
\\ Required tests:
|
||||
print("Required tests:");
|
||||
my(Vr,vrn);
|
||||
Vr=Collatz(27); vrn=#Vr;
|
||||
print("Hailstone/Collatz(27): ",Vr[1..4]," ... ",Vr[vrn-3..vrn],"; length = ",vrn);
|
||||
Collatzmax(1,100000);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue