PE

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 15

PE

Problem 15 2 次元配列の練習。 20x20 なので 40 ステップで目的値に到達する。そのうち、水平移動は 20 ステップ行うので、40C20 を計算する方法でも良い。 『組み合わせ入門』p.2 using System; class PE015 { public static void Main() { const int N = …

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…

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

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