C#

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); …

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

C#

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

Project Euler 14

C# PE

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

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

タプル

C#

using System; using System.Collections.Generic; namespace Example { class MainClass { public static void Main(string[] args) { var t = Tuple.Create(1, 0.4f, "Hello"); Console.WriteLine(t.Item1); Console.WriteLine(t.Item2); Console.WriteLin…

string.Join() の練習

C#

Xamarin Studio を使いながら、C# の勉強をしています。 using System; using System.Collections.Generic; namespace Example { class MainClass { public static void Main(string[] args) { int[] xs = { 3, -23, 4, 32 }; List<int> ys = new List<int>() { 889, 2</int></int>…

素数を求める(配列を使う練習)

C#

エラトステネスのふるいで、素数を求めます。配列を使う練習。 using System; namespace Example { class MainClass { public static void sieve(int n) { bool[] primes = new bool[n+1]; for (int i = 2; i <= n; i++) primes[i] = true; for (int i = 2; …

Xamarin Studio を使ってみる

C#

前回のハローワールドのプログラムは、Emacs を使用してプログラムを書きました。 IDE のコード補完を使いたいので、Xamarin Studio を使ってみることにしました。 Xamarin Stuio を開いたら、メニューの File -> New -> Solution.. を選択。New Solution と…

こんにちはC#

C#

C# を勉強します。 % cat Hello.cs using System; class Hello { public static void Main() { Console.WriteLine("Hello C#"); } } コンパイルと実行結果です。 % dmcs Hello.cs % mono ./Hello.exe Hello C#