Skip to content

Instantly share code, notes, and snippets.

@lotabout
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotabout/7137b923062dfd7807db to your computer and use it in GitHub Desktop.
Save lotabout/7137b923062dfd7807db to your computer and use it in GitHub Desktop.
Project Euler, Problem 26.
#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