Hamming Sequence
04-18-2017, 06:53 PM (This post was last modified: 04-18-2017 07:37 PM by Han.)
Post: #1 Han Senior Member Posts: 1,843 Joined: Dec 2013
Hamming Sequence
Generates the k-th Hamming number. See this post: http://hpmuseum.org/forum/thread-8178.html

Code:
EXPORT HAMMING(k) BEGIN   local x2:=2;   local x3:=3;   local x5:=5;   local m,n;   local j2:=1;   local j3:=1;   local j5:=1;   local h:=;   h(k):=1;   h(1):=1; h(2):=1;   for n from 2 to k do     m:=min(x2,x3,x5);     h(n):=m;     if m==x2 then       j2:=j2+1; x2:=2*h(j2);     end;     if m==x3 then       j3:=j3+1; x3:=3*h(j3);     end;     if m==x5 then       j5:=j5+1; x5:=5*h(j5);     end;    end;   return(h(k));  END;

CAS version:

Code:
#cas HAMMING(k):= BEGIN   local x2:=2;   local x3:=3;   local x5:=5;   local m,n;   local j2:=1;   local j3:=1;   local j5:=1;   local h:=makelist(1,m,k);   for n from 2 to k do     m:=min(x2,x3,x5);     h[n]:=m;     if m==x2 then       j2:=j2+1; x2:=2*h[j2];     end;     if m==x3 then       j3:=j3+1; x3:=3*h[j3];     end;     if m==x5 then       j5:=j5+1; x5:=5*h[j5];     end;    end;   return(h[k]);  END; #end

Graph 3D | QPI | SolveSys
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)