fork download
  1. #include <stdio.h>
  2.  
  3. unsigned char f(unsigned char x) {
  4. return ((x << 1) ^ (x>127) ^ x ^ (x >> 1) ^ ((x & 1) << 7)) + x;
  5. }
  6.  
  7. void iterate(unsigned char s[5]) {
  8. unsigned char new_s[5];
  9. int i;
  10. new_s[0] = f(s[3]) + s[4] + s[1] + f(s[2]);
  11. new_s[1] = f(s[4]) + s[0] + s[2] + f(s[3]);
  12. new_s[2] = f(s[0]) + s[1] + s[3] + f(s[4]);
  13. new_s[3] = f(s[1]) + s[2] + s[4] + f(s[0]);
  14. new_s[4] = f(s[2]) + s[3] + s[0] + f(s[1]);
  15. for (i=0; i<5; i++) {
  16. s[i] = new_s[i];
  17. }
  18. }
  19.  
  20. int main() {
  21. int i, j;
  22. unsigned char s[5] = { 0, 1, 2, 3, 4 };
  23. unsigned char t[5];
  24.  
  25. /* Start by iterating a million times */
  26. for (i=0; i<1000000; i++) iterate(s);
  27.  
  28. /* Store the updated seed values */
  29. for (i=0; i<5; i++) t[i]=s[i];
  30.  
  31. /* Now see if they come up again */
  32. for (j=1; j<1000000000; j++) {
  33. iterate(s);
  34. if (s[0]==t[0] && s[1]==t[1] && s[2]==t[2] && s[3]==t[3] && s[4]==t[4]) {
  35. printf("Stuck in a loop of length %d\n",j);
  36. for (i=0; i<5; i++) printf("%hhu ",s[i]);
  37. putchar('\n');
  38. return 0;
  39. }
  40. }
  41.  
  42. /* We probably won't end up here */
  43. puts("No repetitions");
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.02s 2008KB
stdin
Standard input is empty
stdout
Stuck in a loop of length 560
134 132 134 60 60