If the function is expensive to compute, you might prefer Brent's periodicity detection algorithm, which instead of 1.5xn invocations of the function only requires n + o(log n) invocations and supposedly detects just as fast.
You store [last-n, last-f, next-n] = [0, f(0), 1] and then, for n = 1 to whatever: * compute f(n) * see if it's equal to last-f; if it is, f is or becomes periodic with a period dividing n - last-n. * if n = next-n, change the store to [n, f(n), 2*next-n] * move on to f(n + 1). On Sat, Jun 29, 2013 at 10:43 AM, Simone Mosciatti <mweb....@gmail.com>wrote: > It is very interesting, just a friendly suggestion, if you share code, and > need some help, make sure people can get immediately what is going on. > (READ: comment the code) > > > On Saturday, June 29, 2013 1:34:57 PM UTC+2, Zhemin Lin wrote: >> >> Hi. >> One of my colleagues gave a share about the cycle detecting tortoise-hare >> algorithm >> (wiki<http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare> >> ). >> I translated the script on Wiki from Python to Clojure. >> It worked, but not elegant. I'd like to make it more functional, more >> tasty. >> Any suggestions to help it evolve? >> >> I uploaded my (messy) code here: https://github.com/** >> miaoski/clojure-tortoise-hare<https://github.com/miaoski/clojure-tortoise-hare> >> Thanks a lot! :) >> >> -- > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.