Last active
August 29, 2015 14:01
-
-
Save lotabout/7137b923062dfd7807db to your computer and use it in GitHub Desktop.
Project Euler, Problem 26.
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 <malloc.h> | |
#include <string.h> | |
int num_of_cycle(int n) | |
{ | |
int ret; /* return value */ | |
int *rem_seen = (int *)malloc(n * sizeof(*rem_seen)); | |
memset(rem_seen, 0, n*sizeof(*rem_seen)); | |
int rem = 1; | |
int length = 1; | |
while (rem != 0) { | |
rem = (rem * 10) % n; | |
if (rem_seen[rem] > 0) { | |
ret = length - rem_seen[rem]; | |
break; | |
} else { | |
rem_seen[rem] = length; | |
length ++; | |
} | |
} | |
free(rem_seen); | |
return ret; | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
int i; | |
int max = 0; | |
int max_indx = 0; | |
for (i = 2; i < 1000; i++) { | |
int cycles = num_of_cycle(i); | |
if (cycles > max) { | |
max = cycles; | |
max_indx = i; | |
} | |
} | |
printf("%d\n", max_indx); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment