Scala
非線形方程式の解法(2分法)を利用して2の平方根を求める 1. まず, 条件 を満たす点 を考えると, 関数 の解は, 区間 の中に存在する. 2. 次に, 区間 の中点 を考えると, であれば, 解は区間 の中に存在し, 同様に, であれば, 区間 の中に存在する. 3. この…
をラグランジュ補間で近似するn+1個の点 (x0, y0), (x1, y1) … (xn, yn) が与えられているとき, これらすべての点を通る n次式は次のように表すことができる. この式を使って, 与えられた点以外の点の値を求める. object Scala0701 { // データ点の数 - 1 va…
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(…
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_…
まづは、ファイルの読み込み 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(',…
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 | …
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>…
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…
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…
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) …
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,…
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
まづは、コラッツ数列がどんなものか見てみる scala> def collatz_list(n: Long): List[Long] = { | if (n == 1) List(1L) | else if (n % 2 == 0) n::collatz_list(n / 2) | else n::collatz_list(3 * n + 1) | } collatz_list: (n: Long)List[Long] scala>…
文字列を数値の List に 格納 scala> var str = """ | 37107287533902102798797998220837590246510135740250 | 46376937677490009712648124896970078050417018260538 | 74324986199524741059474233309513058123726617309629 | 91942213363574161572522430563…
28の約数を列挙してみる scala> var factor_list:List[Int] = List(1, 28) factor_list: List[Int] = List(1, 28) scala> for (i <- 2 to 28 / 2) { | if (28 % i == 0) { | factor_list = i::factor_list | println(i) | } | } 2 4 7 14 scala> factor_list…
まづ、格子状の文字列を List に 格納する scala> var 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 1…
Problem 7 を 流用 scala> var prime_list = List(3) prime_list: List[Int] = List(3) scala> var num = 5 num: Int = 5 scala> scala> while (num < 2000000) { | var num_sqrt = Math.sqrt(num) | var fPrime = true | scala.util.control.Breaks.breakab…
scala> (1 to 9) res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> (1 to 9). | map(a => (a + 1 to 10)) res1: scala.collection.immutable.IndexedSeq[scala.collection.immutable.Range.Inclusive] = Vector…
まづ、複数行文字列 scala> var str = """1234567890 | 1234567890 | 1234567890""" str: java.lang.String = 1234567890 1234567890 12345678901文字ずつ、取り出す scala> str.foreach { c => | print(c.toChar) | } 1234567890 1234567890 1234567890 sc…
素数を小さい方から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…
最初の10個の自然数について, その二乗の和は, 12 + 22 + ... + 102 = 385 最初の10個の自然数について, その和の二乗は, (1 + 2 + ... + 10)2 = 3025 これらの数の差は 3025 - 385 = 2640 となる. 同様にして, 最初の100個の自然数について二乗の和と和の二…
2520 は 1 から 10 の数字の全ての整数で割り切れる数字であり, そのような数字の中では最小の値である. では, 1 から 20 までの整数全てで割り切れる数字の中で最小の正の数はいくらになるか. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&pag…
前回のが、全然 Scala っぽくなかったので、再挑戦 scala> (1 to 9) res0: scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> scala> (1 to 9).map(i => (i to 9)) res1:…
左右どちらから読んでも同じ値になる数を回文数という. 2桁の数の積で表される回文数のうち, 最大のものは 9009 = 91 × 99 である. では, 3桁の数の積で表される回文数のうち最大のものを求めよ. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&p…
13195 の素因数は 5, 7, 13, 29 である. 600851475143 の素因数のうち最大のものを求めよ. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%203The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor o…
フィボナッチ数列の項は前の2つの項の和である. 最初の2項を 1, 2 とすれば, 最初の10項は以下の通りである. 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 数列の項の値が400万を超えない範囲で, 偶数値の項の総和を求めよ. http://odz.sakura.ne.jp/projecteule…
3か5の倍数になっている1000未満の自然数を足し合わせよ 10未満の自然数のうち, 3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり, これらの合計は 23 になる. 同じようにして, 1000 未満の 3 か 5 の倍数になっている数字の合計を求めよ. htt…
Problem 1 Multiples of 3 and 5 3と5の倍数 Problem 2 Even Fibonacci numbers 偶数のフィボナッチ数 Problem 3 Largest prime factor 最大の素因数 Problem 4 Largest palindrome product 最大の回文積 Problem 4その2 Problem 5 Smallest multiple 最小…
VBScript For Each arg In WScript.Arguments WScript.Echo arg Next For i = 0 To WScript.Arguments.Count - 1 WScript.Echo WScript.Arguments(i) Next JScript var arg = new Enumerator(WScript.Arguments); for (;!arg.atEnd(); arg.moveNext()) WScri…