ONLY DO WHAT ONLY YOU CAN DO

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

Project Euler

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…

Project Euler Problem 25

フィボナッチ数列で1000桁になる最初の項は何番目か?http://odz.sakura.ne.jp/projecteuler/index.php?Problem%2025フィボナッチ数列は以下の漸化式で定義される:, ただし . 最初の12項は以下である. 12番目の項, が3桁になる最初の項である.1000桁になる最…

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 …

Project Euler Problem 24

0,1,2,3,4,5,6,7,8,9からなる順列を辞書式に並べたときの100万番目はいくつか?http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2024順列とはモノの順番付きの並びのことである. たとえば, 3124は数 1, 2, 3, 4 の一つの順列である. …

Scala で Project Euler Problem 23

F# 版 を そのまま移植与えられた数を素因数分解して、素因数とその指数とを Map にして返す関数 scala> def get_prime_factor(map:collection.mutable.Map[Long, Long], n: Long, factor: Long = 2) { | if (n >= factor) { | if (n % factor != 0 ) | get_…

Project Euler Problem 23

2つの過剰数の和で書き表せない正の整数の総和を求めよhttp://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2023完全数とは, その数の真の約数の和がそれ自身と一致する数のことである. たとえば, 28の真の約数の和は, であるので, 28は…

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,>…

Project Euler Problem 23

2つの過剰数の和で書き表せない正の整数の総和を求めよhttp://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2023完全数とは, その数の真の約数の和がそれ自身と一致する数のことである. たとえば, 28の真の約数の和は, であるので, 28は…

Scala で Project Euler Problem 22

まづは、ファイルの読み込み scala> var source = scala.io.Source.fromFile("D:\\Euler\\022\\names.txt") source: scala.io.BufferedSource = non-empty iterator scala> val name_list = source.getLines.toList.flatMap(_.replaceAll("\"", "").split(',…

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…

Project Euler Problem 22

ファイル中の全名前のスコアの合計を求めよhttp://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%20225000個以上の名前が書かれている46Kのテキストファイル names.txt を用いる. まずアルファベット順にソートせよ.のち, 各名前について…

Scala で Project Euler Problem 21

scala> def get_prime_factor(map:collection.mutable.Map[Long, Long], n: Long, factor: Long = 2) { | if (n >= factor) { | if (n % factor != 0 ) | get_prime_factor(map, n, factor + 1) | else { | if (map.contains(factor)) | map(factor) += 1 | …

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,>…

Project Euler Problem 21

10000未満の友愛数の和を求めよhttp://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2021 を n の真の約数の和と定義する. (真の約数とは n 以外の約数のことである. ) もし, かつ (a ≠ b のとき) を満たすとき, a と b は友愛数(親和数)…

Scala で Project Euler Problem 20

scala> List.range(1, 100).product res0: Int = 0scala> List.range(BigInt(1), BigInt(100)).product <console>:6: error: type mismatch; found : scala.math.BigInt required: Int List.range(BigInt(1), BigInt(100)).product ^scala> List.range(BigInt(1), Big</console>…

F# で Project Euler Problem 20

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

Project Euler Problem 20

100! の各桁の数字の和を求めよhttp://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2020 を と表す.例えば, となる. この数の各桁の合計は である.では, の各桁の数字の和を求めよ.Factorial digit sumhttp://projecteuler.net/problem=…

Scala で Project Euler Problem 19

scala> def weekday(year:Int, month:Int, d:Int) = { | var y = year | var m = month | if (m == 1 || m == 2) { | y -= 1 | m += 12 | } | (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + d) % 7 | } weekday: (year: Int, month: Int, d: Int)Int…

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…

Project Euler Problem 19

20世紀中に月の初めが日曜日になるのは何回あるか?次の情報が与えられている. 1900年1月1日は月曜日である. 9月, 4月, 6月, 11月は30日まであり, 2月を除く他の月は31日まである. 2月は28日まであるが, うるう年のときは29日である. うるう年は西暦が4で割り…

Scala で Project Euler Problem 18

scala> var 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 1…

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 …

Project Euler Problem 18

三角形を頂点から下まで移動するときの最大の和を求めよ以下の三角形の頂点から下まで移動するとき, その数値の和の最大値は23になる.3 7 4 2 4 6 8 5 9 3この例では .以下の三角形を頂点から下まで移動するとき, その最大の和を求めよ.75 95 64 17 47 82 18…

Scala で Project Euler Problem 17

scala> val map = collection.mutable.Map[Int, String] () map: scala.collection.mutable.Map[Int,String] = Map() scala> scala> map += 1 -> "one" res0: map.type = Map(1 -> one) scala> map += 2 -> "two" res1: map.type = Map(1 -> one, 2 -> two) …

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,>…

Project Euler Problem 17

1から1000まですべて単語で書くのに必要な文字はいくつか?1 から 5 までの数字を英単語で書けば one, two, three, four, five であり, 全部で の文字が使われている.では 1 から 1000 (one thousand) までの数字をすべて英単語で書けば, 全部で何文字になる…

Scala で Project Euler Problem 16

scala> BigInt(2).pow(15) res11: scala.math.BigInt = 32768 scala> BigInt(2).pow(100) res12: scala.math.BigInt = 1267650600228229401496703205376scala> BigInt(2).pow(100).toString.toCharArray res14: Array[Char] = Array(1, 2, 6, 7, 6, 5, 0, 6,…

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…

Project Euler Problem 16

累乗の各桁の和 であり, これの各桁の和は となる.同様にして, の各桁の和を求めよ.http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2016Power digit sum and the sum of its digits is .What is the sum of the digits of the number…

Scala で Project Euler Problem 15

scala> (21 to 40).product / (1 to 20).product res0: Int = 0 scala> (BigInt(21) to 40).product / (BigInt(1) to 20).product res1: scala.math.BigInt = 137846528820