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.
|
|
using System; using System.Collections.Generic;
namespace Euler {
class Program {
static long Problem1() { long sum = 0; for (long i = 1; i < 1000; i++) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } return sum; }
static long Problem2() { long max = 4_000_000;
var fibs = new List<long>(); fibs.Add(1); fibs.Add(2);
while (fibs[fibs.Count - 1] < max) { long num = fibs[fibs.Count - 1] + fibs[fibs.Count - 2]; fibs.Add(num); }
long sum = 0; foreach (int i in fibs) { if (i % 2 == 0 && i <= max) { sum += i; } }
return sum; }
static bool IsPrime(long num, List<long> primes) { foreach (long i in primes) { if (num % i == 0) { return false; } } return true; }
static List<long> PrimesUpThrough(long num) { var primes = new List<long>(); primes.Add(2); for (int i = 3; i <= num; i += 2) { if (IsPrime(i, primes)) { primes.Add(i); } } return primes; }
static long Problem3() { long target = 600_851_475_143; long targetSqrt = (long) Math.Sqrt(target); List<long> primes = PrimesUpThrough(targetSqrt); long highestPrimeFactor = 0; foreach (long i in primes) { if (target % i == 0) { highestPrimeFactor = i; } } return highestPrimeFactor; }
static void Main(string[] args) { Console.WriteLine(Problem3()); } } }
|