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.

230 lines
5.6 KiB

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