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