// Arup Guha
// 8/25/09
// A program to determine which length scales are the most euphonious.
#include <stdio.h>
#include <math.h>

#define FACTOR 100
#define MAX 100

int main() {
    
    int numNotes;
    
    // Try each number of notes in a scale from 3 to 100.
    for (numNotes = 3; numNotes <= MAX; numNotes++) {
        
        double multiplier = pow(2, 1.0/numNotes);
        double ratio = 1;
        
        int i;
        int numMatches = 0;
        for (i=1; i<numNotes; i++) {
            
            // Figure out the relative frequency of the next note.
            ratio = ratio*multiplier;
            
            // See if this note forms a low-integer ratio with the base note.
            if (fabs(ratio -  3.0/2) < 1.0/FACTOR)
                numMatches++;
            else if (fabs(ratio -  4.0/3) < 1.0/FACTOR)
                numMatches++;
            else if (fabs(ratio -  5.0/4) < 1.0/FACTOR)
                numMatches++;
        }
        
        // Output if this scale is a good one.
        if (numMatches == 3)
            printf("A good scale has %d notes.\n", numNotes);
        
    }
    
    system("PAUSE");
    return 0;
}
