public static void Problem5() { int n = 1; while (true) { if (CustomMath.IsDivisibleForEachN(n, 20)) { break; } n++; } Console.WriteLine(n); //returns smallest number which can be divided by every natural number up to 20 }
public static void Problem10() /*The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. * Find the sum of all the primes below two million.*/ { int n = 2; long sum = 0; while (n < 2_000_000) { if (CustomMath.IsPrime(n)) { sum += n; } n++; } Console.WriteLine(sum); }
public static void Problem3() { const long NUMBER = 600851475143; int max = 0; foreach (var c in CustomMath.Factors(NUMBER)) //CustomMath methods are in another file { if (CustomMath.IsPrime(c)) { if (c > max) { max = c; } } } Console.WriteLine(max); }
public static void Problem7() { int n = 2; const int N = 10001; int ctr = 0; while (ctr < N) { if (CustomMath.IsPrime(n)) { ctr++; } n++; } n--; Console.WriteLine(n); }
public static void Problem4() { int largestPalindrome = 0; for (int i = 999; i > 99; i--) { for (int j = 999; j > 99; j--) { if (CustomMath.IsPalindrome(i * j)) { if (i * j > largestPalindrome) { largestPalindrome = i * j; } else { break; } } } } Console.WriteLine(largestPalindrome); }