├── .gitignore ├── README.md ├── e1 ├── README.md ├── project.clj ├── spec │ └── e1 │ │ └── core_spec.clj └── src │ └── e1 │ └── core.clj ├── e2_Fibonacci ├── Turtle │ ├── .gitignore │ ├── LICENSE │ ├── README.md │ ├── project.clj │ ├── spec │ │ └── turtle_graphics │ │ │ ├── core_spec.clj │ │ │ └── turtle_spec.clj │ └── src │ │ └── turtle_graphics │ │ ├── core.clj │ │ └── turtle.clj └── e2_problem │ ├── .gitignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── doc │ └── intro.md │ ├── e2_problem.iml │ ├── project.clj │ ├── src │ └── e2_problem │ │ └── core.clj │ └── test │ └── e2_problem │ └── core_test.clj ├── e3_primeFactors ├── prime_factors │ ├── .gitignore │ ├── README.md │ ├── project.clj │ ├── spec │ │ └── prime_factors │ │ │ └── core_spec.clj │ └── src │ │ └── prime_factors │ │ └── core.clj └── sieve │ ├── src │ └── sieve │ │ └── Sieve.java │ └── test │ └── sieve │ └── SieveTest.java ├── e4_palindromic_numbers ├── .gitignore ├── README.md ├── project.clj ├── spec │ └── e4_palindromic_numbers │ │ └── core_spec.clj └── src │ ├── e4_palindromic_numbers │ └── core.clj │ └── euler4.pal ├── e5-smallest-multiple ├── project.clj ├── spec │ └── smallest_multiple_spec.clj └── src │ └── smallest_multiple.clj ├── e6-problems-galore ├── e6 ├── e6.c └── euler6 │ ├── .gitignore │ ├── README.md │ ├── project.clj │ ├── spec │ └── euler6 │ │ └── core_spec.clj │ └── src │ └── euler6 │ └── core.clj ├── e7-1001st-prime ├── README.md ├── project.clj ├── sieve │ ├── src │ │ └── sieve │ │ │ └── Sieve.java │ └── test │ │ └── sieve │ │ └── SieveTest.java ├── spec │ └── e7_1001st_prime │ │ └── core_spec.clj └── src │ └── e7_1001st_prime │ └── core.clj └── e8-largest-product-in-a-series ├── .gitignore ├── README.md ├── data.txt ├── project.clj ├── spec └── e8_largest_product_in_a_series │ └── core_spec.clj └── src └── e8_largest_product_in_a_series └── core.clj /.gitignore: -------------------------------------------------------------------------------- 1 | **/target 2 | **/lib 3 | **/classes 4 | **/checkouts 5 | **/pom.xml 6 | **/*.jar 7 | **/*.class 8 | **/.lein-deps-sum 9 | **/.lein-failures 10 | **/.lein-plugins 11 | **/.lein-repl-history 12 | **/*.iml 13 | **/.idea 14 | **/.DS_Store 15 | .DS_Store 16 | *.zip 17 | .nrepl-port 18 | .idea 19 | *.iml 20 | e3_primeFactors/sieve/out/ 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Euler Project in Clojure. 2 | 3 | -------------------------------------------------------------------------------- /e1/README.md: -------------------------------------------------------------------------------- 1 | # e1 2 | -------------------------------------------------------------------------------- /e1/project.clj: -------------------------------------------------------------------------------- 1 | (defproject e1 "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :main e1.core 7 | :dependencies [[org.clojure/clojure "1.8.0"] 8 | [org.clojure/math.combinatorics "0.1.6"] 9 | ] 10 | :profiles {:dev {:dependencies [[speclj "3.3.2"]]}} 11 | :plugins [[speclj "3.3.2"]] 12 | :test-paths ["spec"]) 13 | -------------------------------------------------------------------------------- /e1/spec/e1/core_spec.clj: -------------------------------------------------------------------------------- 1 | (ns e1.core-spec 2 | (:require [speclj.core :refer :all] 3 | [e1.core :refer :all])) 4 | 5 | (defn p2 [n] (+ 2 n)) 6 | 7 | (describe "Sum of multiples of 3 and 5" 8 | (it "should be zero if limit less than 3" 9 | (should= 0 (sum-multiples-3-5 0)) 10 | (should= 0 (sum-multiples-3-5 1)) 11 | (should= 0 (sum-multiples-3-5 2))) 12 | 13 | (it "should be 3 for 3 and 4" 14 | (should= 3 (sum-multiples-3-5 3)) 15 | (should= 3 (sum-multiples-3-5 4))) 16 | 17 | (it "should be 8 for 5" 18 | (should= 8 (sum-multiples-3-5 5))) 19 | 20 | (it "should be 14 for 6, 7, and 8" 21 | (should= 14 (sum-multiples-3-5 6)) 22 | (should= 14 (sum-multiples-3-5 7)) 23 | (should= 14 (sum-multiples-3-5 8))) 24 | 25 | (it "should be 23 for 9" 26 | (should= 23 (sum-multiples-3-5 9))) 27 | 28 | (it "should be 33 for 10 and 11" 29 | (should= 33 (sum-multiples-3-5 10)) 30 | (should= 33 (sum-multiples-3-5 11))) 31 | 32 | (it "should be 45 for 12, 13, and 14" 33 | (should= 45 (sum-multiples-3-5 12)) 34 | (should= 45 (sum-multiples-3-5 13)) 35 | (should= 45 (sum-multiples-3-5 14))) 36 | 37 | (it "should be 60 for 15, 16, and 17" 38 | (should= 60 (sum-multiples-3-5 15)) 39 | (should= 60 (sum-multiples-3-5 16)) 40 | (should= 60 (sum-multiples-3-5 17)))) 41 | 42 | (describe "General sum of many multiples" 43 | (it "Should calculate multiples of 3 and 5 as above" 44 | (doseq [n (range 50)] 45 | (should= (sum-multiples-3-5 n) 46 | (sum-multiples [3 5] n)))) 47 | 48 | (it "should be zero if no factors" 49 | (should= 0 (sum-multiples [] 1000))) 50 | 51 | (it "should be 5050 if summing multiples of 1 to 100" 52 | (should= 5050 (sum-multiples [1] 100)))) 53 | 54 | (describe "Fast sum of multiples" 55 | (it "should work for one multiple" 56 | (should= (sum-multiples [2] 100) 57 | (fast-sum-multiples [2] 100))) 58 | 59 | (it "should work for two multiples" 60 | (should= (sum-multiples [3 5] 100) 61 | (fast-sum-multiples [3 5] 100)))) 62 | 63 | (describe "Fast sum of multiples" 64 | (context "one factor" 65 | (for [_ (range 100)] 66 | (let [limit 5000 67 | factors [(inc (rand-int 20))]] 68 | (it (pr-str factors limit " should match") 69 | (should= (sum-multiples factors limit) 70 | (fast-sum-multiples factors limit)))))) 71 | 72 | (context "two factors" 73 | (for [_ (range 100)] 74 | (let [limit 5000 75 | factors [(inc (rand-int 20)) 76 | (inc (rand-int 20))]] 77 | (it (pr-str factors limit " should match") 78 | (should= (sum-multiples factors limit) 79 | (fast-sum-multiples factors limit)))))) 80 | 81 | (context "three factors" 82 | (it "works for 2 3 5" 83 | (should= (sum-multiples [2 3 5] 30) 84 | (fast-sum-multiples [2 3 5] 30))) 85 | 86 | (for [_ (range 100)] 87 | (let [limit 5000 88 | factors [(inc (rand-int 30)) 89 | (inc (rand-int 30)) 90 | (inc (rand-int 30))] 91 | expected (sum-multiples factors limit) 92 | actual (fast-sum-multiples factors limit)] 93 | (it (pr-str factors limit " should match: " 94 | (- actual expected)) 95 | (should= expected actual)))) 96 | ) 97 | 98 | (context "four factors" 99 | (it "works for 2 3 5 7" 100 | (should= (sum-multiples [2 3 5 7] 210) 101 | (fast-sum-multiples [2 3 5 7] 210))) 102 | 103 | (for [_ (range 100)] 104 | (let [limit 10000 105 | factors [(p2 (rand-int 30)) 106 | (p2 (rand-int 30)) 107 | (p2 (rand-int 30)) 108 | (inc (rand-int 30))] 109 | expected (sum-multiples factors limit) 110 | actual (fast-sum-multiples factors limit)] 111 | (it (pr-str factors limit " should match: " 112 | (- actual expected)) 113 | (should= expected actual)))))) 114 | 115 | (describe "General Fast sum of multiples" 116 | (it "works for 2 3 5 7 11" 117 | (let [factors [2 3 5 7 11] 118 | limit (reduce * factors)] 119 | (should= (sum-multiples factors limit) 120 | (fast-sum-multiples factors limit)))) 121 | 122 | (it "works for 2 3 5 7 11" 123 | (let [factors [2 3 5 7 11 13] 124 | limit (reduce * factors)] 125 | (should= (sum-multiples factors limit) 126 | (fast-sum-multiples factors limit)))) 127 | 128 | (for [_ (range 100 )] 129 | (let [n-factors (+ 2 (rand-int 6)) 130 | factors (repeatedly 131 | n-factors 132 | #(inc (rand-int 30))) 133 | limit (rand-int 100000)] 134 | (it (str "should match " (pr-str factors) " " limit) 135 | (should= (sum-multiples factors limit) 136 | (fast-sum-multiples factors limit)))))) 137 | 138 | (describe "Timing" 139 | (it "should be faster." 140 | (let [expected (time 141 | (sum-multiples 142 | [2 3 5 7 11] 143 | 1000000)) 144 | actual (time 145 | (fast-sum-multiples 146 | [2 3 5 7 11] 147 | 1000000)) 148 | ] 149 | (should= expected actual)))) 150 | -------------------------------------------------------------------------------- /e1/src/e1/core.clj: -------------------------------------------------------------------------------- 1 | (ns e1.core 2 | (:require [clojure.math.combinatorics :as comb] 3 | [clojure.set :as s])) 4 | 5 | (defn sum-multiples-3-5 [limit] 6 | (let [threes (range 3 (inc limit) 3) 7 | fives (range 5 (inc limit) 5)] 8 | (reduce + (set (concat threes fives))))) 9 | 10 | (defn sum-multiples [factors limit] 11 | (->> factors 12 | (mapcat #(range % (inc limit) %)) 13 | set 14 | (reduce +))) 15 | 16 | (defn sum-up-to [n] 17 | (/ (+ n (* n n)) 2)) 18 | 19 | (defn- floor [n] (int (Math/floor n))) 20 | 21 | (defn- sum-multiple-up-to [m n] 22 | (* m (sum-up-to (floor (/ n m))))) 23 | 24 | (defn- get-multiple [[a b]] 25 | (let [f (min a b) 26 | m (max a b)] 27 | (if (zero? (rem m f)) 28 | m 29 | nil))) 30 | 31 | (defn remove-multiples [factors] 32 | (let [factors (set factors) 33 | pairs (comb/combinations factors 2) 34 | multiples (set (keep get-multiple pairs)) 35 | factors (s/difference factors multiples)] 36 | (seq factors))) 37 | 38 | (defn gcd 39 | [a b] 40 | (if (zero? b) 41 | a 42 | (recur b, (mod a b)))) 43 | 44 | (defn lcm 45 | [a b] 46 | (/ (* a b) (gcd a b))) 47 | 48 | (defn lcmv [& v] (reduce lcm v)) 49 | 50 | (defn sum-of-multiples-of-lcm-of-tuples [condition subsets limit] 51 | (->> subsets 52 | (remove #(empty? %)) 53 | (filter #(condition (count %))) 54 | (map #(sum-multiple-up-to (apply lcmv %) limit)) 55 | (reduce +))) 56 | 57 | (defn fast-sum-multiples [factors limit] 58 | (let [subsets (comb/subsets (remove-multiples factors)) 59 | odds (sum-of-multiples-of-lcm-of-tuples odd? subsets limit) 60 | evens (sum-of-multiples-of-lcm-of-tuples even? subsets limit)] 61 | (- odds evens))) 62 | -------------------------------------------------------------------------------- /e2_Fibonacci/Turtle/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .lein-repl-history 3 | target/ 4 | *.iml 5 | **/*.zip 6 | 7 | -------------------------------------------------------------------------------- /e2_Fibonacci/Turtle/LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of Washington and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /e2_Fibonacci/Turtle/README.md: -------------------------------------------------------------------------------- 1 | # Turtle Graphics Processor 2 | 3 | A framework for Turtle Graphics. 4 | 5 | 6 | -------------------------------------------------------------------------------- /e2_Fibonacci/Turtle/project.clj: -------------------------------------------------------------------------------- 1 | (defproject Turtle "0.1.0-SNAPSHOT" 2 | :description "Turtle Graphics Processor" 3 | :main turtle-graphics.core 4 | :dependencies [[org.clojure/clojure "1.10.1"] 5 | [quil "2.5.0"] 6 | [org.clojure/core.async "1.3.610"] 7 | [org.clojure/tools.namespace "1.1.0"]] 8 | :profiles {:dev {:dependencies [[speclj "3.3.2"]]}} 9 | :plugins [[speclj "3.3.2"]] 10 | :test-paths ["spec"]) 11 | -------------------------------------------------------------------------------- /e2_Fibonacci/Turtle/spec/turtle_graphics/core_spec.clj: -------------------------------------------------------------------------------- 1 | (ns turtle-graphics.core-spec 2 | (:require [speclj.core :refer :all])) 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /e2_Fibonacci/Turtle/spec/turtle_graphics/turtle_spec.clj: -------------------------------------------------------------------------------- 1 | (ns turtle-graphics.turtle-spec 2 | (:require [speclj.core :refer :all] 3 | [turtle-graphics.turtle :as t])) 4 | 5 | (declare turtle) 6 | 7 | (describe "Turtle Update" 8 | (with turtle (-> (t/make) (t/position [1.0 1.0]) (t/heading 1.0))) 9 | (context "position update" 10 | (it "holds position when there's no velocity" 11 | (let [turtle (-> @turtle (t/velocity 0.0) (t/state :idle)) 12 | new-turtle (t/update-turtle turtle)] 13 | (should= turtle new-turtle))) 14 | 15 | (it "steps by velocity when distance is far" 16 | (let [turtle (-> @turtle 17 | (t/heading 0.0) 18 | (t/velocity 5.0) 19 | (t/distance 100.0) 20 | (t/state :busy) 21 | ) 22 | {:keys [position state velocity distance]} (t/update-turtle turtle)] 23 | (should= [6.0 1.0] position) 24 | (should= 5.0 velocity) 25 | (should= 95.0 distance) 26 | (should= :busy state))) 27 | 28 | (it "steps back by velocity when distance is far" 29 | (let [turtle (-> @turtle 30 | (t/heading 0.0) 31 | (t/velocity -5.0) 32 | (t/distance 100.0) 33 | (t/state :busy) 34 | ) 35 | {:keys [position state velocity distance]} (t/update-turtle turtle)] 36 | (should= [-4.0 1.0] position) 37 | (should= -5.0 velocity) 38 | (should= 95.0 distance) 39 | (should= :busy state))) 40 | 41 | (it "steps by distance and goes idle when distance is near" 42 | (let [turtle (-> @turtle 43 | (t/heading 0.0) 44 | (t/velocity 5.0) 45 | (t/distance 3.0) 46 | (t/state :busy) 47 | ) 48 | {:keys [position state velocity distance]} (t/update-turtle turtle)] 49 | (should= [4.0 1.0] position) 50 | (should= 0.0 velocity) 51 | (should= 0.0 distance) 52 | (should= :idle state)))) 53 | 54 | (it "steps back by distance and goes idle when distance is near" 55 | (let [turtle (-> @turtle 56 | (t/heading 0.0) 57 | (t/velocity -5.0) 58 | (t/distance 3.0) 59 | (t/state :busy) 60 | ) 61 | {:keys [position state velocity distance]} (t/update-turtle turtle)] 62 | (should= [-2.0 1.0] position) 63 | (should= 0.0 velocity) 64 | (should= 0.0 distance) 65 | (should= :idle state))) 66 | 67 | (context "angle update" 68 | (it "holds angle when there's no omega" 69 | (let [turtle (-> @turtle (t/omega 0) (t/heading 90) (t/angle 30) (t/state :idle)) 70 | new-turtle (t/update-turtle turtle)] 71 | (should= turtle new-turtle))) 72 | 73 | (it "steps by omega when angle is far" 74 | (let [turtle (-> @turtle 75 | (t/omega 5.0) 76 | (t/angle 100.0) 77 | (t/state :busy) 78 | ) 79 | {:keys [heading state omega angle]} (t/update-turtle turtle)] 80 | (should= 6.0 heading) 81 | (should= 5.0 omega) 82 | (should= 95.0 angle) 83 | (should= :busy state))) 84 | 85 | (it "steps back by omega when angle is far" 86 | (let [turtle (-> @turtle 87 | (t/omega -5.0) 88 | (t/angle 100.0) 89 | (t/state :busy) 90 | ) 91 | {:keys [heading state omega angle]} (t/update-turtle turtle)] 92 | (should= 356.0 heading) 93 | (should= -5.0 omega) 94 | (should= 95.0 angle) 95 | (should= :busy state))) 96 | 97 | (it "steps by omega and goes idle when angle is near" 98 | (let [turtle (-> @turtle 99 | (t/omega 5.0) 100 | (t/angle 3.0) 101 | (t/state :busy) 102 | ) 103 | {:keys [heading state omega angle]} (t/update-turtle turtle)] 104 | (should= 4.0 heading) 105 | (should= 0.0 omega) 106 | (should= 0.0 angle) 107 | (should= :idle state))) 108 | 109 | (it "steps back by omega and goes idle when angle is near" 110 | (let [turtle (-> @turtle 111 | (t/omega -5.0) 112 | (t/angle 3.0) 113 | (t/state :busy) 114 | ) 115 | {:keys [heading state omega angle]} (t/update-turtle turtle)] 116 | (should= 358.0 heading) 117 | (should= 0.0 omega) 118 | (should= 0.0 angle) 119 | (should= :idle state))) 120 | ) 121 | 122 | (context "pen up and down" 123 | (it "marks the starting coordinate upon pen down" 124 | (let [turtle (t/pen-down @turtle) 125 | pen (:pen turtle) 126 | pen-start (:pen-start turtle)] 127 | (should= :down pen) 128 | (should= [1.0 1.0] pen-start)) 129 | ) 130 | 131 | (it "does not mark starting position if pen already down" 132 | (let [turtle (-> @turtle (t/pen-down) (t/position [2.0 2.0]) (t/pen-down)) 133 | pen (:pen turtle) 134 | pen-start (:pen-start turtle)] 135 | (should= :down pen) 136 | (should= [1.0 1.0] pen-start))) 137 | 138 | (it "adds line when pen goes back up" 139 | (let [turtle (-> @turtle 140 | (t/weight [3]) 141 | (t/pen-down) 142 | (t/position [2.0 2.0]) 143 | (t/pen-up)) 144 | pen (:pen turtle) 145 | pen-start (:pen-start turtle) 146 | lines (:lines turtle)] 147 | (should= :up pen) 148 | (should-be-nil pen-start) 149 | (should= [{:line-start [1.0 1.0] 150 | :line-end [2.0 2.0] 151 | :line-weight 3}] lines))) 152 | 153 | (it "does not add line when pen is already up" 154 | (let [turtle (-> @turtle (t/position [2.0 2.0]) (t/pen-up)) 155 | pen (:pen turtle) 156 | pen-start (:pen-start turtle) 157 | lines (:lines turtle)] 158 | (should= :up pen) 159 | (should-be-nil pen-start) 160 | (should= [] lines)) 161 | ) 162 | 163 | (it "adds line upon idle after move" 164 | (let [turtle (-> 165 | @turtle 166 | (t/heading 0) 167 | (t/pen-down) 168 | (t/weight [3]) 169 | (t/forward [1]) 170 | (t/update-turtle)) 171 | pen (:pen turtle) 172 | state (:state turtle) 173 | lines (:lines turtle) 174 | pen-start (:pen-start turtle) 175 | position (:position turtle)] 176 | (should= :down pen) 177 | (should= :idle state) 178 | (should= position pen-start) 179 | (should= [{:line-start [1.0 1.0] 180 | :line-end [2.0 1.0] 181 | :line-weight 3}] lines))) 182 | ) 183 | ) 184 | -------------------------------------------------------------------------------- /e2_Fibonacci/Turtle/src/turtle_graphics/core.clj: -------------------------------------------------------------------------------- 1 | (ns turtle-graphics.core 2 | (:require [quil.core :as q] 3 | [quil.middleware :as m] 4 | [turtle-graphics.turtle :as turtle] 5 | [clojure.core.async :as async] 6 | [clojure.tools.namespace.repl :refer [refresh]])) 7 | 8 | (def channel (async/chan)) 9 | 10 | (defn fibs 11 | ([a b] 12 | (lazy-seq 13 | (cons a (fibs b (+ a b))))) 14 | ([] (fibs 1 1)) 15 | ) 16 | 17 | (def PHI (/ (+ 1 (Math/sqrt 5)) 2)) 18 | 19 | (defn forward [distance] (async/>!! channel [:forward distance])) 20 | (defn back [distance] (async/>!! channel [:back distance])) 21 | (defn right [angle] (async/>!! channel [:right angle])) 22 | (defn left [angle] (async/>!! channel [:left angle])) 23 | (defn pen-up [] (async/>!! channel [:pen-up])) 24 | (defn pen-down [] (async/>!! channel [:pen-down])) 25 | (defn hide [] (async/>!! channel [:hide])) 26 | (defn show [] (async/>!! channel [:show])) 27 | (defn weight [weight] (async/>!! channel [:weight weight])) 28 | (defn speed [speed] (async/>!! channel [:speed speed])) 29 | 30 | (defn dot [] 31 | (weight 5) 32 | (pen-down) 33 | (forward 1) 34 | (pen-up) 35 | (back 1)) 36 | 37 | (defn fib-spiral [] 38 | (speed 1000) 39 | (weight 3) 40 | (doseq [f (take 20 (fibs))] 41 | (weight (inc (Math/log (double f)))) 42 | (forward f) 43 | (right 90) 44 | (dot)) 45 | ) 46 | 47 | (defn rect [a b] 48 | (doseq [_ (range 2)] 49 | (forward a) 50 | (right 90) 51 | (forward b) 52 | (right 90))) 53 | 54 | (defn phi-rect [n] 55 | (pen-down) 56 | (weight 3) 57 | (rect n (* n PHI))) 58 | 59 | (defn scaled-rect [a b scale] 60 | (let [width (* a scale) 61 | height (* b scale)] 62 | (rect width height) 63 | (forward width) 64 | (right 90)) 65 | ) 66 | 67 | (defn phi-spiral [] 68 | (left 90) 69 | (forward 100) 70 | (right 90) 71 | (pen-down) 72 | (weight 2) 73 | (speed 20) 74 | (loop [a 1 b PHI] 75 | (scaled-rect a b 10) 76 | (if (> a 40) 77 | nil 78 | (recur b (* b PHI)) 79 | ) 80 | ) 81 | ) 82 | 83 | (defn square-the-rect [length height f] 84 | (if (zero? height) 85 | (println "Rational.") 86 | (if (> 1 length) 87 | (println "Irrational") 88 | (let [n (quot length height) 89 | r (rem length height)] 90 | (println "Squares: " n " " (/ height f)) 91 | (doseq [_ (range n)] 92 | (forward height) 93 | (right 90) 94 | (pen-down) 95 | (forward height) 96 | (pen-up) 97 | (back height) 98 | (left 90)) 99 | (forward r) 100 | (right 90) 101 | (square-the-rect height r f))))) 102 | 103 | (defn square-spiral [a b] 104 | (let [length (max a b) 105 | height (min a b) 106 | f (quot 980 length) 107 | length (* length f) 108 | height (* height f) 109 | _ (prn length height)] 110 | (speed 20) 111 | (pen-up) 112 | (back (quot length 2)) 113 | (left 90) 114 | (forward (quot height 2)) 115 | (right 90) 116 | (pen-down) 117 | (weight 2) 118 | (rect length height) 119 | (pen-up) 120 | (square-the-rect length height f))) 121 | 122 | (defn flower-spiral [theta] 123 | (let [petals 250 124 | radius-increment 2] 125 | (speed 1000) 126 | (doseq [x (range petals)] 127 | (right theta) 128 | (forward (* radius-increment x)) 129 | (dot) 130 | (back (* radius-increment x))) 131 | (hide))) 132 | 133 | (defn polygon [theta, l, n] 134 | (pen-down) 135 | (speed 1000) 136 | (doseq [_ (range n)] 137 | (forward l) 138 | (right theta))) 139 | 140 | (defn spiral [theta length-f n] 141 | (pen-down) 142 | (speed 1000) 143 | (loop [i 0 len 1] 144 | (if (= i n) 145 | nil 146 | (do 147 | (forward len) 148 | (right theta) 149 | (recur (inc i) (length-f len)))))) 150 | 151 | (defn turtle-script [] 152 | (square-spiral 27 21) 153 | ) 154 | 155 | 156 | (defn setup [] 157 | (q/frame-rate 60) 158 | (q/color-mode :rgb) 159 | (let [state {:turtle (turtle/make) 160 | :channel channel}] 161 | (async/go 162 | (turtle-script) 163 | (prn "Turtle script complete")) 164 | state) 165 | ) 166 | 167 | (defn handle-commands [channel turtle] 168 | (loop [turtle turtle] 169 | (let [command (if (= :idle (:state turtle)) 170 | (async/poll! channel) 171 | nil)] 172 | (if (nil? command) 173 | turtle 174 | (recur (turtle/handle-command turtle command)))))) 175 | 176 | (defn update-state [{:keys [channel] :as state}] 177 | (let [turtle (:turtle state) 178 | turtle (turtle/update-turtle turtle)] 179 | (assoc state :turtle (handle-commands channel turtle))) 180 | ) 181 | 182 | (defn draw-state [state] 183 | (q/background 240) 184 | (q/with-translation 185 | [500 500] 186 | (let [{:keys [turtle]} state] 187 | (turtle/draw turtle))) 188 | ) 189 | 190 | (declare turtle-graphics) 191 | 192 | (defn ^:export -main [& args] 193 | (q/defsketch turtle-graphics 194 | :title "Turtle Graphics" 195 | :size [1000 1000] 196 | :setup setup 197 | :update update-state 198 | :draw draw-state 199 | :features [:keep-on-top] 200 | :middleware [m/fun-mode]) 201 | args) 202 | -------------------------------------------------------------------------------- /e2_Fibonacci/Turtle/src/turtle_graphics/turtle.clj: -------------------------------------------------------------------------------- 1 | (ns turtle-graphics.turtle 2 | (:require [quil.core :as q] 3 | [clojure.spec.alpha :as s])) 4 | 5 | (s/check-asserts true) 6 | (s/def ::position (s/tuple number? number?)) 7 | (s/def ::heading (s/and number? #(<= 0 % 360))) 8 | (s/def ::velocity number?) 9 | (s/def ::distance number?) 10 | (s/def ::omega number?) 11 | (s/def ::angle number?) 12 | (s/def ::weight (s/and pos? number?)) 13 | (s/def ::state #{:idle :busy}) 14 | (s/def ::pen #{:up :down}) 15 | (s/def ::pen-start (s/or :nil nil? 16 | :pos (s/tuple number? number?))) 17 | (s/def ::line-start (s/tuple number? number?)) 18 | (s/def ::line-end (s/tuple number? number?)) 19 | (s/def ::line (s/keys :req-un [::line-start ::line-end])) 20 | (s/def ::lines (s/coll-of ::line)) 21 | (s/def ::visible boolean?) 22 | (s/def ::speed (s/and int? pos?)) 23 | (s/def ::turtle (s/keys :req-un [::position 24 | ::heading 25 | ::velocity 26 | ::distance 27 | ::omega 28 | ::angle 29 | ::pen 30 | ::weight 31 | ::speed 32 | ::lines 33 | ::visible 34 | ::state] 35 | :opt-un [::pen-start])) 36 | 37 | (defn make [] 38 | {:post [(s/assert ::turtle %)]} 39 | {:position [0.0 0.0] 40 | :heading 0.0 41 | :velocity 0.0 42 | :distance 0.0 43 | :omega 0.0 44 | :angle 0.0 45 | :pen :up 46 | :weight 1 47 | :speed 5 48 | :visible true 49 | :lines [] 50 | :state :idle}) 51 | 52 | (def WIDTH 10) 53 | (def HEIGHT 15) 54 | 55 | (defn draw [turtle] 56 | (when (= :down (:pen turtle)) 57 | (q/stroke 0) 58 | (q/stroke-weight (:weight turtle)) 59 | (q/line (:pen-start turtle) (:position turtle))) 60 | 61 | (doseq [line (:lines turtle)] 62 | (q/stroke-weight (:line-weight line)) 63 | (q/line (:line-start line) (:line-end line))) 64 | 65 | (when (:visible turtle) 66 | (q/stroke-weight 1) 67 | (let [[x y] (:position turtle) 68 | heading (q/radians (:heading turtle)) 69 | base-left (- (/ WIDTH 2)) 70 | base-right (/ WIDTH 2) 71 | tip HEIGHT] 72 | (q/stroke 0) 73 | (q/with-translation 74 | [x y] 75 | (q/with-rotation 76 | [heading] 77 | (q/line 0 base-left 0 base-right) 78 | (q/line 0 base-left tip 0) 79 | (q/line 0 base-right tip 0)))))) 80 | 81 | (defn heading [turtle heading] 82 | (assoc turtle :heading heading)) 83 | 84 | (defn velocity [turtle velocity] 85 | (assoc turtle :velocity velocity)) 86 | 87 | (defn position [turtle position] 88 | (assoc turtle :position position)) 89 | 90 | (defn distance [turtle distance] 91 | (assoc turtle :distance distance)) 92 | 93 | (defn omega [turtle omega] 94 | (assoc turtle :omega omega)) 95 | 96 | (defn angle [turtle angle] 97 | (assoc turtle :angle angle)) 98 | 99 | (defn state [turtle state] 100 | (assoc turtle :state state)) 101 | 102 | (defn update-position [{:keys [position velocity heading distance] :as turtle}] 103 | (let [step (min (q/abs velocity) distance) 104 | distance (- distance step) 105 | step (if (neg? velocity) (- step) step) 106 | radians (q/radians heading) 107 | [x y] position 108 | vx (* step (Math/cos radians)) 109 | vy (* step (Math/sin radians)) 110 | position [(+ x vx) (+ y vy)] 111 | ] 112 | (assoc turtle :position position 113 | :distance distance 114 | :velocity (if (zero? distance) 0.0 velocity)))) 115 | 116 | (defn update-heading [{:keys [heading omega angle] :as turtle}] 117 | (let [angle-step (min (q/abs omega) angle) 118 | angle (- angle angle-step) 119 | angle-step (if (neg? omega) (- angle-step) angle-step) 120 | heading (mod (+ heading angle-step) 360)] 121 | (assoc turtle :heading heading 122 | :angle angle 123 | :omega (if (zero? angle) 0.0 omega)))) 124 | 125 | (defn make-line [{:keys [pen-start position weight]}] 126 | {:line-start pen-start 127 | :line-end position 128 | :line-weight weight}) 129 | 130 | (defn update-turtle [turtle] 131 | {:post [(s/assert ::turtle %)]} 132 | (if (= :idle (:state turtle)) 133 | turtle 134 | (let [{:keys [distance 135 | state 136 | angle 137 | lines 138 | position 139 | pen 140 | pen-start] :as turtle} 141 | (-> turtle 142 | (update-position) 143 | (update-heading)) 144 | done? (and (zero? distance) 145 | (zero? angle)) 146 | state (if done? :idle state) 147 | lines (if (and done? (= pen :down)) 148 | (conj lines (make-line turtle)) 149 | lines) 150 | pen-start (if (and done? (= pen :down)) 151 | position 152 | pen-start)] 153 | (assoc turtle :state state :lines lines :pen-start pen-start))) 154 | ) 155 | 156 | 157 | (defn pen-down [{:keys [pen position pen-start] :as turtle}] 158 | (assoc turtle :pen :down 159 | :pen-start (if (= :up pen) position pen-start))) 160 | 161 | (defn pen-up [{:keys [pen lines] :as turtle}] 162 | (if (= :up pen) 163 | turtle 164 | (let [new-line (make-line turtle) 165 | lines (conj lines new-line)] 166 | (assoc turtle :pen :up 167 | :pen-start nil 168 | :lines lines)))) 169 | 170 | (defn forward [turtle [distance]] 171 | (assoc turtle :velocity (:speed turtle) 172 | :distance distance 173 | :state :busy)) 174 | 175 | (defn back [turtle [distance]] 176 | (assoc turtle :velocity (- (:speed turtle)) 177 | :distance distance 178 | :state :busy)) 179 | 180 | (defn right [turtle [angle]] 181 | (assoc turtle :omega (* 2 (:speed turtle)) 182 | :angle angle 183 | :state :busy)) 184 | 185 | (defn left [turtle [angle]] 186 | (assoc turtle :omega (* -2 (:speed turtle)) 187 | :angle angle 188 | :state :busy)) 189 | 190 | (defn hide [turtle] 191 | (assoc turtle :visible false)) 192 | 193 | (defn show [turtle] 194 | (assoc turtle :visible true)) 195 | 196 | (defn weight [turtle [weight]] 197 | (assoc turtle :weight weight)) 198 | 199 | (defn speed [turtle [speed]] 200 | (assoc turtle :speed speed)) 201 | 202 | (defn handle-command [turtle [cmd & args]] 203 | (condp = cmd 204 | :forward (forward turtle args) 205 | :back (back turtle args) 206 | :right (right turtle args) 207 | :left (left turtle args) 208 | :pen-down (pen-down turtle) 209 | :pen-up (pen-up turtle) 210 | :hide (hide turtle) 211 | :show (show turtle) 212 | :weight (weight turtle args) 213 | :speed (speed turtle args) 214 | :else turtle) 215 | ) 216 | 217 | 218 | 219 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | ## [Unreleased] 5 | ### Changed 6 | - Add a new arity to `make-widget-async` to provide a different widget shape. 7 | 8 | ## [0.1.1] - 2021-05-03 9 | ### Changed 10 | - Documentation on how to make the widgets. 11 | 12 | ### Removed 13 | - `make-widget-sync` - we're all async, all the time. 14 | 15 | ### Fixed 16 | - Fixed widget maker to keep working when daylight savings switches over. 17 | 18 | ## 0.1.0 - 2021-05-03 19 | ### Added 20 | - Files from the new template. 21 | - Widget maker public API - `make-widget-sync`. 22 | 23 | [Unreleased]: https://github.com/your-name/e2_problem/compare/0.1.1...HEAD 24 | [0.1.1]: https://github.com/your-name/e2_problem/compare/0.1.0...0.1.1 25 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor to control, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/README.md: -------------------------------------------------------------------------------- 1 | # e2_problem 2 | 3 | A Clojure library designed to ... well, that part is up to you. 4 | 5 | ## Usage 6 | 7 | FIXME 8 | 9 | ## License 10 | 11 | Copyright © 2021 FIXME 12 | 13 | Distributed under the Eclipse Public License either version 1.0 or (at 14 | your option) any later version. 15 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to e2_problem 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/e2_problem.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/project.clj: -------------------------------------------------------------------------------- 1 | (defproject e2_problem "0.1.0-SNAPSHOT" 2 | :description "Euler 2 Solver" 3 | :main e2-problem.core 4 | :dependencies [[org.clojure/clojure "1.8.0"] 5 | [org.clojure/tools.namespace "1.1.0"]]) 6 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/src/e2_problem/core.clj: -------------------------------------------------------------------------------- 1 | (ns e2-problem.core 2 | (:require [clojure.tools.namespace.repl :refer [refresh]])) 3 | 4 | (defn fibs 5 | ([a b] 6 | (lazy-seq 7 | (cons a (fibs b (+ a b))))) 8 | ([] (fibs 1N 1N)) 9 | ) 10 | 11 | (defn sum-even-fibs-upto [n] 12 | (loop [fs (fibs) sum 0] 13 | (let [fib (first fs)] 14 | (if (> fib n) 15 | sum 16 | (recur (rest fs) 17 | (if (even? fib) 18 | (+ sum fib) 19 | sum)))))) 20 | 21 | (defn ratio [n] 22 | (let [fs (fibs) 23 | numerator (nth fs (inc n)) 24 | denominator (nth fs n)] 25 | (double (/ numerator denominator)))) 26 | 27 | -------------------------------------------------------------------------------- /e2_Fibonacci/e2_problem/test/e2_problem/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns e2-problem.core-test 2 | (:require [clojure.test :refer :all] 3 | [e2-problem.core :refer :all])) 4 | 5 | (deftest a-test 6 | (testing "FIXME, I fail." 7 | (is (= 0 1)))) 8 | -------------------------------------------------------------------------------- /e3_primeFactors/prime_factors/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /lib 3 | /classes 4 | /checkouts 5 | pom.xml 6 | *.jar 7 | *.class 8 | .lein-deps-sum 9 | .lein-failures 10 | .lein-plugins 11 | .lein-repl-history 12 | -------------------------------------------------------------------------------- /e3_primeFactors/prime_factors/README.md: -------------------------------------------------------------------------------- 1 | # prime_factors 2 | -------------------------------------------------------------------------------- /e3_primeFactors/prime_factors/project.clj: -------------------------------------------------------------------------------- 1 | (defproject prime_factors "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :main prime-factors.core 7 | :dependencies [[org.clojure/clojure "1.10.1"] 8 | [org.clojure/data.int-map "1.0.0"] 9 | [com.stuartsierra/frequencies "0.1.0"]] 10 | :profiles {:dev {:dependencies [[speclj "3.3.2"]]}} 11 | :plugins [[speclj "3.3.2"]] 12 | :test-paths ["spec"] 13 | :java-source-paths ["../sieve/src"]) 14 | -------------------------------------------------------------------------------- /e3_primeFactors/prime_factors/spec/prime_factors/core_spec.clj: -------------------------------------------------------------------------------- 1 | (ns prime-factors.core-spec 2 | (:require [speclj.core :refer :all] 3 | [prime-factors.core :refer :all])) 4 | 5 | (defn power2 [n] 6 | (apply * (repeat n 2N))) 7 | 8 | (describe "factor primes" 9 | (it "factors 1 -> []" 10 | (should= [] (factors-of 1))) 11 | (it "factors 2 -> [2]" 12 | (should= [2] (factors-of 2))) 13 | (it "factors 3 -> [3]" 14 | (should= [3] (factors-of 3))) 15 | (it "factors 4 -> [2 2]" 16 | (should= [2 2] (factors-of 4))) 17 | (it "factors 5 -> [5]" 18 | (should= [5] (factors-of 5))) 19 | (it "factors 6 -> [2 3]" 20 | (should= [2 3] (factors-of 6))) 21 | (it "factors 7 -> [7]" 22 | (should= [7] (factors-of 7))) 23 | (it "factors 8 -> [2 2 2]" 24 | (should= [2 2 2] (factors-of 8))) 25 | (it "factors 9 -> [3 3]" 26 | (should= [3 3] (factors-of 9))) 27 | (it "factors lots" 28 | (should= [2 2 3 3 5 7 11 11 13] 29 | (factors-of (* 2 2 3 3 5 7 11 11 13)))) 30 | (it "factors Euler 3" 31 | (should= [71 839 1471 6857] (factors-of 600851475143))) 32 | 33 | (it "factors mersenne 2^31-1" 34 | (should= [2147483647] (factors-of (dec (power2 31))))) 35 | 36 | ;(it "factors mersenne 2^61-1" 37 | ; (let [m61 (dec (power2 61))] 38 | ; (should= [m61] (factors-of m61)))) 39 | ; 40 | ;(it "factors m31*m31" 41 | ; (let [m31 (dec (power2 31))] 42 | ; (should= [m31 m31] (factors-of (* m31 m31))))) 43 | ) 44 | 45 | (describe "get primes up to" 46 | (it "1 -> []]" 47 | (should= [] (get-primes-up-to 1))) 48 | (it "2 -> [2]" 49 | (should= [2] (get-primes-up-to 2))) 50 | (it "3 -> [2 3]" 51 | (should= [2 3] (get-primes-up-to 3))) 52 | (it "4 -> [2 3]" 53 | (should= [2 3] (get-primes-up-to 4))) 54 | (it "5 -> [2 3 5]" 55 | (should= [2 3 5] (get-primes-up-to 5))) 56 | (it "30 -> [2 3 5 7 11 13 17 19 23 29]" 57 | (should= [2 3 5 7 11 13 17 19 23 29] (get-primes-up-to 30))) 58 | (it "168 primes below 1000" 59 | (should= 168 (count (get-primes-up-to 1000)))) 60 | (it "78498 primes below 1000000" 61 | (should= 78498 (count (fast-primes-up-to 1000000)))) 62 | ) 63 | 64 | (describe "find twin primes" 65 | (it "[] -> []" 66 | (should= [] (find-twins []))) 67 | (it "[1] -> []" 68 | (should= [] (find-twins [1]))) 69 | (it "[1 1] -> []" 70 | (should= [] (find-twins [1 1]))) 71 | (it "[1 2] -> []" 72 | (should= [] (find-twins [1 2]))) 73 | (it "[1 3] -> [1]" 74 | (should= [1] (find-twins [1 3]))) 75 | (it "[1 3 4 7 9] -> [1 7]" 76 | (should= [1 7] (find-twins [1 3 4 7 9]))) 77 | (it "should find twin primes up to 100" 78 | (should= [3 5 11 17 29 41 59 71] (find-twins (fast-primes-up-to 100)))) 79 | (it "should find twin primes up to 1000" 80 | (should-not (empty? (find-twins (fast-primes-up-to 1000))))) 81 | (it "should find twin primes up to 10000" 82 | (should-not (empty? (find-twins (fast-primes-up-to 10000))))) 83 | (it "should find twin primes up to 100000" 84 | (should-not (empty? (find-twins (fast-primes-up-to 100000))))) 85 | (it "should find twin primes up to 1,000,000" 86 | (should-not (empty? (find-twins (fast-primes-up-to 1000000))))) 87 | (it "should find twin primes up to 10,000,000" 88 | (should-not (empty? (find-twins (fast-primes-up-to 10000000))))) 89 | ) 90 | -------------------------------------------------------------------------------- /e3_primeFactors/prime_factors/src/prime_factors/core.clj: -------------------------------------------------------------------------------- 1 | (ns prime-factors.core 2 | (:import (sieve Sieve))) 3 | 4 | (defn factors-of [n] 5 | (loop [factors [] n n divisor 2] 6 | (if (> n 1) 7 | (cond 8 | (> divisor (Math/sqrt n)) 9 | (conj factors n) 10 | (= 0 (mod n divisor)) 11 | (recur (conj factors divisor) 12 | (quot n divisor) 13 | divisor) 14 | :else 15 | (recur factors n (inc divisor))) 16 | factors))) 17 | 18 | (defn mark-multiples [composites prime n] 19 | (loop [i (* 2 prime)] 20 | (when (<= i n) 21 | (aset composites i true) 22 | (recur (+ i prime)))) 23 | ) 24 | 25 | (defn filter-primes [composites n] 26 | (loop [primes [] i 2] 27 | (if (> i n) 28 | primes 29 | (if (aget composites i) 30 | (recur primes (inc i)) 31 | (recur (conj primes i) (inc i)))))) 32 | 33 | (defn sieve [n] 34 | (let [composites (make-array Boolean/TYPE (inc n)) 35 | limit (int (Math/sqrt n))] 36 | (loop [candidate 2] 37 | (if (> candidate n) 38 | (filter-primes composites n) 39 | (if (aget composites candidate) 40 | (recur (inc candidate)) 41 | (do (when (<= candidate limit) 42 | (mark-multiples composites candidate n)) 43 | (recur (inc candidate)))))))) 44 | 45 | (defn get-primes-up-to [n] 46 | (if (> n 1) 47 | (sieve n) 48 | [])) 49 | 50 | (defn fast-primes-up-to [n] 51 | (vec (Sieve/primesUpTo n))) 52 | 53 | (defn find-twins [ns] 54 | (loop [twins [] ns ns] 55 | (if (< (count ns) 2) 56 | twins 57 | (let [diff (- (second ns) (first ns))] 58 | (if (= 2 diff) 59 | (recur (conj twins (first ns)) (rest ns)) 60 | (recur twins (rest ns))))))) 61 | 62 | (defn print-gap-frequency-graph [gap-frequencies] 63 | (let [gaps (keys gap-frequencies) 64 | max-gap (apply max gaps) 65 | log-freqs (into {} (map #(vector (first %) (Math/log (second %))) gap-frequencies)) 66 | max-log-freq (apply max (vals log-freqs)) 67 | magnification (/ 90 max-log-freq) 68 | gap-range (range 2 max-gap 2)] 69 | (doseq [gap gap-range] 70 | (printf "%5d: %8d %s\n" 71 | gap 72 | (get gap-frequencies gap 0) 73 | (apply str (repeat (* magnification (get log-freqs gap 0)) "*")))))) 74 | 75 | (defn get-last-digit-frequencies [ns modulus] 76 | (let [last-digits (map #(mod % modulus) ns) 77 | last-digit-frequencies (frequencies last-digits)] 78 | last-digit-frequencies)) 79 | 80 | (defn print-graph [data-map] 81 | (let [vs (vals data-map) 82 | max-val (apply max vs) 83 | magnification (/ 90 max-val) 84 | categories (sort (keys data-map))] 85 | (doseq [x categories] 86 | (printf "%5s %5d: %s\n" 87 | x 88 | (get data-map x) 89 | (apply str (repeat (* magnification (get data-map x)) "*")))) 90 | ) 91 | ) 92 | 93 | (defn print-last-digit-frequencies [ns modulus] 94 | (let [last-digits (get-last-digit-frequencies ns modulus) 95 | freqs (remove #(< % 2) (vals last-digits)) 96 | min-freq (apply min freqs) 97 | max-freq (apply max freqs) 98 | mean-freq (/ (reduce + freqs) (double (count freqs)))] 99 | (printf "min: %d, max: %d, mean: %.2f\n" min-freq max-freq mean-freq) 100 | (print-graph last-digits))) 101 | 102 | (defn print-consecutive-last-digit-frequencies [ns] 103 | (let [first (map #(mod % 10) ns) 104 | second (rest first) 105 | consecutives (map #(str %1 "-" %2) first second) 106 | consecutive-frequencies (frequencies consecutives)] 107 | (print-graph consecutive-frequencies)) 108 | ) 109 | 110 | 111 | (defn twin-density [n] 112 | (let [primes (fast-primes-up-to n) 113 | gaps (map #(- %1 %2) (rest primes) primes) 114 | mean-gap (/ (reduce + gaps) (double (count gaps))) 115 | gap-frequencies (frequencies gaps) 116 | nprimes (count primes) 117 | twins (find-twins primes) 118 | twin-gaps (map #(- %1 %2) (rest twins) twins) 119 | longest-twin-gap (apply max twin-gaps) 120 | ntwins (count twins) 121 | prime-density (double (/ nprimes n)) 122 | twin-density (double (/ ntwins n)) 123 | twin-prevalence (double (/ ntwins nprimes))] 124 | (printf "primes: %d, twins %d\n" nprimes ntwins) 125 | (printf "prime-density: %.4f\n" prime-density) 126 | (printf "twin-density: %.4f\n" twin-density) 127 | (printf "twin-prevalence: %.4f\n" twin-prevalence) 128 | (printf "longest twin gap: %d\n" longest-twin-gap) 129 | (printf "longest gap: %d\n" (apply max gaps)) 130 | (printf "mean gap: %.4f\n" mean-gap) 131 | (print-gap-frequency-graph gap-frequencies) 132 | (printf "\nLast digit frequencies:") 133 | )) 134 | 135 | ;https://github.com/unclebob/Euler/tree/main/e3_primeFactors -------------------------------------------------------------------------------- /e3_primeFactors/sieve/src/sieve/Sieve.java: -------------------------------------------------------------------------------- 1 | package sieve; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Sieve { 7 | public static List primesUpTo(int n) { 8 | if (n < 2) 9 | return new ArrayList<>(); 10 | return sieve(n); 11 | } 12 | 13 | private static List sieve(int n) { 14 | List primes = new ArrayList<>(); 15 | boolean[] composites = new boolean[n + 1]; 16 | for (int candidate = 2; candidate <= n; candidate++) { 17 | if (!composites[candidate]) { 18 | primes.add(candidate); 19 | for (int multiple = 2 * candidate; multiple <= n; multiple += candidate) 20 | composites[multiple] = true; 21 | } 22 | } 23 | return primes; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /e3_primeFactors/sieve/test/sieve/SieveTest.java: -------------------------------------------------------------------------------- 1 | package sieve; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.collection.IsEmptyCollection.empty; 7 | import static org.hamcrest.collection.IsIterableContainingInOrder.contains; 8 | import static org.hamcrest.core.IsEqual.equalTo; 9 | 10 | public class SieveTest { 11 | @Test 12 | public void testSieve() throws Exception { 13 | assertThat(Sieve.primesUpTo(1), empty()); 14 | assertThat(Sieve.primesUpTo(2), contains(2)); 15 | assertThat(Sieve.primesUpTo(10), contains(2, 3, 5, 7)); 16 | assertThat(Sieve.primesUpTo(1000).size(), equalTo(168)); 17 | assertThat(Sieve.primesUpTo(1000000).size(), equalTo(78498)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /e4_palindromic_numbers/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /lib 3 | /classes 4 | /checkouts 5 | pom.xml 6 | *.jar 7 | *.class 8 | .lein-deps-sum 9 | .lein-failures 10 | .lein-plugins 11 | .lein-repl-history 12 | -------------------------------------------------------------------------------- /e4_palindromic_numbers/README.md: -------------------------------------------------------------------------------- 1 | # e4_palindromic_numbers 2 | -------------------------------------------------------------------------------- /e4_palindromic_numbers/project.clj: -------------------------------------------------------------------------------- 1 | (defproject e4_palindromic_numbers "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :main e4-palindromic-numbers.core 7 | :dependencies [[org.clojure/clojure "1.8.0"]] 8 | :profiles {:dev {:dependencies [[speclj "3.3.2"]]}} 9 | :plugins [[speclj "3.3.2"]] 10 | :test-paths ["spec"]) 11 | -------------------------------------------------------------------------------- /e4_palindromic_numbers/spec/e4_palindromic_numbers/core_spec.clj: -------------------------------------------------------------------------------- 1 | (ns e4-palindromic-numbers.core-spec 2 | (:require [speclj.core :refer :all] 3 | [e4-palindromic-numbers.core :refer :all])) 4 | 5 | (describe "Palindromic Numbers" 6 | (it "Will build palindromes from numbers" 7 | (should= 123321 (make-palindrome 123))) 8 | 9 | (it "Will make all 6 digit palindromes" 10 | (let [palindromes (make-palindromes)] 11 | (should= 900 (count palindromes)) 12 | (should= 999999 (first palindromes)) 13 | (should= 100001 (last palindromes)))) 14 | ) 15 | 16 | (describe "Factors of Palindromic numbers" 17 | (it "can make factor pairs" 18 | (should= [[1 19]] (factor-pairs 19)) 19 | (should= [[1 20] [2 10] [4 5]] (factor-pairs 20))) 20 | 21 | (it "can identify three digit pairs" 22 | (should (four-digit-pair? [100 999])) 23 | (should-not (four-digit-pair? [99 200])) 24 | (should-not (four-digit-pair? [200 1000]))) 25 | ) 26 | 27 | (describe "Greatest Palindromic number product of two three digit numbers" 28 | (it "will find that number" 29 | (should= [906609 [913 993]] (find-greatest-palindrome))) 30 | ) 31 | -------------------------------------------------------------------------------- /e4_palindromic_numbers/src/e4_palindromic_numbers/core.clj: -------------------------------------------------------------------------------- 1 | (ns e4-palindromic-numbers.core 2 | (:require [clojure.string :as str])) 3 | 4 | (defn make-palindrome [n] 5 | (let [prefix (str n) 6 | suffix (str/join (reverse prefix))] 7 | (Integer/parseInt (str/join [prefix suffix])))) 8 | 9 | (defn make-palindromes [] 10 | (for [n (reverse (range 1000 10000))] 11 | (make-palindrome n))) 12 | 13 | (defn factor-pairs [n] 14 | (let [limit (int (Math/sqrt n)) 15 | pairs (for [x (range 1 (inc limit))] 16 | (if (zero? (mod n x)) 17 | [x (quot n x)] 18 | nil))] 19 | (remove nil? pairs))) 20 | 21 | (defn four-digit-pair? [[a b]] 22 | (and (< 999 a 10000) 23 | (< 999 b 10000))) 24 | 25 | (defn find-greatest-palindrome [] 26 | (loop [palindromes (make-palindromes)] 27 | (if (empty? palindromes) 28 | nil 29 | (let [pairs (factor-pairs (first palindromes)) 30 | good-pairs (filter four-digit-pair? pairs)] 31 | (if (empty? good-pairs) 32 | (recur (rest palindromes)) 33 | [(first palindromes) (first good-pairs)]))) 34 | ) 35 | ) 36 | 37 | (defn fast-find-greatest-palindrome [] 38 | (loop [seed 9999 39 | palindrome 99999999 40 | factor 9999] 41 | (if (and 42 | (zero? (rem palindrome factor)) 43 | (> 9999 (quot palindrome factor))) 44 | palindrome 45 | (if (> 9999 (quot palindrome factor)) 46 | (recur seed palindrome (dec factor)) 47 | (recur (dec seed) (make-palindrome (dec seed)) 9999))) 48 | )) 49 | 50 | (defn factors-of [n] 51 | (loop [factors [] n n divisor 2] 52 | (if (> n 1) 53 | (cond 54 | (> divisor (Math/sqrt n)) 55 | (conj factors n) 56 | (= 0 (mod n divisor)) 57 | (recur (conj factors divisor) 58 | (quot n divisor) 59 | divisor) 60 | :else 61 | (recur factors n (inc divisor))) 62 | factors))) 63 | 64 | (defn factor-list [n] 65 | (loop [factor 999 factors []] 66 | (if (zero? factor) 67 | factors 68 | (if (zero? (mod n factor)) 69 | (recur (dec factor) (conj factors [factor (quot n factor)])) 70 | (recur (dec factor) factors) 71 | )))) -------------------------------------------------------------------------------- /e4_palindromic_numbers/src/euler4.pal: -------------------------------------------------------------------------------- 1 | /EULER 4 SOLUTION 2 | 3 | PZERO=20 4 | 5 | *200 6 | 7 | MAIN, CLA 8 | TLS 9 | TAD SEED 10 | ISZ SEED 11 | CIA 12 | JMS CALL 13 | MKPAL 14 | 15 | JMS CALL 16 | PRDOT 17 | 18 | CLA 19 | TAD MAXFAC 20 | DCA FAC 21 | 22 | FACLUP, 23 | CLA 24 | TAD FAC 25 | TAD K100 26 | SMA CLA 27 | JMP MAIN 28 | 29 | JMS CALL 30 | DLOAD 31 | DPAL 32 | TAD FAC 33 | CIA 34 | JMS CALL 35 | ISFAC 36 | SKP 37 | JMP GOTFAC 38 | 39 | CLA 40 | TAD I OFP /OTHER FAC > 999 TRY NEXT PAL. 41 | TAD MAXFAC 42 | SMA CLA 43 | JMP MAIN 44 | ISZ FAC 45 | JMP FACLUP 46 | 47 | GOTFAC, 48 | CLA 49 | TAD I OFP 50 | TAD MAXFAC 51 | SMA CLA 52 | JMP MAIN 53 | 54 | JMS CRLF 55 | CLA 56 | TAD FAC 57 | CIA 58 | JMS CALL 59 | PRAC 60 | JMS CALL 61 | PRDOT 62 | CLA 63 | TAD I OFP 64 | JMS CALL 65 | PRAC 66 | JMS CRLF 67 | JMS CALL 68 | DLOAD 69 | DPAL 70 | JMS CALL 71 | PRDACC 72 | JMS CRLF 73 | HLT 74 | 75 | DECIMAL 76 | SEED, -999 77 | MAXFAC, -999 78 | OCTAL 79 | FAC, 0 80 | OFP, OTHFAC+1 81 | 82 | *400 83 | 84 | /MAKE A PALINDROMIC NUMBER FROM A SEED. 85 | /ABC->ABCCBA IN DECIMAL IN DACC AND STORED IN DPAL 86 | 87 | MKPAL, 0 88 | DCA DPAL+1 89 | DCA DPAL 90 | TAD DPAL+1 91 | JMS CALL 92 | DIV 93 | K10 94 | DCA WRK 95 | TAD REM 96 | DCA DIGS 97 | TAD WRK 98 | JMS CALL 99 | DIV 100 | K10 101 | DCA DIGS+2 102 | TAD REM 103 | DCA DIGS+1 104 | JMS CALL 105 | DLOAD 106 | DPAL 107 | TAD K1000 108 | JMS CALL 109 | DMUL 110 | JMS CALL 111 | DSTORE 112 | DPAL 113 | CLA 114 | TAD DIGS 115 | JMS CALL 116 | MUL 117 | K10 118 | TAD DIGS+1 119 | JMS CALL 120 | MUL 121 | K10 122 | TAD DIGS+2 123 | DCA DWRK+1 124 | DCA DWRK 125 | JMS CALL 126 | DLOAD 127 | DPAL 128 | JMS CALL 129 | DADD 130 | DWRK 131 | 132 | JMS CALL 133 | DSTORE 134 | DPAL 135 | JMP I MKPAL 136 | 137 | /SKIP IF AC IS A FACTOR OF DACC. AC=0 138 | ISFAC, 0 139 | DCA DFAC+1 140 | DCA DFAC 141 | 142 | JMS CALL 143 | DDIV 144 | DFAC 145 | 146 | JMS CALL 147 | DSTORE 148 | OTHFAC 149 | 150 | JMS CALL 151 | DLOAD 152 | DREM 153 | JMS CALL 154 | DSKEQ 155 | D0 156 | SKP 157 | ISZ ISFAC 158 | JMP I ISFAC 159 | 160 | DFAC, 0 161 | 0 162 | OTHFAC, 0 163 | 0 164 | 165 | OCTAL 166 | DPAL, 0 167 | 0 168 | 169 | 170 | DIGS, 0 171 | 0 172 | 0 173 | 174 | WRK, 0 175 | 176 | DWRK, 0 177 | 0 178 | 179 | // PZERO FOR EULER 180 | *PZERO 181 | DECIMAL 182 | K100, 100 183 | K1000, 1000 184 | K10, 10 185 | OCTAL 186 | 187 | PZERO = . 188 | 189 | 190 | ~ 191 | 192 | *1000 193 | /DMATHLIB 194 | /DLOAD - LOAD ARG INTO DACC, AC=0 195 | DLOAD, 0 196 | CLA 197 | TAD I DLOAD 198 | ISZ DLOAD 199 | DCA DARGP 200 | TAD I DARGP 201 | DCA DACC 202 | ISZ DARGP 203 | TAD I DARGP 204 | DCA DACC+1 205 | JMP I DLOAD 206 | 207 | /DOUBLE PRECISION STORE ACCUMULATOR POINTED TO BY ARG 208 | DSTORE, 0 209 | CLA 210 | TAD I DSTORE 211 | DCA DARGP 212 | ISZ DSTORE 213 | 214 | TAD DACC 215 | DCA I DARGP 216 | ISZ DARGP 217 | TAD DACC+1 218 | DCA I DARGP 219 | JMP I DSTORE 220 | 221 | /SKIP IF DOUBLE PRECISION ARGUMENT IS EQUAL TO DACC. AC=0 222 | DSKEQ, 0 223 | CLA 224 | TAD I DSKEQ 225 | DCA DARGP 226 | ISZ DSKEQ 227 | 228 | TAD DACC 229 | CIA 230 | TAD I DARGP 231 | SZA CLA 232 | JMP I DSKEQ 233 | 234 | ISZ DARGP 235 | TAD DACC+1 236 | CIA 237 | TAD I DARGP 238 | SNA CLA 239 | ISZ DSKEQ 240 | JMP I DSKEQ 241 | 242 | /DOUBLE PRECISION ADD ARGUMENT TO DACC. AC=0 243 | 244 | DADD, 0 245 | CLA CLL 246 | TAD I DADD 247 | ISZ DADD 248 | DCA DARGP 249 | TAD DARGP 250 | IAC 251 | DCA DARGP2 252 | 253 | TAD I DARGP2 254 | TAD DACC+1 255 | DCA DACC+1 256 | RAL 257 | TAD I DARGP 258 | TAD DACC 259 | DCA DACC 260 | 261 | JMP I DADD 262 | 263 | /COMPLEMENT AND INCREMENT DACC 264 | DCIA, 0 265 | CLA CLL 266 | TAD DACC+1 267 | CMA IAC 268 | DCA DACC+1 269 | TAD DACC 270 | CMA 271 | SZL 272 | IAC 273 | DCA DACC 274 | JMP I DCIA 275 | 276 | /MULTIPY DACC BY AC 277 | DMUL, 0 278 | CIA 279 | DCA PLIERD 280 | JMS DSTORE 281 | DCAND 282 | JMS DLOAD 283 | D0 284 | TAD PLIERD 285 | SNA CLA 286 | JMP I DMUL 287 | DMUL1, JMS DADD 288 | DCAND 289 | ISZ PLIERD 290 | JMP DMUL1 291 | JMP I DMUL 292 | 293 | PLIERD, 0 294 | DCAND, 0 295 | 0 296 | 297 | /DIV DACC BY DARG (AWFUL) R IN DREM AC=0 298 | DDIV, 0 299 | CLA 300 | TAD I DDIV 301 | ISZ DDIV 302 | DCA .+4 303 | JMS DSTORE 304 | DVDEND 305 | JMS DLOAD 306 | 0 307 | JMS DCIA /NEGATE DIVISOR 308 | JMS DSTORE 309 | DVSOR 310 | JMS DLOAD 311 | DVDEND 312 | 313 | DCA DQUOT 314 | DCA DQUOT+1 315 | JMP DDIV1 316 | 317 | DDIV2, ISZ DQUOT+1 // INCREMENT DQUOT 318 | SKP 319 | ISZ DQUOT 320 | 321 | DDIV1, JMS DSTORE 322 | DREM 323 | JMS DADD 324 | DVSOR 325 | TAD DACC 326 | SMA CLA 327 | JMP DDIV2 328 | 329 | JMS DLOAD 330 | DQUOT 331 | JMP I DDIV 332 | 333 | 334 | DARGP, 0 335 | DARGP2, 0 336 | 337 | DVSOR, 0 338 | 0 339 | DVDEND, 0 340 | 0 341 | DQUOT, 0 342 | 0 343 | 344 | /PAGE ZERO DATA FOR DMATHLIB 345 | 346 | *PZERO 347 | DACC, 0 348 | 0 349 | D0, 0 350 | 0 351 | DREM, 0 352 | 0 353 | PZERO=. 354 | ~ 355 | 356 | /SINGLE PRECISION MATH LIBRARY 357 | *2000 358 | /DIVIDE AC BY ARGP (SLOW AND NAIVE) 359 | /Q IN AC, R IN REM 360 | DIV, 0 361 | DCA REM 362 | TAD I DIV 363 | ISZ DIV 364 | DCA ARGP 365 | TAD I ARGP 366 | CIA 367 | DCA MDVSOR 368 | DCA QUOTNT 369 | TAD REM 370 | DIVLUP, TAD MDVSOR 371 | SPA 372 | JMP DIVDUN 373 | ISZ QUOTNT 374 | JMP DIVLUP 375 | DIVDUN, CIA 376 | TAD MDVSOR 377 | CIA 378 | DCA REM 379 | TAD QUOTNT 380 | JMP I DIV 381 | MDVSOR, 0 382 | QUOTNT, 0 383 | ARGP, 0 384 | 385 | /MULTIPLY AC BY ARGP (SLOW AND NAIVE) 386 | /GIVING SINGLE PRECISION PRODUCT IN AC 387 | 388 | MUL, 0 389 | DCA CAND 390 | TAD I MUL 391 | ISZ MUL 392 | DCA ARGP 393 | TAD I ARGP 394 | SNA 395 | JMP I MUL 396 | CIA 397 | DCA PLIER 398 | TAD CAND 399 | ISZ PLIER 400 | JMP .-2 401 | JMP I MUL 402 | CAND, 0 403 | PLIER, 0 404 | 405 | /PZERO FOR SMATHLIB 406 | *PZERO 407 | REM, 0 408 | PZERO=. 409 | ~ 410 | 411 | /TTY UTILS 412 | *3000 413 | /PRINT ONE CHAR IN AC. IF CR THEN PRINT LF. 414 | PRTCHAR,0 415 | TSF 416 | JMP .-1 417 | TLS 418 | DCA CH 419 | TAD CH 420 | TAD MCR 421 | SZA 422 | JMP RETCHR 423 | TAD KLF 424 | TSF 425 | JMP .-1 426 | TLS 427 | RETCHR, CLA 428 | TAD CH 429 | JMP I PRTCHAR 430 | CH, 0 431 | MCR, -215 432 | 433 | /PRINT AC AS ONE DECIMAL DIGIT AC=0 434 | PRDIG, 0 435 | TAD K260 436 | TSF 437 | JMP .-1 438 | TLS 439 | CLA 440 | JMP I PRDIG 441 | 442 | K260, 260 443 | 444 | /PRINT THE DACC IN DECIMAL 445 | 446 | PRDACC, 0 447 | JMS CALL 448 | DSTORE 449 | DACSV 450 | JMS CALL 451 | DDIV 452 | D1E6 453 | TAD DACC+1 454 | JMS PRDIG 455 | JMS CALL 456 | DLOAD 457 | DREM 458 | JMS CALL 459 | DDIV 460 | D1E5 461 | TAD DACC+1 462 | JMS PRDIG 463 | JMS CALL 464 | DLOAD 465 | DREM 466 | JMS CALL 467 | DDIV 468 | D1E4 469 | TAD DACC+1 470 | JMS PRDIG 471 | JMS CALL 472 | DLOAD 473 | DREM 474 | JMS CALL 475 | DDIV 476 | D1E3 477 | TAD DACC+1 478 | JMS PRDIG 479 | JMS CALL 480 | DLOAD 481 | DREM 482 | JMS CALL 483 | DDIV 484 | D1E2 485 | TAD DACC+1 486 | JMS PRDIG 487 | JMS CALL 488 | DLOAD 489 | DREM 490 | JMS CALL 491 | DDIV 492 | D1E1 493 | TAD DACC+1 494 | JMS PRDIG 495 | JMS CALL 496 | DLOAD 497 | DREM 498 | TAD DACC+1 499 | JMS PRDIG 500 | JMS CALL 501 | DLOAD 502 | DACSV 503 | JMP I PRDACC 504 | 505 | 506 | DACSV, 0 507 | 0 508 | D1E6, 0364 509 | 1100 510 | D1E5, 0030 511 | 3240 512 | D1E4, 2 513 | 3420 514 | D1E3, 0 515 | 1750 516 | D1E2, 0 517 | 144 518 | D1E1, 0 519 | 12 520 | 521 | /PRINT AC, AC=AC 522 | PRAC, 0 523 | DCA SAC 524 | TAD SAC 525 | JMS CALL 526 | DIV 527 | D1E3+1 528 | JMS PRDIG 529 | TAD REM 530 | JMS CALL 531 | DIV 532 | D1E2+1 533 | JMS PRDIG 534 | TAD REM 535 | JMS CALL 536 | DIV 537 | D1E1+1 538 | JMS PRDIG 539 | TAD REM 540 | JMS PRDIG 541 | TAD SAC 542 | JMP I PRAC 543 | SAC, 0 544 | 545 | /PRINT DOT AC=AC 546 | PRDOT, 0 547 | DCA SAC 548 | TAD KDOT 549 | JMS TYPE 550 | TAD SAC 551 | JMP I PRDOT 552 | 553 | /---------------------- 554 | /PZERO TEST LIBRARY 555 | *PZERO 556 | TYPE, 0 / AC=0 557 | TSF 558 | JMP .-1 559 | TLS 560 | CLA 561 | JMP I TYPE 562 | 563 | CRLF, 0 / AC=0 564 | CLA 565 | TAD KCR 566 | JMS TYPE 567 | TAD KLF 568 | JMS TYPE 569 | JMP I CRLF 570 | 571 | /SOUND BELL AND HALT WITH ADDR OF BAD TEST IN AC 572 | ERROR, 0 573 | CLA 574 | TAD KBELL 575 | JMS TYPE 576 | CLA CMA 577 | TAD ERROR 578 | HLT 579 | 580 | /PRINT DOT, COUNT ERROR 581 | PASS, 0 582 | CLA 583 | TAD KDOT 584 | JMS TYPE 585 | ISZ TESTS 586 | JMP I PASS 587 | 588 | /TESTS COMPLETE, PRINT ZERO AND HALT WITH # OF TESTS IN AC. 589 | TSTDUN, 590 | JMS CRLF 591 | TAD KZERO 592 | JMS TYPE 593 | JMS CRLF 594 | TAD TESTS 595 | HLT 596 | 597 | /CALL SUBROUTINE 598 | CALL, 0 599 | DCA AC 600 | TAD I CALL 601 | DCA CALLEE 602 | TAD CALL 603 | IAC 604 | DCA I CALLEE 605 | ISZ CALLEE 606 | TAD AC 607 | JMP I CALLEE 608 | AC, 0 609 | CALLEE, 0 610 | 611 | TESTS, 0 612 | KZERO, 260 613 | KBELL, 207 614 | KCR, 215 615 | KLF, 212 616 | KDOT, 256 617 | 618 | PZERO=. 619 | ~ 620 | $ 621 | -------------------------------------------------------------------------------- /e5-smallest-multiple/project.clj: -------------------------------------------------------------------------------- 1 | (defproject smallest-multiple "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :main smallest-multiple 7 | :dependencies [[org.clojure/clojure "1.10.1"]] 8 | :profiles {:dev {:dependencies [[speclj "3.3.2"]]}} 9 | :plugins [[speclj "3.3.2"]] 10 | :test-paths ["spec"] 11 | :java-source-paths ["../e3_primeFactors/sieve/src"]) 12 | -------------------------------------------------------------------------------- /e5-smallest-multiple/spec/smallest_multiple_spec.clj: -------------------------------------------------------------------------------- 1 | (ns smallest-multiple-spec 2 | (:require [speclj.core :refer :all] 3 | [smallest-multiple :refer :all]) 4 | ) 5 | 6 | 7 | (describe "smallest-multiple" 8 | (it "is 1 with no inputs" 9 | (should= 1 (lcm []))) 10 | 11 | (it "is 1 with [1]" 12 | (should= 1 (lcm [1]))) 13 | 14 | (it "is 2 with [2]" 15 | (should= 2 (lcm [2]))) 16 | 17 | (it "is 2 with [1 2]" 18 | (should= 2 (lcm [1 2]))) 19 | 20 | (it "is 4 with [2 4]" 21 | (should= 4 (lcm [2 4]))) 22 | 23 | (it "is 2520 with (range 1 11)" 24 | (should= 2520 (lcm (range 1 11)))) 25 | 26 | (it "is answer with (range 1 21)" 27 | (should= 232792560 (lcm (range 1 21)))) 28 | ) 29 | 30 | (describe "gcd" 31 | (it "is 1 if mutually prime" 32 | (should= 1 (gcd 3 5))) 33 | 34 | (it "is n if a = b" 35 | (should= 3 (gcd 3 3))) 36 | 37 | (it "is b if b divides a" 38 | (should= 3 (gcd 6 3))) 39 | 40 | (it "is the largest n such that n divides a and b" 41 | (should= 4 (gcd 8 12))) 42 | 43 | ) 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | ; 60 | ; (it "is 12 with [2 3 4]" 61 | ; (should= 12 (lcm [2 3 4]))) 62 | ; 63 | 64 | ; 65 | 66 | ; ) 67 | 68 | 69 | ; 70 | 71 | ; 72 | 73 | ; 74 | 75 | ; 76 | ; ) -------------------------------------------------------------------------------- /e5-smallest-multiple/src/smallest_multiple.clj: -------------------------------------------------------------------------------- 1 | (ns smallest-multiple) 2 | 3 | (defn odious? [n] 4 | (let [b (Integer/toString n 2) 5 | ones (filter #(= \1 %) (seq b))] 6 | (odd? (count ones)))) 7 | 8 | (defn odious [n] 9 | (filter odious? (range 1 (inc n)))) 10 | 11 | (defn evil [n] 12 | (remove odious? (range 1 (inc n)))) 13 | 14 | (defn tri [n] 15 | (/ (+ (* n n) n) 2)) 16 | 17 | (defn tris [n] 18 | (map tri (range 1 (inc n)))) 19 | 20 | (defn fibs 21 | ([a b] 22 | (lazy-seq 23 | (cons a (fibs b (+ a b))))) 24 | ([] (fibs 1N 1N)) 25 | ) 26 | e 27 | (defn factors-of [n] 28 | (loop [factors [] n n divisor 2] 29 | (if (> n 1) 30 | (cond 31 | (> divisor (Math/sqrt n)) 32 | (conj factors n) 33 | (= 0 (mod n divisor)) 34 | (recur (conj factors divisor) 35 | (quot n divisor) 36 | divisor) 37 | :else 38 | (recur factors n (inc divisor))) 39 | factors))) 40 | 41 | (defn gcd [a b] 42 | (if (zero? b) 43 | a 44 | (gcd b (mod a b)))) 45 | 46 | (defn lcm 47 | ([a b] 48 | (/ (* a b) (gcd a b))) 49 | ([ns] 50 | (reduce lcm 1N ns)) 51 | ) 52 | 53 | (defn fac [n] (apply * (range 1N (inc n)))) 54 | 55 | (defn ratio [n] 56 | (let [f (fac n) 57 | l (lcm (range 1N (inc n)))] 58 | [f l (/ f l)])) -------------------------------------------------------------------------------- /e6-problems-galore/e6: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/unclebob/Euler/b2ba3b0c582c71f6b9a72a1effa318a6e4761514/e6-problems-galore/e6 -------------------------------------------------------------------------------- /e6-problems-galore/e6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define NDIGITS 40 6 | 7 | void printNum(char* n) { 8 | int printed_a_digit = 0; 9 | 10 | for (int i=NDIGITS-1; i>=0; i--) { 11 | if (!printed_a_digit && (n[i] == 0)) 12 | continue; 13 | else { 14 | putchar('0'+n[i]); 15 | printed_a_digit = 1; 16 | } 17 | } 18 | if (!printed_a_digit) 19 | putchar('0'); 20 | putchar('\n'); 21 | } 22 | 23 | 24 | int hiDigit(char* n) { 25 | int hi = 0; 26 | for (int i=0; i0) 28 | hi=i; 29 | return hi; 30 | } 31 | 32 | void addTo(char* b, long n) { 33 | for(int d=0; d=10) { 40 | b[d] -= 10; 41 | b[d+1]++; 42 | } 43 | } 44 | } 45 | 46 | void moveNum(char* to, char* from) { 47 | bcopy(from, to, NDIGITS); 48 | } 49 | 50 | void multiplyBy(char* dest, char* m) { 51 | char partial[NDIGITS]; 52 | bzero(partial, NDIGITS); 53 | for (int di=0; di<=hiDigit(dest); di++) { 54 | for (int mi=0; mi<=hiDigit(m); mi++){ 55 | long p = dest[di] * m[mi]; 56 | addTo(partial+di+mi, p); 57 | } 58 | } 59 | moveNum(dest, partial); 60 | } 61 | 62 | void subtractFrom(char* dest, char* n) { 63 | for (int i=0; i primesUpTo(long n) { 10 | if (n < 2) 11 | return new ArrayList<>(); 12 | return sieve(n); 13 | } 14 | 15 | public static List sieve(long n) { 16 | List primes = new ArrayList<>(); 17 | int nints = (int) (n / 32) + 1; 18 | composites = new int[nints]; 19 | int count = 0; 20 | for (long candidate = 2; candidate <= n; candidate += (candidate==2)?1:2) { 21 | if (!isComposite(candidate)) { 22 | if (++count >= 999999999) { 23 | System.out.println("n= " + count + " prime = " + candidate); 24 | } 25 | // primes.add(candidate); 26 | if (count > 1000000001) 27 | return primes; 28 | for (long multiple = 2 * candidate; multiple <= n; multiple += candidate) 29 | setComposite(multiple); 30 | } 31 | } 32 | return primes; 33 | } 34 | 35 | private static void setComposite(long candidate) { 36 | int wordIndex = (int) (candidate / 32); 37 | int bitIndex = (int) (candidate % 32); 38 | int bit = (1 << bitIndex); 39 | composites[wordIndex] |= bit; 40 | } 41 | 42 | private static boolean isComposite(long candidate) { 43 | int wordIndex = (int) (candidate / 32); 44 | int theWord = composites[wordIndex]; 45 | int bitIndex = (int) (candidate % 32); 46 | int bit = (1 << bitIndex); 47 | int result = theWord & bit; 48 | return result != 0; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /e7-1001st-prime/sieve/test/sieve/SieveTest.java: -------------------------------------------------------------------------------- 1 | package sieve; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.collection.IsEmptyCollection.empty; 7 | import static org.hamcrest.collection.IsIterableContainingInOrder.contains; 8 | import static org.hamcrest.core.IsEqual.equalTo; 9 | 10 | public class SieveTest { 11 | @Test 12 | public void testSieve() throws Exception { 13 | assertThat(Sieve.primesUpTo(1), empty()); 14 | assertThat(Sieve.primesUpTo(2), contains(2)); 15 | assertThat(Sieve.primesUpTo(10), contains(2, 3, 5, 7)); 16 | assertThat(Sieve.primesUpTo(1000).size(), equalTo(168)); 17 | assertThat(Sieve.primesUpTo(1000000).size(), equalTo(78498)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /e7-1001st-prime/spec/e7_1001st_prime/core_spec.clj: -------------------------------------------------------------------------------- 1 | (ns e7-1001st-prime.core-spec 2 | (:require [speclj.core :refer :all] 3 | [e7-1001st-prime.core :refer :all])) 4 | 5 | ;(describe "primes using fast java sieve" 6 | ; (it "should find distant prime" 7 | ; (let [primes (fast-primes-up-to 200000N)] 8 | ; (should= 0 (nth primes 10000))))) 9 | 10 | ;(describe "primes using slow clojure sieve" 11 | ; (it "should find distant prime" 12 | ; (let [primes (get-primes-up-to 3000000000)] 13 | ; (should= 0 (nth primes 100000000))))) 14 | 15 | (describe "lazy-primes" 16 | (context "is-prime?" 17 | (it "knows the first prime" 18 | (should-not (is-prime? [] 0)) 19 | (should-not (is-prime? [] 1)) 20 | (should (is-prime? [] 2))) 21 | 22 | (it "knows 3" 23 | (should (is-prime? [2] 3))) 24 | 25 | (it "knows 5" 26 | (should-not (is-prime? [2 3] 4)) 27 | (should (is-prime? [2 3] 5)))) 28 | 29 | (context "next-prime" 30 | (it "should find the next primes" 31 | (should= 2 (get-next-prime [])) 32 | (should= 7 (get-next-prime [2 3 5])) 33 | (should= 11 (get-next-prime [2 3 5 7])))) 34 | 35 | (context "primes" 36 | (it "finds primes" 37 | (let [ps (primes)] 38 | (should= 2 (first ps)) 39 | (should= 3 (second ps)) 40 | (should= 5 (nth ps 2)) 41 | (should= 7 (nth ps 3))))) 42 | ) 43 | 44 | ;(describe "primes using lazy primes" 45 | ; (it "should find distant prime" 46 | ; (let [primes (primes)] 47 | ; (should= 0 (nth primes 100000))))) 48 | 49 | (describe "find repeats in the sieve" 50 | (it "finds the degenerate repeat" 51 | (should= [0 0] (find-repeating-pattern []))) 52 | 53 | (it "finds single and double patterns" 54 | (should= [0 0] (find-repeating-pattern [true])) 55 | (should= [0 0] (find-repeating-pattern [true false])) 56 | (should= [0 0] (find-repeating-pattern [false true])) 57 | (should= [0 1] (find-repeating-pattern [true true])) 58 | ) 59 | 60 | (it "finds triple patterns" 61 | (should= [0 1] (find-repeating-pattern [false false false])) 62 | (should= [0 0] (find-repeating-pattern [false false true])) 63 | (should= [0 0] (find-repeating-pattern [false true false])) 64 | (should= [1 1] (find-repeating-pattern [false true true])) 65 | (should= [1 1] (find-repeating-pattern [true false false])) 66 | (should= [0 0] (find-repeating-pattern [true false true])) 67 | (should= [0 0] (find-repeating-pattern [true true false])) 68 | (should= [0 1] (find-repeating-pattern [true true true]))) 69 | 70 | (it "finds quad patterns" 71 | (should= [0 1] (find-repeating-pattern [false false false false])) 72 | (should= [0 0] (find-repeating-pattern [false false false true])) 73 | (should= [0 0] (find-repeating-pattern [false false true false])) 74 | (should= [2 1] (find-repeating-pattern [false false true true])) 75 | (should= [2 1] (find-repeating-pattern [false true false false])) 76 | (should= [0 2] (find-repeating-pattern [false true false true])) 77 | (should= [0 0] (find-repeating-pattern [false true true false])) 78 | (should= [1 1] (find-repeating-pattern [false true true true])) 79 | (should= [1 1] (find-repeating-pattern [true false false false])) 80 | (should= [0 0] (find-repeating-pattern [true false false true])) 81 | (should= [0 2] (find-repeating-pattern [true false true false])) 82 | (should= [2 1] (find-repeating-pattern [true false true true])) 83 | (should= [2 1] (find-repeating-pattern [true true false false])) 84 | (should= [0 0] (find-repeating-pattern [true true false true])) 85 | (should= [0 0] (find-repeating-pattern [true true true false])) 86 | (should= [0 1] (find-repeating-pattern [true true true true]))) 87 | 88 | (it "detects repeats" 89 | (should-not (is-repeat? 0 1 [true false])) 90 | (should (is-repeat? 0 1 [true true])) 91 | (should-not (is-repeat? 0 2 [false true true false])) 92 | 93 | (should (is-repeat? 0 2 [false true false true false])) 94 | 95 | ) 96 | ) -------------------------------------------------------------------------------- /e7-1001st-prime/src/e7_1001st_prime/core.clj: -------------------------------------------------------------------------------- 1 | (ns e7-1001st-prime.core 2 | (:import (sieve Sieve))) 3 | 4 | (defn factors-of [n] 5 | (loop [factors [] n n divisor 2] 6 | (if (> n 1) 7 | (cond 8 | (> divisor (Math/sqrt n)) 9 | (conj factors n) 10 | (= 0 (mod n divisor)) 11 | (recur (conj factors divisor) 12 | (quot n divisor) 13 | divisor) 14 | :else 15 | (recur factors n (inc divisor))) 16 | factors))) 17 | 18 | (defn fast-primes-up-to [n] 19 | (vec (Sieve/primesUpTo n))) 20 | 21 | (defn mark-multiples [composites prime n] 22 | (loop [i (* 2 prime)] 23 | (when (<= i n) 24 | (aset composites i true) 25 | (recur (+ i prime)))) 26 | ) 27 | 28 | (defn filter-primes [composites n] 29 | (loop [primes [] i 2] 30 | (if (> i n) 31 | primes 32 | (if (aget composites i) 33 | (recur primes (inc i)) 34 | (recur (conj primes i) (inc i)))))) 35 | 36 | (declare find-repeating-pattern) 37 | 38 | (defn sieve [n] 39 | (let [composites (make-array Boolean/TYPE (inc n)) 40 | limit (int (Math/sqrt n))] 41 | (loop [candidate 2] 42 | (if (> candidate n) 43 | (filter-primes composites n) 44 | (if (aget composites candidate) 45 | (recur (inc candidate)) 46 | (do (when (<= candidate limit) 47 | (mark-multiples composites candidate n)) 48 | ;(prn candidate (find-repeating-pattern (vec composites))) 49 | (recur (inc candidate)))))))) 50 | 51 | (defn get-primes-up-to [n] 52 | (if (> n 1) 53 | (sieve n) 54 | [])) 55 | 56 | (defn is-prime? [primes candidate] 57 | (if (empty? primes) 58 | (= 2 candidate) 59 | ;(not-any? zero? (map #(rem candidate %) primes)) 60 | (loop [primes primes] 61 | (if (empty? primes) 62 | true 63 | (if (zero? (rem candidate (first primes))) 64 | false 65 | (recur (rest primes))))) 66 | )) 67 | 68 | (defn get-next-prime [primes] 69 | (if (empty? primes) 70 | 2 71 | (loop [n (inc (last primes))] 72 | (if (is-prime? primes n) 73 | n 74 | (recur (inc n)))))) 75 | 76 | (defn primes 77 | ([] (primes 2 [2])) 78 | ([this-prime current-primes] 79 | (let [next-prime (get-next-prime current-primes)] 80 | (lazy-seq 81 | (cons this-prime 82 | (primes next-prime (conj current-primes next-prime))) 83 | )))) 84 | 85 | (defn is-repeat? [i j bools] 86 | (let [n (count bools)] 87 | (loop [i i 88 | j j] 89 | (if (>= j n) 90 | true 91 | (if (= (nth bools i) (nth bools j)) 92 | (recur (inc i) (inc j)) 93 | false))))) 94 | 95 | (defn find-repeating-pattern 96 | ([bools] 97 | (find-repeating-pattern 98 | (if (vector? bools) 99 | (into-array Boolean/TYPE bools) 100 | bools) 101 | 0)) 102 | ([bools pos] 103 | (let [n (count bools)] 104 | (if (or (< n 2) (> pos 30)) 105 | [0 0] 106 | (loop [period 1] 107 | (if (> period (/ n 2)) 108 | (find-repeating-pattern (rest bools) (inc pos)) 109 | (if (is-repeat? 0 period bools) 110 | [pos period] 111 | (recur (inc period))))))) 112 | )) 113 | 114 | (defn gcd [a b] 115 | (if (zero? b) 116 | a 117 | (gcd b (mod a b)))) 118 | 119 | (defn lcm 120 | ([a b] 121 | (/ (* a b) (gcd a b))) 122 | ([ns] 123 | (reduce lcm 1N ns)) 124 | ) -------------------------------------------------------------------------------- /e8-largest-product-in-a-series/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /lib 3 | /classes 4 | /checkouts 5 | pom.xml 6 | *.jar 7 | *.class 8 | .lein-deps-sum 9 | .lein-failures 10 | .lein-plugins 11 | .lein-repl-history 12 | -------------------------------------------------------------------------------- /e8-largest-product-in-a-series/README.md: -------------------------------------------------------------------------------- 1 | # e8-largest-product-in-a-series 2 | -------------------------------------------------------------------------------- /e8-largest-product-in-a-series/data.txt: -------------------------------------------------------------------------------- 1 | 73167176531330624919225119674426574742355349194934 2 | 96983520312774506326239578318016984801869478851843 3 | 85861560789112949495459501737958331952853208805511 4 | 12540698747158523863050715693290963295227443043557 5 | 66896648950445244523161731856403098711121722383113 6 | 62229893423380308135336276614282806444486645238749 7 | 30358907296290491560440772390713810515859307960866 8 | 70172427121883998797908792274921901699720888093776 9 | 65727333001053367881220235421809751254540594752243 10 | 52584907711670556013604839586446706324415722155397 11 | 53697817977846174064955149290862569321978468622482 12 | 83972241375657056057490261407972968652414535100474 13 | 82166370484403199890008895243450658541227588666881 14 | 16427171479924442928230863465674813919123162824586 15 | 17866458359124566529476545682848912883142607690042 16 | 24219022671055626321111109370544217506941658960408 17 | 07198403850962455444362981230987879927244284909188 18 | 84580156166097919133875499200524063689912560717606 19 | 05886116467109405077541002256983155200055935729725 20 | 71636269561882670428252483600823257530420752963450 21 | -------------------------------------------------------------------------------- /e8-largest-product-in-a-series/project.clj: -------------------------------------------------------------------------------- 1 | (defproject e8-largest-product-in-a-series "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :main e8-largest-product-in-a-series.core 7 | :dependencies [[org.clojure/clojure "1.11.1"]] 8 | :profiles {:dev {:dependencies [[speclj "3.3.2"]]}} 9 | :plugins [[speclj "3.3.2"]] 10 | :test-paths ["spec"]) 11 | -------------------------------------------------------------------------------- /e8-largest-product-in-a-series/spec/e8_largest_product_in_a_series/core_spec.clj: -------------------------------------------------------------------------------- 1 | (ns e8-largest-product-in-a-series.core-spec 2 | (:require [speclj.core :refer :all] 3 | [e8-largest-product-in-a-series.core :refer :all])) 4 | 5 | (describe "parsing input" 6 | (it "gathers lines into a string" 7 | (should= "1234" (parse-input "1 \n2 \n3 \n4")) 8 | (should= "12345678" (parse-input "12\n345\n6\n78 "))) 9 | 10 | (it "can read the actual input data" 11 | (let [data (slurp "data.txt") 12 | data (parse-input data)] 13 | (should= 1000 (count data))))) 14 | 15 | (describe "prepare data" 16 | (it "turns string into digits" 17 | (should= [] (string->digits "")) 18 | (should= [1] (string->digits "1")) 19 | (should= [0 1 2 3 4 5 6 7 8 9] (string->digits "0123456789")))) 20 | 21 | (describe "sliding window" 22 | (it "walks list selecting n adjacent items" 23 | (should= [] (gather-groups 1 [])) 24 | (should= [[1]] (gather-groups 1 [1])) 25 | (should= [] (gather-groups 2 [1])) 26 | (should= [[1 2]] (gather-groups 2 [1 2])) 27 | (should= [[1] [2] [3]] (gather-groups 1 [1 2 3])) 28 | (should= [[1 2] [2 3]] (gather-groups 2 [1 2 3])))) 29 | 30 | 31 | (def data (-> "data.txt" slurp parse-input string->digits)) 32 | 33 | (describe "find largest product" 34 | (it "finds largest product of 2" 35 | (should= [6 [2 3]] (find-largest-product 2 [1 1 2 3]))) 36 | (it "finds largest product of 4" 37 | (should= [5832 [9 9 8 9]] (find-largest-product 4 data))) 38 | 39 | (it "finds largest product of 13" 40 | (should= [23514624000 [5 5 7 6 6 8 9 6 6 4 8 9 5]] (find-largest-product 13 data))) 41 | 42 | (it "finds largest product of 30" 43 | (should= 374476218826752000000N (first (find-largest-product 30 data)))) 44 | 45 | (it "finds largest product of 100" 46 | (should= 0 (first (find-largest-product 100 data)))) 47 | ) 48 | 49 | (describe "finding longest strings" 50 | (it "finds longest non-zero product" 51 | (let [[product group] (find-longest-product data)] 52 | (should= 2412446685431734624320887406251212800000000N product) 53 | (should= 69 (count group)) 54 | 55 | ) 56 | ) 57 | ) 58 | 59 | 60 | -------------------------------------------------------------------------------- /e8-largest-product-in-a-series/src/e8_largest_product_in_a_series/core.clj: -------------------------------------------------------------------------------- 1 | (ns e8-largest-product-in-a-series.core 2 | (:require [clojure.string :as string])) 3 | 4 | (defn factors-of [n] 5 | (loop [factors [] n n divisor 2] 6 | (if (> n 1) 7 | (cond 8 | (> divisor (Math/sqrt n)) 9 | (conj factors n) 10 | (= 0 (mod n divisor)) 11 | (recur (conj factors divisor) 12 | (quot n divisor) 13 | divisor) 14 | :else 15 | (recur factors n (inc divisor))) 16 | factors))) 17 | 18 | (defn parse-input [text] 19 | (let [lines (string/split-lines text) 20 | lines (map #(.trim %) lines)] 21 | (apply str lines))) 22 | 23 | (defn string->digits [s] 24 | (map #(- (int %) (int \0)) s)) 25 | 26 | (defn gather-groups [group-size items] 27 | (loop [items items 28 | groups []] 29 | (if (> group-size (count items)) 30 | groups 31 | (recur (rest items) (conj groups (take group-size items)))))) 32 | 33 | (defn find-largest-product [n digits] 34 | (let [groups (gather-groups n digits) 35 | groups (filter #(not-any? zero? %) groups)] 36 | (if (empty? groups) 37 | [0 []] 38 | (let [products (map #(reduce *' %) groups) 39 | pairs (apply hash-map (interleave products groups)) 40 | max-product (apply max products) 41 | group (get pairs max-product)] 42 | [max-product group])))) 43 | 44 | (defn find-longest-product [digits] 45 | (loop [n 1 46 | result []] 47 | (let [[product group] (find-largest-product n digits)] 48 | (if (zero? product) 49 | result 50 | (recur (inc n) [product group]))) 51 | ) 52 | ) 53 | --------------------------------------------------------------------------------