ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

F#

F# で非線形方程式を解く (2分法)

非線形方程式の解法(2分法)を利用して2の平方根を求める 1. まず, 条件 を満たす点 を考えると, 関数 の解は, 区間 の中に存在する. 2. 次に, 区間 の中点 を考えると, であれば, 解は区間 の中に存在し, 同様に, であれば, 区間 の中に存在する. 3. この…

F#で関数の近似(ラグランジュ補間)

をラグランジュ補間で近似するn+1個の点 (x0, y0), (x1, y1) … (xn, yn) が与えられているとき, これらすべての点を通る n次式は次のように表すことができる. この式を使って, 与えられた点以外の点の値を求める. module Fs0701 // データ点の数 - 1 let N =…

さまざまな言語で四則演算と数値の出力

Scala object Scala0101 { def main(args:Array[String]) { println(3 + 5) println(3 - 5) println(3 * 5) println(Math.pow(3, 5)) println(5 / 3) println(5.0 / 3) println(5 / 3.0) println(5 % 3) printf("%d\n", 3 * 5) print(3 * 5 + "\n") println(…

System.Numerics.BigInteger.Log10

F#

前々から、BigInteger.Log10 が使えなくて、何でだろうと思ってたんですが、 c:\>fsi Microsoft (R) F# 2.0 Interactive build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;; > System.Numerics.BigInteger.Log…

F# で Project Euler Problem 25

> (999.0 + (System.Math.Log10 5.0) / 2.0) / (System.Math.Log10 ((1.0 + (sqrt 5.0)) / 2.0)) - ;; val it : float = 4781.859271 > System.Convert.ToString(System.Numerics.BigInteger.Pow(1618033989I, 4781) / System.Numerics.BigInteger.Pow(10000…

F# で Project Euler Problem 24

まづ、0,1,2,3 の数列で試してみる > let problem24 x = - let i = ref x - let s = ref (Set [0..3]) - [3 .. -1 .. 1] - |> List.iter - ( - fun n -> - let j = if n = 1 then 1 else List.reduce(*) [n .. -1 .. 2] - let mutable k = !i / j - i := !i …

F# で Project Euler Problem 23

与えられた数を素因数分解して、素因数とその指数とを Map にして返す関数 > let rec get_prime_factor (map:Map<int, int> byref) (n:int) (factor: int) = - if n >= factor then - if n % factor <> 0 then - get_prime_factor &map n (factor + 1) - else - if Map</int,>…

F# で Project Euler Problem 22

まづは、ファイルの読み込み > let lines = System.IO.File.ReadAllLines @"D:\Euler\022\names.txt" - ;; val lines : string [] = [|""MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","M"+[46386 chars]|]> let name_list = lines |> Array.m…

F# で Project Euler Problem 21

> let mutable map:Map<int, int> = Map.empty;; val mutable map : Map<int,int> = map [] > let rec get_prime_factor (map:Map<int, int> byref) (n:int) (factor: int) = - if n >= factor then - if n % factor <> 0 then - get_prime_factor &map n (factor + 1) - else - if Map.co</int,></int,int></int,>…

F# で Project Euler Problem 20

> [1I..100I] |> List.reduce(*) - ;; val it : System.Numerics.BigInteger = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 {IsEve…

F# で Project Euler Problem 19

> let weekday year month d = - let mutable y = year - let mutable m = month - - if (m = 1 || m = 2) then - y <- y - 1 - m <- m + 12 - (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + d) % 7 - ;; val weekday : int -> int -> int -> int> w…

F# で Project Euler Problem 18

> let str = " - 75 - 95 64 - 17 47 82 - 18 35 87 10 - 20 04 82 47 65 - 19 01 23 75 03 34 - 88 02 77 73 07 63 67 - 99 65 04 28 06 16 70 92 - 41 41 26 56 83 40 80 70 33 - 41 48 72 33 47 32 37 16 94 29 - 53 71 44 65 25 43 91 52 97 51 14 - 70 …

F# で Project Euler Problem 17

> let mutable map:Map<int, string> = Map.empty;; val mutable map : Map<int,string> = map [] > map <- map |> Map.add 1 "one";; val it : unit = () > map <- map |> Map.add 2 "two";; val it : unit = () > map <- map |> Map.add 3 "three";; val it : unit = () > map <- ma</int,string></int,>…

F# で Project Euler Problem 16

> System.Numerics.BigInteger.Pow(2I,15);; val it : System.Numerics.BigInteger = 32768I > System.Numerics.BigInteger.Pow(2I,100);; val it : System.Numerics.BigInteger = 1267650600228229401496703205376I> System.Convert.ToString (System.Numer…

F# で Project Euler Problem 15

> ([21..40] |> List.reduce(*)) / ([1..20] |> List.reduce(*)) - ;; val it : int = 0 > ([21I..40I] |> List.reduce(*)) / ([1I..20I] |> List.reduce(*)) - ;; val it : System.Numerics.BigInteger = 137846528820I

F# で Project Euler Problem 14

今回は、Scala 版とは違って、辞書にコラッツ数列そのものを格納するのではなく、 次の数だけ格納してみる。 > let rec collatz_list(n:int64): List<int64> = - if (n = 1L) then [1L] - elif (n % 2L = 0L) then n::collatz_list(n / 2L) - else n::collatz_list(</int64>…

F# で Project Euler Problem 13

Scala でのロジックをそのまま移植 > let str = " - 37107287533902102798797998220837590246510135740250 - 46376937677490009712648124896970078050417018260538 - 74324986199524741059474233309513058123726617309629 - 9194221336357416157252243056330…

F# で Project Euler Problem 12

Scala でのロジックをそのまま移植 > let rec get_prime_factor_list(n:int64, factor:int64) = - if n < factor * factor then [n] - elif n % factor = 0L then factor::get_prime_factor_list(n / factor, factor ) - else get_prime_factor_list(n , fac…

F# で Project Euler Problem 11

まづ、格子状の文字列を List に 格納する > let str = " - 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 - 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 - 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65…

F# で Project Euler Problem 10

> prime_sum;; val it : int = 5 > let mutable prime_sum = 2 + 3;; val mutable prime_sum : int = 5 > let mutable num = 5;; val mutable num : int = 5 > let mutable fPrime = true;; val mutable fPrime : bool = true > let mutable fWhile = true;;…

F# で Project Euler Problem 9

> [1..9] - ;; val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9] > [1..9] - |> List.map(fun i -> [i..9]) - ;; val it : int list list = [[1; 2; 3; 4; 5; 6; 7; 8; 9]; [2; 3; 4; 5; 6; 7; 8; 9]; [3; 4; 5; 6; 7; 8; 9]; [4; 5; 6; 7; 8; 9]; [5; 6; 7…

F# で Project Euler Problem 8

まず、複数行文字列 > " - 12345 - 67890 - " - ;; val it : string = " 12345 67890 " > let str = " - 12345 - 67890 - ";; val str : string = " 12345 67890 "改行を取り除く > str.Replace("\n", "") - ;; val it : string = "1234567890"文字列の中の…

F# で Project Euler Problem 7

素数を小さい方から6つ並べると 2, 3, 5, 7, 11, 13 であり, 6番目の素数は 13 である. 10 001 番目の素数を求めよ. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%207By listing the first six prime numbers: 2, 3, 5, 7, 11, an…

F# で Project Euler Problem 6

最初の10個の自然数について, その二乗の和は, 12 + 22 + ... + 102 = 385 最初の10個の自然数について, その和の二乗は, (1 + 2 + ... + 10)2 = 3025 これらの数の差は 3025 - 385 = 2640 となる. 同様にして, 最初の100個の自然数について二乗の和と和の二…

F# で Project Euler Problem 5

2520 は 1 から 10 の数字の全ての整数で割り切れる数字であり, そのような数字の中では最小の値である. では, 1 から 20 までの整数全てで割り切れる数字の中で最小の正の数はいくらになるか. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&pag…

F# で Project Euler Problem 4 その3

昨日の続き いげ太氏から、「Scala で言う flatMap 相当が、F# では collect として定義されていますので、List.collect id で平坦化できますよ。」 とコメントを頂いたので使ってみる。 > [1..9] - |> List.map(fun i -> [i..9] |> List.map(fun j -> (i, j…

F# で Project Euler Problem 4 その2

昨日の続き Scala のロジックを そのまま移植しようとしたが、F# には flatten が見当たらないので ごにょごにょしてみる... > [1..9] - |> List.map(fun i -> - [i..9] |> List.map(fun j -> (i, j)) - ) - ;; val it : (int * int) list list = [[(1, 1); …

F# で Project Euler Problem 4

左右どちらから読んでも同じ値になる数を回文数という. 2桁の数の積で表される回文数のうち, 最大のものは 9009 = 91 × 99 である. では, 3桁の数の積で表される回文数のうち最大のものを求めよ. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&p…

F# で Project Euler Problem 3

> let rec factor_list (n, factor) = - if n < factor * factor then [n] - elif n % factor = 0 then factor::factor_list(n / factor, factor ) - else factor_list(n , factor + 1) - ;; val factor_list : int * int -> int list > factor_list(13195, …

F# で Project Euler Problem 2

> [1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610; 987; 1597; 2584; 4181; 6765; 10946; 17711; 28657; 46368; 75025; 121393; 196418; 317811; 514229; 832040; 1346269; 2178309; 3524578] - ;; val it : int list = [1; 2; 3; 5; 8; 13; 21; 3…