2014-01-01から1年間の記事一覧

Fizz-Buzz 問題 その2

Fizz-Buzz問題 - C#練習日記 の続きです。 無限シーケンスを作成する方法で Fizz-Buzz 問題を解いてみました。 参考: fizzbuzz - ヒューリズム http://chaton.practical-scheme.net/gauche/a/2012/08/09#entry-5023d040-7e774

Project Euler 22

PE C#

Problem 22 MatchCollection と Linq を連携させるには、Cast<Match>() が必要。 Linq のメソッドチェーンで改行するさい、インデントを深くすべきか考え中。 using System; using System.IO; using System.Text.RegularExpressions; using System.Linq; class PE02</match>…

Project Euler 21

C# PE

Problem 21 d(n) の計算は、素直に割り算している。 using System; class PE021 { static int SumProperDivisors(int n) { int sum = 0; for (int i = 1; i*2 <= n; i++) { if (n % i == 0) { sum += i; } } return sum; } static int Calc() { int ans = 0;…

Project Euler 20

C# PE

Problem 20 BigInteger を使う方法と、使わない方法の 2 通りで解いてみた。 using System; using System.Collections.Generic; using System.Numerics; using System.Linq; class PE020 { // BigInteger を使う方法 static int Calc1() { BigInteger x = 1;…

Project Euler 19

C# PE

Problem 19 using System; class PE019 { static void Main() { int ans = 0; var d = new DateTime(1901, 1, 1); var endDate = new DateTime(2000, 12, 31); while (d < endDate) { if (d.DayOfWeek == DayOfWeek.Sunday) { ans++; } d = d.AddMonths(1); …

Project Euler 18

Problem 18 DP の練習。 new 演算子で配列を生成すると、その要素は規定値で初期化される。 たとえば int の配列の場合は 0 で初期化される。 Linq 少しずつ慣れてきた。 map は Select filter は Where reduce は Aggregate int.Parse で文字列から整数へ変…

Project Euler 17

Problem 17 整数から英語表記への変換は、CLISP の format 関数を使いました。 ;;; Project Euler 17 ;;; $ clisp english.lisp ;;; (format nil "~R" 342) ;=> "three hundred and forty-two" (23 letters) ;;; (format nil "~R" 115) ;=> "one hundred and…

コマンドラインからのコンパイル、実行を楽するために rake を使ってみる。

C# プログラムを書いてから、コンパイル、実行するとき、以下のようにコマンドラインからコマンドを実行しています。 % cat test.cs using System; class Test { static void Main() { Console.WriteLine("Hello C#"); } } % mcs test.cs -out:a.exe % mono …

dmcs, gmcsは、mcsに-sdkオプションを指定している

Xamarinの基盤となっている「Mono」と、C#コンパイラ「mcs」 - Build Insiderより。 monoにはターゲットプロファイルに合わせて、mcs、gmcs、dmcs、smcsといったコンパイラツールが乱立していた。今はmcsのオプション-sdkで全て対応している。 dmcs は、-sdk…

Project Euler 16

Problem 16 累乗の計算には、BigInteger.Pow() スタティックメソッドを使う。 文字の数値への変換は、c - '0' とします。int.Parse(string) でもいいみたいです。 using System; using System.Numerics; class PE016 { public static void Main() { int sum …

多次元配列、ジャグ配列を foreach で回す

using System; public class TestArray { public static void Main() { int[,] xs = new int[,] { { 1, 2 }, { 3, 4 } }; foreach (int x in xs) { Console.Write("{0} ", x); } Console.WriteLine(); int[][] ys = new int[2][]; ys[0] = new int[] { 10, 2…

Project Euler 15

PE

Problem 15 2 次元配列の練習。 20x20 なので 40 ステップで目的値に到達する。そのうち、水平移動は 20 ステップ行うので、40C20 を計算する方法でも良い。 『組み合わせ入門』p.2 using System; class PE015 { public static void Main() { const int N = …

checked でオーバーフローをチェックする

C#

Problem 14 のコラッツの問題で計算がいっこうに終わらないと思ったら、int で計算していたところがオーバーフローして、無限ループになっていたのが原因だったようだ。 C# では checked でオーバーフローがチェックできる仕組みがあるようなので、試してみ…

Project Euler 14

C# PE

Problem 14 int で計算するとオーバーフローする。 メモ化をしたいけど、上限値がわからない。そういう場合は、ある一定値までメモ化する、というように決めてしまえばよい。この問題では N = 1000000 までとする。 ScalaでProject Euler(28) - 桃の天然水…

Project Euler 13

PE

Problem 13 System.Numerics.BigInteger を使う。 部分文字列は、Substring() メソッドを使う。 using System; using System.IO; using System.Numerics; public class PE013 { public static void Main() { var x = new BigInteger(0); foreach (string s i…

Project Euler 12

PE

Problem 12 約数の個数は、4 約数の個数 - Wikipedia のように求める。 Python の itertools.groupby が欲しい。素因数分解したあとのグループ化で使う。Enumerable.GroupBy でできるかな? using System; using System.IO; using System.Collections.Generic…

三角数を求める(コルーチンの練習)

三角数 - Wikipediaを求めるプログラム。C# のコルーチンの練習。 using System; using System.Collections.Generic; using System.Linq; public class TestTriangles { static IEnumerable<int> Triangles() { int x = 0; for (int i = 1; /* none */; i++) { x </int>…

Project Euler 11

C# PE

Problem 11 C# ポケットリファレンス を読みながら、ファイル読み込みや、文字列処理を書いています。 File.ReadAllText, File.ReadAllLinesで一括でテキストを読み込む String.Split で文字列を分割 int best = 0 を var best = 0 と書いたら内部ではどう扱…

Project Euler 10

PE C#

Problem 10 配列の練習 答えは int だとオーバーフローする using System; using System.IO; using System.Collections.Generic; public class P010 { static List<int> sieve(int n) { bool[] xs = new bool[n+1]; for (int i = 0; i <= n; i++) { xs[i] = true;</int>…

Project Euler 9

PE

Problem 9 a + b + c = 1000 だから、2 重ループで a, b の値を決めて、残りの c は、c = 1000 - (a + b) で求めます。 using System; public class P009 { public static void Main() { for (int i = 0; i < 1000; i++) { for (int j = i+1; j < 1000; j++)…