20-CS-4003-001 Organization of Programming Languages Fall 2017
Lambda

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures           Code

Lambda - Hamming's Sequence

Development and use of control & data abstractions to solve problems efficiently
 
Problem:
Given: Set P of distinct primes.
Produce: Distinct set of integers whose prime factors are in P.
 
Example:  For P = {3,5,11}
Solution: 3,5,9,11,15,25,27,33,45,55,75,81,99,121,125,135 ...
 
Possible Program:
   For all numbers 1,2,3 ... do the following:
      If the number has only prime factors in P, print it.
 
This is unfortunately too slow to be of value.    Code prod-cons   Code PriQ
 
Better Program:
        _  5, 11, 25, 55, 121, 125, ...
       /
      /	  
   3 <
      \ 
       \_  9, 15, 27, 33, 45, 75, 81, 99, 135, ...
           3 * {3, 5, 9, 11, 15, 25, 27, 33, 45, ...}
 
So, generalizing:
   hamming (P) =
      if  P is empty then return the empty list.
      return 
         first (P)
               +
         merge (first (P) * hamming (P), 
                hamming (rest (P))).
 
Using normal Java coding practice this will not work because hamming (P) calls itself which results in stack overflow. But a priority queue will help solve the problem. Each node in the priority queue holds everything that is needed to compute a hamming sequence that is to be merged, namely the multiplying factor and the list of primes. Nodes in the priority queue are ordered by the value of the first number in the sequence.
 
Java Code:    Code

                                    (3)
                                 1*{3,5,11}
                   _______________/     \_______________
                  /                                     \
                (5)                                     (9)
             1*{5,11}                                3*{3,5,11}
         _____/    \____                       _______/     \_____
        /               \                     /                   \
      (11)              (25)                (15)                  (27)
     1*{11}           5*{5,11}            3*{5,11}             9*{3,5,11}
         \           __/   \__           __/   \__             __/   \__
          \         /         \         /         \           /         \
         (121)    (55)      (125)     (33)       (75)       (45)       (81)
        11*{11}  5*{11}   25*{5,11}  3*{11}    15*{5,11}  9*{5,11}  27*{3,5,11}
             \       \      /   \        \       /   \     /   \      /   \
              ..      ..  ..     ..       ..   ..     .. ..     ..  ..     ..


class Hamming_1 extends JFrame implements ActionListener {
   JTextArea text;
   JComboBox num;
   JButton go;
   JTextField pl;

   public Hamming_1 () {
      ...
   }

   public void ham(BigInteger p[]) {
      Node n = new Node((BigInteger)p[1], 1, new BigInteger("1"));
      PQueue q = new PQueue();
      q.insert(n);

      text.setText("");
      int iters = Integer.parseInt((String)num.getSelectedItem());
      int i=1;
      while (i <= iters) {
         n = (Node)q.dequeue();
         text.append((i++)+":\t"+n.fst+"\n");

         q.insert(new Node(n.mul.multiply(p[n.idx]).multiply(p[n.idx]),
			   n.idx,
			   n.mul.multiply(p[n.idx])));

         if (p[n.idx+1] != null)
            q.insert(new Node(n.mul.multiply(p[n.idx+1]),
			      n.idx+1,
			      n.mul));

      }
   }

   public void actionPerformed (ActionEvent evt) {
      BigInteger p[] = new BigInteger[5];
      p[0] = null; p[1] = new BigInteger("3");
      p[2] = new BigInteger("5"); p[3] = new BigInteger("11");
      p[4] = null;
      ham(p);
   }
}