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

Project Euler 35

PE C#

Problem 35 1000000 までの素数を求める(エラトステネスのふるいで素数を求める) 求めたそれぞれの素数に対して、各桁の数値を回転させた数を作成し、素数かどうか調べる using System; using System.Collections.Generic; using System.Linq; class PE035 {…

Project Euler 34

C# PE

Problem 34 最大何桁まで求めたら良いのか。 [1] pry(main)> def f(n) n < 1 ? 1 : n*f(n-1) end => :f [2] pry(main)> (1..12).each { |i| puts "i:#{i} #{i*f(9)}" } i:1 362880 i:2 725760 i:3 1088640 i:4 1451520 i:5 1814400 i:6 2177280 i:7 2540160 …

Project Euler 33

C# PE

Problem 33 問題文を読んでもどうもよく理解できずすっきりしない。 xy/yz = xz となる x, y, z を求めている。 using System; class PE033 { static int Gcd(int m, int n) { int r = m % n; while (r != 0) { m = n; n = r; r = m % n; } return n; } stat…

構造体のソート

C#

List<T>.Sort メソッドで、リストに格納した構造体をソートします。 using System; using System.Collections.Generic; struct Lang { public string name; public string developer; public Lang(string name, string developer) { this.name = name; this.dev</t>…

Project Euler 32

C# PE

Problem 32 1 桁 x 4 桁、もしくは 2 桁 x 3 桁のいずれか。 1 桁 x 3 桁はありえない。9 * 999 = 8991 なので、最大でも 8 桁にしかならないから。 3 桁 x 3 桁はありえない。100 * 100 = 10000 なので、9 桁を超えてしまうから。 using System; using Syst…

Project Euler 31

C# PE

Problem 31 動的計画法の基本的な問題。 using System; class PE031 { static void Main() { int[] coins = { 1, 2, 5, 10, 20, 50, 100, 200 }; const int N = 200; int[] dp = new int[N+1]; dp[0] = 1; for (int i = 0; i < coins.Length; i++) { for (in…

構造体の練習

C#

using System; struct St { public int x; } class Cl { public int x; } class Test { static void Foo(St st, Cl cl) { st.x = cl.x = 2; } static void Bar(ref St st, Cl cl) { st.x = cl.x = 3; } static void Main() { var st = new St(); var cl = ne…

Project Euler 30

C# PE

Problem 30 上限値を考える 何桁まで調べればよいのだろうか。9 の 5 乗で調べてみる。 [11] pry(main)> (1..10).each { |i| puts "i:#{i} #{9**5 * i}" } i:1 59049 i:2 118098 i:3 177147 i:4 236196 i:5 295245 i:6 354294 i:7 413343 i:8 472392 i:9 531…

Stopwatch クラスで処理時間を計測する

C#

using System; using System.Diagnostics; // Stopwatch using System.Threading; // Thread class TestStopwatch { static void Main() { var sw = new Stopwatch(); sw.Start(); Thread.Sleep(3000); sw.Stop(); Console.WriteLine(sw.Elapsed); // 計測さ…