fork(1) download
  1. #include <stdio.h>
  2.  
  3.  
  4. p(n,i,z){return--i?p(n,i,z*i*i%n):z%n;} //is n prime?
  5. c;
  6. f(a,i,n)int*a;{
  7. return i<0||i/n ? c //are we out of array -> return # of steps
  8. :c++>n ? 0 //Did we move more than n-times? -> We can't escape (Pigeonhole principle)
  9. :i&&p(i,i,1) ? f(a,i-a[i],n) //is index i a prime? -> move left
  10. : f(a,a[i],n); // move right
  11. }
  12.  
  13.  
  14. int main(void) {
  15. int a[] = {2,5,6,8,1,2,3};
  16. int b[] = {2,0,2};
  17. int x[] = {14,1,2,5,1,3,51,5,12,3,4,41,15,4,12,243,51,2,14,51,12,11};
  18. printf("%d\n", f(a,3, sizeof(a)/sizeof(*a)));
  19. c = 0;
  20. printf("%d\n", f(b,2, sizeof(b)/sizeof(*b)));
  21. c = 0;
  22. printf("%d\n", f(x,5, sizeof(x)/sizeof(*x)));
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
1
0
6