Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Euler solutions in C# for the first eight. #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions csharp/Euler/ProjectEuler001.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectEulerMain
{
class ProjectEuler001
{
/// <summary>
/// Find the sum of all multiples of 3 or 5 below 1000
/// </summary>
/// <param name="args"></param>
public static void Euler1(string[] args)
{
Stopwatch stopwatch = Stopwatch.StartNew();
var multiples = new BlockingCollection<int>();

Parallel.For(0, 1000, i =>
{
if (i % 3 == 0 || i % 5 == 0)
multiples.Add(i);
});

int sumOfMultiples = multiples.Sum();

stopwatch.Stop();
Console.WriteLine("The sum of all multipels of 3 or 5 below 1000 is {0}",
sumOfMultiples);
Console.WriteLine("The total time was {0} miliseconds",
stopwatch.ElapsedMilliseconds);
}
}
}
48 changes: 48 additions & 0 deletions csharp/Euler/ProjectEuler002.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace ProjectEulerMain
{
class ProjectEuler002
{
/// <summary>
/// Find the sum of all even numbers in a Fibionacci sequence, whose values do not exceed 4Million
/// </summary>
/// <param name="args"></param>
public static void Euler2(string[] args)
{
Stopwatch stopwatch = Stopwatch.StartNew();

Console.WriteLine("The sum of all even numbers in a fibonacci sequence (whose values do not exceed 4 Million) is {0}",
EvenFibionacciNumbers());

stopwatch.Stop();
Console.WriteLine("The total time was {0} miliseconds",
stopwatch.ElapsedMilliseconds);
}

private static int EvenFibionacciNumbers()
{
int a = 0,
b = 1;

var evens = new List<int>();

while(true)
{
int temp = a;
a = b;
b = temp + b;

if (b > 4000000)
break;
else if (b % 2 == 0)
evens.Add(b);
}

return evens.Sum();
}
}
}
38 changes: 38 additions & 0 deletions csharp/Euler/ProjectEuler003.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Diagnostics;

namespace ProjectEulerMain
{
class ProjectEuler003
{
/// <summary>
/// Find the largest prime factor of the number 600,851,475,143
/// </summary>
/// <returns></returns>
public static void Euler3(string[] args)
{
Stopwatch stopwatch = Stopwatch.StartNew();

Console.WriteLine("The largest prime factor of the number 600,851,475,143 is {0}",
MaxFactor());

stopwatch.Stop();
Console.WriteLine("The total time was {0} miliseconds",
stopwatch.ElapsedMilliseconds);
}

private static long MaxFactor()
{
long number = 600851475143,
k = 2;

while (k * k <= number)
if (number % k == 0)
number /= k;
else
k++;

return number;
}
}
}
35 changes: 35 additions & 0 deletions csharp/Euler/ProjectEuler004.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Diagnostics;
using System.Linq;

namespace ProjectEulerMain
{
class ProjectEuler004
{
/// <summary>
/// Find the largest pallindromic numbers from the prodct of two three digit numbers
/// </summary>
/// <param name="args"></param>
public static void Euler4(string[] args)
{
Stopwatch stopwatch = Stopwatch.StartNew();

var result = Enumerable.Range(100, 900).
SelectMany(x => Enumerable.Range(x, 1000 - x).
Select(y => x * y)).
Where(IsPallindrome).
Max();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of Linq Fluent syntax 👍


stopwatch.Stop();
Console.WriteLine("The result is: {0}.", result);
Console.WriteLine("The total time was {0} miliseconds",
stopwatch.ElapsedMilliseconds);
}

private static bool IsPallindrome(int number)
{
string s = number.ToString();
return s.Reverse().SequenceEqual(s);
}
}
}
67 changes: 67 additions & 0 deletions csharp/Euler/ProjectEuler005.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Diagnostics;
using System.Linq;

namespace ProjectEulerMain
{
class ProjectEuler005
{
/// <summary>
/// Find the smallest multiple that is divisable by all numbers from 1 to 20
/// </summary>
/// <param name="args"></param>
public static void Euler5(string[] args)
{
Stopwatch stopwatch = Stopwatch.StartNew();

int[] nums = Enumerable.Range(1, 20).ToArray();
int lcm = 1;

for (int i = 0; i < nums.Length; i++)
lcm = LCM(lcm, nums[i]);

stopwatch.Stop();
Console.WriteLine("The smallest multiple that is divisable by 1 through 20 is {0}", lcm);
Console.WriteLine("The total time was {0} miliseconds",
stopwatch.ElapsedMilliseconds);
}

private static int LCM(int value1, int value2)
{
int a = Math.Abs(value1);
int b = Math.Abs(value2);

a = checked((a / GCD(a, b)));

return checked((a * b));
}

private static int GCD(int value1, int value2)
{
int a = Math.Abs(value1);
int b = Math.Abs(value2);

int gcd = 1;

if(value1 == 0 || value2 == 0)
throw new ArgumentOutOfRangeException();

if (a == b)
return a;
if (a > b && a % b == 0)
return b;

if (b > a && b % a == 0)
return a;

while (b != 0)
{
gcd = b;
b = a % b;
a = gcd;
}

return gcd;
}
}
}
56 changes: 56 additions & 0 deletions csharp/Euler/ProjectEuler006.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectEulerMain
{
class ProjectEuler006
{
/// <summary>
/// Find the difference between the sum of squares, and the square of sums, of the first 100 natural numbers
/// </summary>
/// <param name="args"></param>
public static void Euler6(string[] args)
{
Stopwatch stopwatch = Stopwatch.StartNew();

long sumOfSquares = SumOfSquares(),
squareOfSums = SquareOfSums();

stopwatch.Stop();
Console.WriteLine("The difference between the sum of squares and the square of sums for the first 100 naural numbers is {0}.",
Math.Abs(sumOfSquares - squareOfSums));
Console.WriteLine("The total time was {0} miliseconds",
stopwatch.ElapsedMilliseconds);
}

private static long SumOfSquares()
{
var listOfSquares = new BlockingCollection<long>();
int j;

Parallel.For(1, 101, i =>
{
j = i * i;
listOfSquares.Add(j);
});

return listOfSquares.Sum();
}

private static long SquareOfSums()
{
var listOfSums = new BlockingCollection<long>();

Parallel.For(1, 101, i =>
{
listOfSums.Add(i);
});

return listOfSums.Sum() * listOfSums.Sum();
}
}
}
76 changes: 76 additions & 0 deletions csharp/Euler/ProjectEuler007.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectEulerMain
{
class ProjectEuler007
{
/// <summary>
/// Find the 10,001st prime number.
/// </summary>
/// <param name="args"></param>
public static void Euler7(string[] args)
{
Stopwatch stopwatch = Stopwatch.StartNew();

Console.WriteLine("The 10,001st prime number is {0}.",
FindThePrime());

stopwatch.Stop();
Console.WriteLine("The total time was {0} miliseconds",
stopwatch.ElapsedMilliseconds);
}

// Start with a close bounding value, to keep the
// sieve from taking longer than needed.
private const long limit = int.MaxValue / 16384;
private static PrimeGenerator generator = new PrimeGenerator(limit);

private static long FindThePrime()
{
return generator.GenNextPrime().Skip(10000).Take(1).First();
}

public class PrimeGenerator
{
private readonly long _limit;
private readonly bool[] _isPrime;

public PrimeGenerator(long limit)
{
_limit = limit;
_isPrime = new bool[_limit];

// Initialize the sieve to true.
for (var i = 0L; i < _limit; i++)
{
_isPrime[i] = true;
}
}

public IEnumerable<long> GenNextPrime()
{
for (var candidate = 2L; candidate < _limit; candidate++)
{
// Every true canditate is prime, because every multiple
// of the previous candidate is marked false.
if (!_isPrime[candidate])
continue;

yield return candidate;

// This section is parallellizable, but with a small sieve the performance
// with parallelization is less than the performance with a single thread.
for (var multiple = candidate * candidate; multiple < _limit; multiple += candidate)
{
_isPrime[multiple] = false;
}
}
}
}
}
}
Loading