<commit_before>(ns euler.problem64
  "https://projecteuler.net/problem=64

  The first ten continued fraction representations of (irrational) square roots are:

      sqrt(2)=[1;(2)]          period=1
      sqrt(3)=[1;(1,2)]        period=2
      sqrt(5)=[2;(4)]          period=1
      sqrt(6)=[2;(2,4)]        period=2
      sqrt(7)=[2;(1,1,1,4)]    period=4
      sqrt(8)=[2;(1,4)]        period=2
      sqrt(10)=[3;(6)]         period=1
      sqrt(11)=[3;(3,6)]       period=2
      sqrt(12)=[3;(2,6)]       period=2
      sqrt(13)=[3;(1,1,1,1,6)] period=5

  Exactly four continued fractions, for N <= 13, have an odd period. How many
  continued fractions for N <= 10000 have an odd period?"
  (:require [clojure.math.numeric-tower :as math]))

(defn continued-fraction-sqrt [S]
  ;; https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Continued_fraction_expansion
  ;; Using variables S, m, d, a as in the URL above.
  (let [root (math/sqrt S)]
    (if (= root (math/floor root))
      [root]
      (loop [seen []
             m 0
             d 1
             a (math/floor (math/sqrt S))]
        (let [next-seen (conj seen {:m m, :d d, :a a})
              next-m (- (* d a) m)
              next-d (/ (- S (math/expt next-m 2)) d)
              next-a (math/floor (/ (+ (math/floor (math/sqrt S)) next-m) next-d))]
          ;; If the pattern repeats, return all the 'a' variables collected in order.
          ;; Otherwise, continue the recursion.
          (if (some #(= % {:m next-m, :d next-d, :a next-a}) next-seen)
            (vec (for [x next-seen] (get x :a)))
            (recur next-seen
                   next-m
                   next-d
                   next-a)))))))

(defn problem64 []
  (let [cont-fracs (for [i (range 2 10001)] (continued-fraction-sqrt i))]
    (count (filter #(odd? (count (rest %))) cont-fracs))))<commit_msg>Use 'map' instead of 'for' in problem64.clj<commit_after>(ns euler.problem64
  "https://projecteuler.net/problem=64

  The first ten continued fraction representations of (irrational) square roots are:

      sqrt(2)=[1;(2)]          period=1
      sqrt(3)=[1;(1,2)]        period=2
      sqrt(5)=[2;(4)]          period=1
      sqrt(6)=[2;(2,4)]        period=2
      sqrt(7)=[2;(1,1,1,4)]    period=4
      sqrt(8)=[2;(1,4)]        period=2
      sqrt(10)=[3;(6)]         period=1
      sqrt(11)=[3;(3,6)]       period=2
      sqrt(12)=[3;(2,6)]       period=2
      sqrt(13)=[3;(1,1,1,1,6)] period=5

  Exactly four continued fractions, for N <= 13, have an odd period. How many
  continued fractions for N <= 10000 have an odd period?"
  (:require [clojure.math.numeric-tower :as math]))

(defn continued-fraction-sqrt [S]
  ;; https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Continued_fraction_expansion
  ;; Using variables S, m, d, a as in the URL above.
  (let [root (math/sqrt S)]
    (if (= root (math/floor root))
      [root]
      (loop [seen []
             m 0
             d 1
             a (math/floor (math/sqrt S))]
        (let [next-seen (conj seen {:m m, :d d, :a a})
              next-m (- (* d a) m)
              next-d (/ (- S (math/expt next-m 2)) d)
              next-a (math/floor (/ (+ (math/floor (math/sqrt S)) next-m) next-d))]
          ;; If the pattern repeats, return all the 'a' variables collected in order.
          ;; Otherwise, continue the recursion.
          (if (some #(= % {:m next-m, :d next-d, :a next-a}) next-seen)
            (vec (for [x next-seen] (get x :a)))
            (recur next-seen
                   next-m
                   next-d
                   next-a)))))))

(defn problem64 []
  (let [cont-fracs (map continued-fraction-sqrt (range 2 10001))]
    (count (filter #(odd? (count (rest %))) cont-fracs))))
{'fraction_non_alphanumeric': 0.1681721572794899, 'fraction_numerical': 0.04197662061636557, 'mean_word_length': 2.075980392156863, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2872204', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1299, 'n_words': 353}