| 
View
 

PE052_c

Page history last edited by Kenneth Finnegan 16 years, 5 months ago

// Solution to Project Euler Problem #52

// x-6x have same digits

#include <stdio.h>

main() {

    int i;

   

    for (i=1; i; i++) {

        int digitcnt[6][10];

        int j, k, badflag=0;

        for (j=0; j<6; j++) {

            int temp;

            for (k=0; k<10; k++) { // init the array of digit counts

                digitcnt[j][k] = 0;

            }

            temp = i * (j+1); // create 1x, 2x, 3x, 4x, 5x, 6x

            while (temp) { // increment the count for each digit 0-9

                digitcnt[j][temp%10] += 1;

                temp /= 10;

            }

        }

        for (j=1; j<6 && badflag==0; j++) { // check to see if the counts for each digit are equal

            for (k=0; k<10; k++) {

                if (digitcnt[j][k] != digitcnt[0][k]) {

                    badflag = 1;

                    break;

                }

            }

        }

        if (badflag == 0) {

            printf("Solution found: %d\n", i);

            break;

        }

    }

}

Comments (0)

You don't have permission to comment on this page.