Post Reply 
Hamming Sequence
04-18-2017, 06:53 PM (This post was last modified: 04-18-2017 07:37 PM by Han.)
Post: #1
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:=[1];

  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
Find all posts by this user
Quote this message in a reply
Post Reply 




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