Created
April 1, 2012 09:02
-
-
Save Mjiig/2273876 to your computer and use it in GitHub Desktop.
Project euler #52
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#include <math.h> | |
void digits ( char digits[10], int n ) | |
{ | |
int top=(int)log10((float)n)+1; | |
int i=0; | |
memset(digits, 0, 10); | |
while(i<top) | |
{ | |
digits[n%10]++; | |
n/=10; | |
i++; | |
} | |
} | |
bool answer( int n ) | |
{ | |
char digits1[10]; | |
char digits2[10]; | |
int i; | |
digits(digits1, n); | |
for(i=2; i<=6; i++) | |
{ | |
digits(digits2, i*n); | |
if(memcmp(digits1, digits2, 10)) //true if the values are different | |
{ | |
return false; //the number is not the answer | |
} | |
} | |
return true; // if we got this far the number is the answer | |
} | |
int main ( void ) | |
{ | |
int i=0; | |
while(!answer(++i)); | |
printf("%d\n", i); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment