Skip to content

Instantly share code, notes, and snippets.

@purukaushik
Created February 6, 2017 03:20
Show Gist options
  • Save purukaushik/14e34cbf2610fbccc17ec612facf1e55 to your computer and use it in GitHub Desktop.
Save purukaushik/14e34cbf2610fbccc17ec612facf1e55 to your computer and use it in GitHub Desktop.
A non-working prime number generator
(ns clojure-noob.prime
(:require [clojure.string :as string])
(:gen-class))
(defn N-stream []
(defn from
[n]
(cons n (lazy-seq (from (inc n)))))
(from 1))
(take 1000 (N-stream))
;;PRIME GENERATOR HELPERS
;; not eq zero test
(defn nz? [x]
(not (zero? x)))
;; a%b!=0?
(defn modnz? [a b]
(nz? (mod a b)))
;; prime generator
(defn prime-stream
([] (prime-stream (N-stream)))
([xs]
(cons (first xs) (prime-stream (filter #(modnz? % (first xs)) (rest xs) ) ))))
(take 10 (prime-stream))
@purukaushik
Copy link
Author

This runs forever until a java OutOfMemoryError happens ☹️

@flyingmachine
Copy link

Unfortunately I don't have time to analyze this but one thing that popped out at me is that you are defining from within the N-stream function, which seems like it would cause problems.

There are usually folks in the clojure subreddit, http://www.reddit.com/r/clojure, who are able to help folks out. Good luck!

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