/* Compile with: * c++ -O3 -Wall -pedantic -o additive-stream additive-stream.cc \ * -lreadline -lcurses * * It should be pretty obvious what you need to uncomment * to see the actual stream itself, if you want it. */ #include #include #include #include #include #include char * readline (const char *prompt); int main( int argc, char **argv ) { int length, i, j, next; double loopCount; // must be type double to count very very high! int * list; int * originalList; int foundLoop; printf("length = %d\n", argc - 1); if (argc == 1) { printf("\n"); printf("Call with a sequence of digits, e.g.:\n"); printf(" %s 1 2 3 4\n", argv[0]); printf("\n"); exit(1); } list = (int *) malloc((argc - 1)*sizeof(int)); originalList = (int *) malloc((argc - 1)*sizeof(int)); for (i = 1; i < argc; i++) { list[i-1] = atoi(argv[i])%10; } length = argc - 1; for (i = 0; i < length; i++) { printf("%d", list[i]); originalList[i] = list[i]; } printf("\n\n"); /* Uncomment these three lines to first print the input seed */ // for (i = 0; i < length; i++) { // printf("%d", list[i]); // } foundLoop = 0; loopCount = 0.0; while(foundLoop == 0) { next = (list[0] + list[1]) % 10; for (i = 0; i < length-1; i++) { list[i] = list[i+1]; } list[length-1] = next; /* And uncomment these two lines to see the generated digits */ // printf("%d", next); // fflush(stdout); foundLoop = 1; j = 0; for (i = 0; i < length; i++) { if (list[i] != originalList[i]) { foundLoop = 0; j++; } } loopCount++; if (j == 1) { for (i = 0; i < length; i++) { printf("%d", list[i]); } printf(" => close hit at position %20.0f\n", loopCount); } } printf("\n Found loop at position %.0f\n", loopCount); exit(0); }