fork download
  1. ; length of a cycle
  2.  
  3. (define (f n)
  4. (set! counter (+ counter 1))
  5. (case n
  6. ((0 1) 6)
  7. ((2 8) 0)
  8. ((3) 1)
  9. ((4 7) 4)
  10. ((5 6) 3)))
  11.  
  12. (define counter 0)
  13.  
  14. (define (floyd f x0)
  15. (let loop ((tortoise (f x0)) (hare (f (f x0))))
  16. (if (not (= tortoise hare))
  17. (loop (f tortoise) (f (f hare)))
  18. (let loop ((mu 0) (tortoise x0) (hare hare))
  19. (if (not (= tortoise hare))
  20. (loop (+ mu 1) (f tortoise) (f hare))
  21. (let loop ((lam 1) (hare (f tortoise)))
  22. (if (not (= tortoise hare))
  23. (loop (+ lam 1) (f hare))
  24. (values lam mu))))))))
  25.  
  26. (call-with-values
  27. (lambda () (floyd f 2))
  28. (lambda (lam mu)
  29. (display lam) (newline)
  30. (display mu) (newline)))
  31.  
  32. (display counter) (newline)
  33.  
  34. (define (brent f x0)
  35. (let loop ((power 1) (lam 1) (tortoise x0) (hare (f x0)))
  36. (if (not (= tortoise hare))
  37. (if (= power lam)
  38. (loop (* power 2) 1 hare (f hare))
  39. (loop power (+ lam 1) tortoise (f hare)))
  40. (let loop ((i 0) (hare x0))
  41. (if (< i lam)
  42. (loop (+ i 1) (f hare))
  43. (let loop ((mu 0) (tortoise x0) (hare hare))
  44. (if (not (= tortoise hare))
  45. (loop (+ mu 1) (f tortoise) (f hare))
  46. (values lam mu))))))))
  47.  
  48. (set! counter 0)
  49.  
  50. (call-with-values
  51. (lambda () (brent f 2))
  52. (lambda (lam mu)
  53. (display lam) (newline)
  54. (display mu) (newline)))
  55.  
  56. (display counter) (newline)
Success #stdin #stdout 0.02s 42848KB
stdin
Standard input is empty
stdout
3
2
16
3
2
13