Created
February 6, 2017 03:20
-
-
Save purukaushik/14e34cbf2610fbccc17ec612facf1e55 to your computer and use it in GitHub Desktop.
A non-working prime number generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
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
This runs forever until a java OutOfMemoryError happens☹️