-
Notifications
You must be signed in to change notification settings - Fork 16
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
macauleym
wants to merge
6
commits into
kalisjoshua:master
Choose a base branch
from
macauleym:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+433
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e388ad3
Add Euler solutions in C# for the first eight.
macauleym 2664062
C# Update
macauleym 17306d6
C# Updated Formatting
macauleym 991d3c9
C# Euler 8 Update
macauleym 3b7bf0f
C# Euler 7 Update
macauleym 50449cd
Merge branch 'kalisjoshua:master' into master
macauleym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 👍