2014-08-01から1ヶ月間の記事一覧

Project Euler 41

C# PE

Problem 41 リストの要素を swap する関数は stackoverflow の以下を参考にしました。 c# - Swap two items in List<T> - Stack Overflow using System; using System.Collections.Generic; using System.Linq; class PE041 { static void Swap<T>(List<T> ls, int a,</t></t></t>…

Project Euler 40

PE C#

Problem 40 10 から 99 までの整数をつなげたとき、その桁数は 桁数:2 * (100 - 10) です。 100 から 999 までの整数をつながたとき、その桁数は 桁数:3 * (1000 - 100) です。 $ pry [3] pry(main)> x = 10 => 10 [4] pry(main)> x.to_s.length * (10*x - x…

Project Euler 39

C# PE

Problem 39 right angle triangle とは直角三角形のこと p <= 1000 なので、1 辺の長さは 500 まで調べる using System; using System.Collections.Generic; using System.Linq; class PE039 { static void Main() { const int N = 500; var freq = new Dict…

Array.ForEach メソッドに Console.WriteLine を渡すのが分からない → Action<int> で解決

Array.ForEach, List<T>.ForEach の練習。 Console.WriteLine を、引数には渡せるけれども、一時変数には代入できないのはなぜだろう。 using System; using System.Collections.Generic; using System.Linq; class TestForeach { static void Main() { int[] x</t>…

Project Euler 38

C# PE

Problem 38 n > 1 なので 100000 まで調べている。もっと値は小さくできそうだけど……。 using System; class PE038 { static int Calc(int n) { string s = ""; for (int i = 1; ; i++) { s += (n * i).ToString(); int[] freq = new int[10]; int m = 0; fo…

Project Euler 37

C# PE

Problem 37 using System; using System.Collections.Generic; using System.Linq; class PE037 { static List<int> MakePrimes(int n) { var primes = new List<int>(); primes.Add(2); // 2 is prime var table = new bool[n+1]; for (int i = 3; i <= n; i += 2) { </int></int>…

Project Euler 36

C# PE

Problem 36 2 進数の回文は、末尾が必ず 1 となるので、奇数のみ調べる。 using System; using System.Collections.Generic; using System.Linq; class PE036 { static string Bin(int n) { if (n < 0) throw new Exception("n < 0"); var ls = new List<char>(); </char>…