/* This program lists all possible "Chord Scales," where a chord scale is defined as:

1. Consisting entirely of only half steps and whole steps.
2. "Wraping" back to the starting note after exactly one octave.
3. Containing no two consecutive half-steps, even when "wrapping" to the octave above or below.

for more information, click here. */


#include "stdio.h"

 

int main()

{

       int i,j;

       int t,ii;

       int c;

       int prevhalf;

 

       for (i=0; i<0x0100; i++)

       {

              printf("%.3d: ",i);

              t = i;

              c = 0;

 

              prevhalf = 0;

 

              for (j=0; j<8; j++)

              {

                     if (t & 0x80)

                     {

                           printf("W ");

                            c+=2;

                           prevhalf = 0;

                     }

                     else

                     {

                           printf("H ");

 

                           if (prevhalf)

                           {

                                  if (j<=4)

                                         printf("\t");

 

                                  printf("\tconsecutive half steps");

                                  goto skip;

                           }

                           c++;

                           prevhalf = 1;

                     }

 

                     if (c==12)    // got a scale that wraps exactly to one ocatve

                     {

                           if (!((i|t)&0x80))

                           {

                                  printf("\tfirst/last half step");

                                  goto skip;

                           }

 

                           i += (1 << (7-j)) - 1;

 

                           printf("\n");

                           break;

                     }

                     else if (c>12)

                     {

                           printf("\tdoes not hit octave");

skip:

                           ii = (1 << (7-j)) - 1;  // skip identical entries

 

                           if (ii)

                           {

                                  if (ii==1)

                                         printf(" (skip %d)",i+1);

                                  else

                                         printf(" (skip %d-%d)",i+1,i+ii);

 

                                  i += ii;

                           }

 

                           printf("\n");

                            break;

                     }

 

                     t <<= 1;

              }

       }

       return 0;

}