<commit_before>(ns shokunin.euler)

;; sum of multiples of 3 and 5 < 1000
;; https://projecteuler.net/problem=1

(defn multiple-seq [n]
  (iterate (partial + n) n))

(let [x3 (take-while #(< % 1000) (multiple-seq 3))
      x5 (take-while #(< % 1000) (multiple-seq 5))
      xs (clojure.set/union (set x3) (set x5))]
  (apply + xs))


;; sum of even fibonacci numbers below 4 million
;; https://projecteuler.net/problem=2

(defn fib-seq
  ([] (fib-seq 1 1))
  ([x y] (cons x (lazy-seq (fib-seq y (+ x y))))))

(def answer-2 (reduce + (filter even? (take-while (fn [x] (< x 4000000)) (fib-seq)))))<commit_msg>Work prime number testers/generators<commit_after>(ns shokunin.euler)

;; sum of multiples of 3 and 5 < 1000
;; https://projecteuler.net/problem=1

(defn multiple-seq [n]
  (iterate (partial + n) n))

(let [x3 (take-while #(< % 1000) (multiple-seq 3))
      x5 (take-while #(< % 1000) (multiple-seq 5))
      xs (clojure.set/union (set x3) (set x5))]
  (apply + xs))


;; sum of even fibonacci numbers below 4 million
;; https://projecteuler.net/problem=2

(defn fib-seq
  ([] (fib-seq 1 1))
  ([x y] (cons x (lazy-seq (fib-seq y (+ x y))))))

(def answer-2 (reduce + (filter even? (take-while (fn [x] (< x 4000000)) (fib-seq)))))

;; prime number tools, useful for subsequent problems.

;;; a super naive and expensive check
(defn naive-prime? [n]
  (cond
   (< n 2) false
   (= n 2) true
   (even? n) false
   :else (not-any? #(= 0 (rem n %)) (range 3 (Math/sqrt n) 2))))

;; eratosthenes seive. does not work for large primes because the stack
;; gets way too deep.
(defn dirty-sieve
  ([] (sieve (iterate inc 2)))
  ([s]
  (cons (first s)
        (lazy-seq (sieve (filter #(not= 0 (rem % (first s)))
                                 (rest s)))))))
{'fraction_non_alphanumeric': 0.18600682593856654, 'fraction_numerical': 0.04152445961319681, 'mean_word_length': 3.1098130841121496, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3673988', 'n_tokens_mistral': 755, 'n_tokens_neox': 661, 'n_words': 215}