Skip to content

Commit

Permalink
Added (not yet working) code for #540.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjray committed Jan 30, 2016
1 parent b8d97b1 commit f4099c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/math.combinatorics "0.1.1"]])
[org.clojure/math.combinatorics "0.1.1"]
[org.clojure/math.numeric-tower "0.0.4"]])
29 changes: 29 additions & 0 deletions src/projecteuler/p540.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns projecteuler.core
(:require [clojure.math.numeric-tower :as math]))

;; https://projecteuler.net/problem=540

; NOT WORKING YET.
;
; Correct answer for P(20), incorrect for P(1000000). Dies with memory/GC error
; when run for P(3141592653589793N).

(defn- euclid-primitive [m n]
(let [a (- (* m m) (* n n))
b (* 2 m n)
c (+ (* m m) (* n n))]
(vec (sort (list a b c)))))
(defn- get-euclid-prims [max-p]
(let [max-n (int (Math/sqrt (/ max-p 2)))
max-m (inc max-n)]
(loop [n 1, c #{}]
(cond
(> n max-n) c
:else
(recur (inc n)
(set (concat c
(filter #(<= (last %) max-p)
(map #(euclid-primitive % n)
(filter #(= 1 (math/gcd % n))
(range (inc n)
(inc max-m) 2)))))))))))

0 comments on commit f4099c4

Please sign in to comment.