convert tabs to spaces & run dos2unix

This commit is contained in:
Colin McMillen 2020-11-18 17:58:59 -05:00
parent 2d24bc7a89
commit 1283c803b8

View File

@ -1,162 +1,162 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using static System.Console; using static System.Console;
using Xunit; using Xunit;
namespace Euler { namespace Euler {
public class Program { public class Program {
[Fact] [Fact]
static long Problem1() { static long Problem1() {
long sum = 0; long sum = 0;
for (long i = 1; i < 1000; i++) { for (long i = 1; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) { if (i % 3 == 0 || i % 5 == 0) {
sum += i; sum += i;
} }
} }
Assert.Equal(233168, sum); Assert.Equal(233168, sum);
return sum; return sum;
} }
[Fact] [Fact]
static long Problem2() { static long Problem2() {
long max = 4_000_000; long max = 4_000_000;
var fibs = new List<long>(); var fibs = new List<long>();
fibs.Add(1); fibs.Add(1);
fibs.Add(2); fibs.Add(2);
while (fibs[fibs.Count - 1] < max) { while (fibs[fibs.Count - 1] < max) {
long num = fibs[fibs.Count - 1] + fibs[fibs.Count - 2]; long num = fibs[fibs.Count - 1] + fibs[fibs.Count - 2];
fibs.Add(num); fibs.Add(num);
} }
long sum = 0; long sum = 0;
foreach (int i in fibs) { foreach (int i in fibs) {
if (i % 2 == 0 && i <= max) { if (i % 2 == 0 && i <= max) {
sum += i; sum += i;
} }
} }
Assert.Equal(4613732, sum); Assert.Equal(4613732, sum);
return sum; return sum;
} }
static bool IsPrime(long num, List<long> primes) { static bool IsPrime(long num, List<long> primes) {
foreach (long i in primes) { foreach (long i in primes) {
if (num % i == 0) { if (num % i == 0) {
return false; return false;
} }
} }
return true; return true;
} }
static List<long> PrimesUpThrough(long num) { static List<long> PrimesUpThrough(long num) {
var primes = new List<long>(); var primes = new List<long>();
primes.Add(2); primes.Add(2);
for (int i = 3; i <= num; i += 2) { for (int i = 3; i <= num; i += 2) {
if (IsPrime(i, primes)) { if (IsPrime(i, primes)) {
primes.Add(i); primes.Add(i);
} }
} }
return primes; return primes;
} }
static List<long> FirstNPrimes(long n) { static List<long> FirstNPrimes(long n) {
var primes = new List<long>(); var primes = new List<long>();
primes.Add(2); primes.Add(2);
for (int i = 3; ; i += 2) { for (int i = 3; ; i += 2) {
if (IsPrime(i, primes)) { if (IsPrime(i, primes)) {
primes.Add(i); primes.Add(i);
if (primes.Count == n) { if (primes.Count == n) {
return primes; return primes;
} }
} }
} }
} }
[Fact] [Fact]
static long Problem3() { static long Problem3() {
long target = 600_851_475_143; long target = 600_851_475_143;
long targetSqrt = (long) Math.Sqrt(target); long targetSqrt = (long) Math.Sqrt(target);
List<long> primes = PrimesUpThrough(targetSqrt); List<long> primes = PrimesUpThrough(targetSqrt);
long highestPrimeFactor = 0; long highestPrimeFactor = 0;
foreach (long i in primes) { foreach (long i in primes) {
if (target % i == 0) { if (target % i == 0) {
highestPrimeFactor = i; highestPrimeFactor = i;
} }
} }
Assert.Equal(6857, highestPrimeFactor); Assert.Equal(6857, highestPrimeFactor);
return highestPrimeFactor; return highestPrimeFactor;
} }
static bool IsPalindromicNumber(long l) { static bool IsPalindromicNumber(long l) {
string s = "" + l; string s = "" + l;
for (int i = 0; i < s.Length / 2; i++) { for (int i = 0; i < s.Length / 2; i++) {
if (s[i] != s[s.Length - i - 1]) { if (s[i] != s[s.Length - i - 1]) {
return false; return false;
} }
} }
return true; return true;
} }
[Fact] [Fact]
static long Problem4() { static long Problem4() {
long largest = 0; long largest = 0;
for (long i = 999; i >= 100; i--) { for (long i = 999; i >= 100; i--) {
for (long j = 999; j >= 100; j--) { for (long j = 999; j >= 100; j--) {
long target = i * j; long target = i * j;
if (target < largest) { if (target < largest) {
continue; continue;
} }
if (IsPalindromicNumber(target)) { if (IsPalindromicNumber(target)) {
largest = target; largest = target;
} }
} }
} }
Assert.Equal(906609, largest); Assert.Equal(906609, largest);
return largest; return largest;
} }
[Fact] [Fact]
static long Problem5() { static long Problem5() {
for (long test = 20; ; test += 20) { for (long test = 20; ; test += 20) {
for (int i = 2; i <= 20; i++) { for (int i = 2; i <= 20; i++) {
if (test % i != 0) { if (test % i != 0) {
break; break;
} }
if (i == 20) { if (i == 20) {
Assert.Equal(232792560, test); Assert.Equal(232792560, test);
return test; return test;
} }
} }
} }
} }
[Fact] [Fact]
static long Problem6() { static long Problem6() {
long sum = 0; long sum = 0;
long sumSq = 0; long sumSq = 0;
for (long i = 1; i <= 100; i++) { for (long i = 1; i <= 100; i++) {
sum += i; sum += i;
sumSq += i * i; sumSq += i * i;
} }
long result = sum * sum - sumSq; long result = sum * sum - sumSq;
Assert.Equal(25164150, result); Assert.Equal(25164150, result);
return result; return result;
} }
[Fact] [Fact]
static long Problem7() { static long Problem7() {
List<long> primes = FirstNPrimes(10001); List<long> primes = FirstNPrimes(10001);
long result = primes[primes.Count - 1]; long result = primes[primes.Count - 1];
Assert.Equal(104743, result); Assert.Equal(104743, result);
return result; return result;
} }
static void Main(string[] args) { static void Main(string[] args) {
WriteLine(Problem7()); WriteLine(Problem7());
} }
} }
} }