home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / HAMMING.C < prev    next >
C/C++ Source or Header  |  1996-06-04  |  1KB  |  64 lines

  1. /* Copyright 1992 Digital Equipment Corporation
  2.    All Rights Reserved
  3. */
  4.  
  5. /* Hamming sequence problem in C. */
  6. /* Author: Peter Van Roy */
  7.  
  8. #include <stdio.h>
  9. #define FALSE 0
  10. #define TRUE  1
  11.  
  12. #define MAX 1000
  13.  
  14. #define TWO   0
  15. #define THREE 1
  16. #define FIVE  2
  17.  
  18. int seq[MAX];
  19. int i,i2,i3,i5; 
  20.  
  21. /* Generate the Hamming sequence from 1 to n. */
  22. void hamming(n)
  23. int n;
  24. {
  25.    int min,whichmin;
  26.    int e2,e3,e5;
  27.  
  28.    i=i2=i3=i5=1;
  29.    seq[1]=1;
  30.  
  31.    while (seq[i]<=n) {
  32.       /* Get the candidate integers */
  33.       e2=2*seq[i2];
  34.       e3=3*seq[i3];
  35.       e5=5*seq[i5];
  36.  
  37.       /* Find the minimum candidate. */
  38.       min=e2; whichmin=TWO;
  39.       if (e3<min) { min=e3; whichmin=THREE; }
  40.       if (e5<min) { min=e5; whichmin=FIVE; }
  41.  
  42.       /* Insert if bigger. */
  43.       if (min>seq[i]) {
  44.          i++;
  45.          seq[i]=min;
  46.       }
  47.  
  48.       /* Move to next element. */
  49.       if (whichmin==TWO)   i2++;
  50.       if (whichmin==THREE) i3++;
  51.       if (whichmin==FIVE)  i5++;
  52.    }
  53. }
  54.  
  55. main()
  56. {
  57.    int j;
  58.  
  59.    hamming(1000);
  60.    for (j=1; j<i; j++)
  61.       printf("%d ",seq[j]);
  62.    printf("\n");
  63. }
  64.