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

Project Euler 29

C# PE

Problem 29 using System; using System.Collections.Generic; using System.Numerics; class PE029 { static void Main() { var st = new HashSet<BigInteger>(); for (int i = 2; i <= 100; i++) { BigInteger a = i; for (int j = 2; j <= 100; j++) { st.Add(BigInte</biginteger>…

Project Euler 28

C# PE

Problem 28 using System; class PE028 { static int Calc(int size) { // N x N サイズのとき、4 隅の一番大きな数字は N^2 // 4 隅の数字のうち、一番大きな数字を除いた残りの数字は // N^2 - (n - 1) * 1 // N^2 - (n - 1) * 2 // N^2 - (n - 1) * 3 // …

Project Euler 27

C# PE

Problem 27 using System; class PE027 { static bool IsPrime(int n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (int i = 3; i*i <= n; i += 2) { if (n % i == 0) return false; } return true; } static …

Linq メソッド参考URL

C#

Linq メソッド一覧は、以下の記事が参考になります。感謝です。 LINQの拡張メソッド一覧と、ほぼ全部のサンプルを作ってみました。 - 地平線に行く

Project Euler 26

PE C#

Problem 26 素直に割っていき、余りがゼロになるかまたはサイクルするまで繰り返す。 using System; using System.Collections.Generic; using System.Linq; class PE026 { static List<int> CalcRecurringCycle(int d) { var divs = new List<int>(); var mods = new </int></int>…

Project Euler 25

C# PE

Problem 25 using System; using System.Numerics; class PE025 { static void Main() { var a = new BigInteger(1); var b = new BigInteger(1); int nth = 2; while (b.ToString().Length < 1000) { var t = a + b; a = b; b = t; nth++; } Console.WriteL…

Project Euler 24

C# PE

Problem 24 using System; using System.Collections.Generic; using System.Linq; class PE024 { static long Fact(long n) { if (n == 0) return 1; else return n * Fact(n-1); } static string Calc(long n) { var ans = new List<int>(); var digits = Enume</int>…

Project Euler 23

C# PE

Problem 23 Wikipedia で、完全数、不足数、過剰数について調べる。 完全数(perfect number) その数自身を除く約数の和が、その数自身と等しい自然数のこと。 例えば 6 (= 1 + 2 + 3)、28 (= 1 + 2 + 4 + 7 + 14) や496が完全数。 不足数(deficient number) …

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