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

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

Project Euler 8

PE

Problem 8 char.IsDigit(c) で文字 c が数値かどうか判定 文字 c から数値への変換は、c - '0' で出来た using System; using System.IO; using System.Collections.Generic; namespace PE { public class PE008 { public static void Calc() { string text …

Project Euler 7

Problem 7 using System; using System.Collections.Generic; namespace PE { public class PE007 { public static void Calc() { var primes = new List<int>(); for (int i = 3; /* none */; i += 2) { bool isprime = true; foreach (int x in primes) { if (x</int>…

Project Euler 6

Problem 6 using System; namespace PE { public class PE006 { public static void Calc() { int sumsq = 0; for (int i = 1; i <= 100; i++) { sumsq += i * i; } int sqsum = (int)Math.Pow(100 * 101 / 2, 2); Console.WriteLine(sqsum - sumsq); } } }

Project Euler 5

PE

Problem 5 and と i のいずれでも割り切れる数は不要なので gcd で取り除く。lcm で求める方法もある。 using System; namespace PE { public class PE005 { public static void Calc() { int ans = 1; for (int i = 1; i <= 20; i++) { ans = ans / Util.Gc…

Project Euler 4

PE

Problem 4 using System; namespace PE { public class PE004 { public static bool IsPalindrome(int n) { string s = n.ToString(); for (int i = 0; i < s.Length/2; i++) { if (s[i] != s[s.Length - i - 1]) { return false; } } return true; } public…

Project Euler 3

PE

Problem 3 using System; using System.Collections.Generic; namespace PE { public class PE003 { public static void Calc() { long n = 600851475143; List<long> factors = new List<long>(); long x = 3; while (x*x <= n) { if (n % x == 0) { factors.Add(x); n </long></long>…

Project Euler 2

PE

Problem 2 using System; namespace PE { public class PE002 { public static void Calc() { const int n = 4000000; int a = 1; int b = 2; int sum = 2; while (b <= n) { int tmp = a + b; a = b; b = tmp; if (tmp % 2 == 0) sum += tmp; } Console.Wri…

varに戸惑う

C# の var の使いどころに戸惑っている。 これまで書いたことのある言語は、C++, Java, Python で、C# の var のような型推論は初めての体験。 練習のためにどう書くの問題を解いてみた。 第13回オフラインリアルタイムどう書くの参考問題。C#で解く。 - Qii…

Project Euler 1

PE

Problem 1 using System; namespace PE { public class PE001 { public static void Calc() { int sum = 0; for (int i = 3; i < 1000; i++) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } Console.WriteLine(sum); } } }