«前の日記(2017-08-20) 最新 次の日記(2017-08-24)» 編集

ToDo:

  • 99 深呼吸して、おちけつ (~12/31 あと278日)
  • 98 毎日アウトプットを重きにおいた目標をたてる (~12/31 あと278日)
  • 97 買物は行動を変えるもの・ことに限る (~12/31 あと278日)
  • 96 合理的な行動はときに我慢を強いる(~12/31 あと278日)

ɥozɐʞıɥ


2017-08-21 Clojure再び [長年日記]

_ [Clojure] ニュートン法で2の平方根を求める

Rustづいていたが並行してClojureも勉強するので計算機プログラムの構造と解釈 第2版を読んでいます。

ニュートン法で平方根を求めるのです。

iterateとlazy-seqでそれぞれ遅延評価を使っています。

修正しました。第一引数が有効数で第二引数が平方根で求める値です。初期値は1.で固定です。*1

(ns newton.core

(:gen-class))

(defn newton[[x y]]

(cons x (lazy-seq (newton [(/ (+' (/ y x) x ) 2) y]))))

(defn -main[& args]

(do

(println

(first

(first

(first

(filter

#(< (Math/abs (- (first (first %) ) (first (second %)))) (Float/parseFloat (first args)))

(partition 2 1

(iterate

(fn [[x y]] [(/ (+' (/ y x) x ) 2) y] ) [1. (Integer/parseInt (second args))])))))))

(println

(first

(first

(filter

#(< (Math/abs (- (first %) (second %))) (Float/parseFloat (first args)))

(partition 2 1 (newton [1. (Integer/parseInt (second args))]) )))))))

*1 8/23修正



2002|09|10|11|12|
2003|01|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|
«前の日記(2017-08-20) 最新 次の日記(2017-08-24)» 編集