Skip to content

Instantly share code, notes, and snippets.

@denlab
Created September 21, 2011 12:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save denlab/1231894 to your computer and use it in GitHub Desktop.
Save denlab/1231894 to your computer and use it in GitHub Desktop.
coding-dojo-20110921
(defn primes [n] nil
(reverse
(loop [candidate 2 current n acc []]
(if (zero? current)
acc
(if (every? #(not= 0 (rem candidate %)) acc)
(recur (inc candidate) (dec current) (cons candidate acc))
(recur (inc candidate) current acc))))))
(fact
(primes 1) => [2]
(primes 2) => [2 3]
(primes 3) => [2 3 5])
@denlab
Copy link
Author

denlab commented Sep 22, 2011 via email

@denlab
Copy link
Author

denlab commented Sep 22, 2011 via email

@laurentpetit
Copy link

avec iterate, je ne vois pas comment faire pour passer l'accumulateur. A moins que le iterate itere autour du couple (candidate / accumulateur ), et que l'iterate soit "coiffé" par un map qui retourne à chaque niveau le (first) de l'accumulateur :
(map (comp first second) (iterate (fn [[candidate acc]] ....) [2 []])
mais ca commence p-e alors à faire beaucoup de créations d'objets intermédiaires (enfin tant qu'on n'a pas mesuré, bien sûr ...)

@cgrand
Copy link

cgrand commented Sep 23, 2011

Menfin le décompte des objets intermédiaires quand on utilise un algo naïf c'est pas important. Là on est dans une approche parnassienne du code. https://gist.github.com/1236838#comments

"Le code pour le code."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment