C# solutions to Project Euler problems.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
5.6 KiB

4 years ago
4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using static System.Console;
  4. using Xunit;
  5. namespace Euler {
  6. public class Program {
  7. [Fact]
  8. static long Problem1() {
  9. long sum = 0;
  10. for (long i = 1; i < 1000; i++) {
  11. if (i % 3 == 0 || i % 5 == 0) {
  12. sum += i;
  13. }
  14. }
  15. Assert.Equal(233168, sum);
  16. return sum;
  17. }
  18. [Fact]
  19. static long Problem2() {
  20. long max = 4_000_000;
  21. var fibs = new List<long>();
  22. fibs.Add(1);
  23. fibs.Add(2);
  24. while (fibs[fibs.Count - 1] < max) {
  25. long num = fibs[fibs.Count - 1] + fibs[fibs.Count - 2];
  26. fibs.Add(num);
  27. }
  28. long sum = 0;
  29. foreach (int i in fibs) {
  30. if (i % 2 == 0 && i <= max) {
  31. sum += i;
  32. }
  33. }
  34. Assert.Equal(4613732, sum);
  35. return sum;
  36. }
  37. static bool IsPrime(long num, List<long> primes) {
  38. foreach (long i in primes) {
  39. if (num % i == 0) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. static List<long> PrimesUpThrough(long num) {
  46. var primes = new List<long>();
  47. primes.Add(2);
  48. for (int i = 3; i <= num; i += 2) {
  49. if (IsPrime(i, primes)) {
  50. WriteLine(i);
  51. primes.Add(i);
  52. }
  53. }
  54. return primes;
  55. }
  56. static List<long> FirstNPrimes(long n) {
  57. var primes = new List<long>();
  58. primes.Add(2);
  59. for (int i = 3; ; i += 2) {
  60. if (IsPrime(i, primes)) {
  61. primes.Add(i);
  62. if (primes.Count == n) {
  63. return primes;
  64. }
  65. }
  66. }
  67. }
  68. [Fact]
  69. static long Problem3() {
  70. long target = 600_851_475_143;
  71. long targetSqrt = (long) Math.Sqrt(target);
  72. List<long> primes = PrimesUpThrough(targetSqrt);
  73. long highestPrimeFactor = 0;
  74. foreach (long i in primes) {
  75. if (target % i == 0) {
  76. highestPrimeFactor = i;
  77. }
  78. }
  79. Assert.Equal(6857, highestPrimeFactor);
  80. return highestPrimeFactor;
  81. }
  82. static bool IsPalindromicNumber(long l) {
  83. string s = "" + l;
  84. for (int i = 0; i < s.Length / 2; i++) {
  85. if (s[i] != s[s.Length - i - 1]) {
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. [Fact]
  92. static long Problem4() {
  93. long largest = 0;
  94. for (long i = 999; i >= 100; i--) {
  95. for (long j = 999; j >= 100; j--) {
  96. long target = i * j;
  97. if (target < largest) {
  98. continue;
  99. }
  100. if (IsPalindromicNumber(target)) {
  101. largest = target;
  102. }
  103. }
  104. }
  105. Assert.Equal(906609, largest);
  106. return largest;
  107. }
  108. [Fact]
  109. static long Problem5() {
  110. for (long test = 20; ; test += 20) {
  111. for (int i = 2; i <= 20; i++) {
  112. if (test % i != 0) {
  113. break;
  114. }
  115. if (i == 20) {
  116. Assert.Equal(232792560, test);
  117. return test;
  118. }
  119. }
  120. }
  121. }
  122. [Fact]
  123. static long Problem6() {
  124. long sum = 0;
  125. long sumSq = 0;
  126. for (long i = 1; i <= 100; i++) {
  127. sum += i;
  128. sumSq += i * i;
  129. }
  130. long result = sum * sum - sumSq;
  131. Assert.Equal(25164150, result);
  132. return result;
  133. }
  134. [Fact]
  135. static long Problem7() {
  136. List<long> primes = FirstNPrimes(10001);
  137. long result = primes[primes.Count - 1];
  138. Assert.Equal(104743, result);
  139. return result;
  140. }
  141. [Fact]
  142. static long Problem8() {
  143. string number =
  144. "73167176531330624919225119674426574742355349194934" +
  145. "96983520312774506326239578318016984801869478851843" +
  146. "85861560789112949495459501737958331952853208805511" +
  147. "12540698747158523863050715693290963295227443043557" +
  148. "66896648950445244523161731856403098711121722383113" +
  149. "62229893423380308135336276614282806444486645238749" +
  150. "30358907296290491560440772390713810515859307960866" +
  151. "70172427121883998797908792274921901699720888093776" +
  152. "65727333001053367881220235421809751254540594752243" +
  153. "52584907711670556013604839586446706324415722155397" +
  154. "53697817977846174064955149290862569321978468622482" +
  155. "83972241375657056057490261407972968652414535100474" +
  156. "82166370484403199890008895243450658541227588666881" +
  157. "16427171479924442928230863465674813919123162824586" +
  158. "17866458359124566529476545682848912883142607690042" +
  159. "24219022671055626321111109370544217506941658960408" +
  160. "07198403850962455444362981230987879927244284909188" +
  161. "84580156166097919133875499200524063689912560717606" +
  162. "05886116467109405077541002256983155200055935729725" +
  163. "71636269561882670428252483600823257530420752963450";
  164. int numDigits = 13;
  165. long maxProduct = 0;
  166. for (int i = 0; i < number.Length - numDigits; i++) {
  167. long product = 1;
  168. for (int j = i; j < i + numDigits; j++) {
  169. int digit = Int32.Parse(number.Substring(j, 1));
  170. product *= digit;
  171. }
  172. if (product > maxProduct) {
  173. maxProduct = product;
  174. }
  175. }
  176. Assert.Equal(23514624000, maxProduct);
  177. return maxProduct;
  178. }
  179. [Fact]
  180. static long Problem9() {
  181. for (int a = 1; a <= 333; a++) {
  182. for (int b = a + 1; b < 1000 - a; b++) {
  183. int c = 1000 - b - a;
  184. if (c < b) {
  185. break;
  186. }
  187. if (a * a + b * b == c * c) {
  188. int result = a * b * c;
  189. Assert.Equal(31875000, result);
  190. return result;
  191. }
  192. }
  193. }
  194. return 0;
  195. }
  196. [Fact]
  197. static long Problem10() {
  198. List<long> primes = PrimesUpThrough(2_000_000);
  199. long sum = 0;
  200. foreach (long prime in primes) {
  201. sum += prime;
  202. }
  203. Assert.Equal(142913828922, sum);
  204. return sum;
  205. }
  206. static void Main(string[] args) {
  207. WriteLine(Problem10());
  208. }
  209. }
  210. }