问题描述:
The sum of the squares of the first ten natural numbers is,
12+ 22+ ... + 102= 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2= 552= 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025385
= 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
(ns euler-problem-6.core
(:use [clojure.contrib.math]))
(defn square
[num]
(* num num))
(square 3)
(defn square-sum-version-1
[num]
(loop
[current-num 1M sum 0M]
(if (= current-num num)
(+ sum (square current-num))
(recur (inc current-num) (+ sum (square current-num))))))
(defn square-sum-version-2
[num]
(reduce + (map square (range 1M (inc num)))))
;(time (square-sum-version-1 1000000M))
;(time (square-sum-version-2 1000000M))
(defn sum-square-version-1
[num]
(loop
[current-num 1M add-sum 0M]
(if (= current-num num)
(square (+ add-sum current-num))
(recur (inc current-num) (+ current-num add-sum)))))
(defn sum-square-version-2
[num]
(square (reduce + (range 1M (inc num)))))
;(sum-square-version-1 10)
;(sum-square-version-2 10)
(defn dif-sum-square-with-square-sum
[num]
(abs (- (sum-square-version-1 num) (square-sum-version-1 num))))
(dif-sum-square-with-square-sum 100M)
经测试,尾递归版本的性能要好于延迟加载的版本,但是延迟加载程序简单明了!
分享到:
相关推荐
该存储库是使用Clojure解决Euler问题的支架。 它以有组织的结构生成规格和解决方案存根,从而使生活稍微好一些。 安装 如果尚未安装,则将其添加到您的路径中。 签出这个项目: $ git clone git@github....
Clojure骇客解决了Euler项目的问题。 目的是用惯用的Clojure编写功能解决方案。 用法 大多数解决方案都是完整的,并且完全独立地包含在具有“解决”功能的唯一名称空间中。 有些正在“进行中”。 执照 版权所有:...
欧拉计划 问题1-2、4、6、8-10、12、14、21、24-26、32、34、36、39、41、46、49、52、55-58、62、65的Clojure解决方案70-74、87、92、97、124、173-174、179、187、214运行解决方案使用以下命令行运行解决方案: ...
Clojure's clean, careful design lets you write programs that get right to the essence of a problem, without a lot of clutter and ceremony. Clojure is Lisp reloaded. Clojure has the power inherent in...
欧拉解决Clojure中的Euler项目问题的方法。 持续的爱好。执照版权所有:copyright:2012-2015 Dave Yarwood 在Eclipse Public License 1.0版下分发。
Clear, practical Clojure for the professional programmer Professional Clojure is the experienced developer's guide to functional programming using the Clojure language. Designed specifically to meet ...
在今后的软件设计中无论使用Clojure语言,还是坚持使用Java语言,Clojure语言都将与java做比较,哪种是设计软件的最佳方式。 Clojure语言是一个JVM(包括Groovy,Jython和JRuby等语言)的新语言,它提供了活力,...
Clojure is an opinionated language—it doesn’t try to cover all paradigms or provide every checklist bullet-point feature. Instead it provides the features needed to solve all kinds of real-world ...
Practical Clojure Clojure语言书籍
clojure clojure clojureclojure clojure
【1】[Clojure编程乐趣](The Joy of Clojure).pdf 【2】Clojure – Functional Programming for the JVM中文版.pdf ...【6】Clojure in Action.pdf 【7】clojure in small pieces.pdf 以上7本书的电子版
主要特性:从 Java 中保护你的 Clojure 程序,进行安全的互操作,正确的使用外部 Java 库。Typed Clojure 支持关键字映射的使用,类型检测器中包含有用的映射操作,包括关键字查找,添加或者移除关键字,合并映射...
[2013] Functional Programming Patterns in Scala and Clojure - Write Lean Programs for the JVM.(Michael Bevilacqua-Linn).[1937785475].pdf+epub.rar [2014] Clojure Cookbook - Recipes for Functional ...
Clojure编程乐趣和clojure_programming.pdf两本书
tools.analyzer, 用于Clojure代码的分析器,用Clojure编写,并在EDN中生成 AST tools.analyzer主机无关Clojure代码的分析器,用Clojure编写,并在EDN中生成 AST 。我在 2015年12月的ClojureX上讨论了 tools.analyzer...
Chapter 6. Exploring Category Theory Chapter 7. Programming with Logic Chapter 8. Leveraging Asynchronous Tasks Chapter 9. Reactive Programming Chapter 10. Testing Your Code Chapter 11. ...
4clojure解决方案对问题的解决方案。 不是最惯用的 Clojure 代码,但到底是什么 - 它有效! :grinning_face:小费不要复制/粘贴解决方案,而是在理解所遵循的方法后尝试自己编写。 使用这些解决方案来找出算法而不是...
[2009] Programming Clojure.(Stuart Halloway).[1934356336].pdf [2010] Functional Programming with Clojure - Simple Concurrency on the JVM.(Tim Berglund, Matthew McCullough).[193650202X].pdf [2010] ...
clojure1.3.0及资料,附《Programming Clojure》,《Practical Clojure》