-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve p7, p10 in C#; make primes generic
- Loading branch information
1 parent
0544747
commit 58ceee8
Showing
12 changed files
with
193 additions
and
34 deletions.
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
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
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
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,29 @@ | ||
/* | ||
Project Euler Problem 7 | ||
Problem: | ||
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that | ||
the 6th prime is 13. | ||
What is the 10 001st prime number? | ||
*/ | ||
using System; | ||
|
||
namespace Euler | ||
{ | ||
public class p0007 : IEuler | ||
{ | ||
public object Answer() | ||
{ | ||
int i = 0; | ||
foreach (long p in Prime.Primes<long>()) | ||
{ | ||
if (i == 10000) | ||
return (int)p; | ||
i++; | ||
} | ||
return -1; | ||
} | ||
} | ||
} |
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,21 @@ | ||
/* | ||
Project Euler Problem 10 | ||
Problem: | ||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. | ||
Find the sum of all the primes below two million. | ||
*/ | ||
using System; | ||
|
||
namespace Euler | ||
{ | ||
public class p0010 : IEuler | ||
{ | ||
public object Answer() | ||
{ | ||
return Enumerable.Sum(Prime.Primes<long>(2000000)); | ||
} | ||
} | ||
} |
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
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
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
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,39 @@ | ||
prime.cs | ||
======== | ||
|
||
View source code :source:`csharp/include/prime.cs` | ||
|
||
Includes | ||
-------- | ||
|
||
.. csharp:namespace:: Euler | ||
.. csharp:class:: Prime | ||
.. csharp:method:: static IEnumerable<T> Primes<T>(T? stop = null) | ||
This function implements the `Sieve of Eratosthenes <https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes>`_. In general, | ||
it will iterate over all of the prime numbers. You can also provide an optional ``stop`` argument, which will force | ||
it to not yield any numbers above that limit. Below is a gif (courtesy of Wikipedia) that demonstrates the principle | ||
of the sieve. | ||
|
||
.. image:: https://upload.wikimedia.org/wikipedia/commons/9/94/Animation_Sieve_of_Eratosth.gif | ||
:alt: Any animated example of the Sieve of Eratosthenes | ||
|
||
.. csharp:method:: static IEnumerable<T> PrimeFactors<T>(T n) | ||
This function will iterate over all the prime factors of a number, as well as ``-1`` and ``0`` if relevant. | ||
|
||
.. csharp:method:: static dynamic isComposite(long n) | ||
Returns ``0`` if the number is prime, and the smallest prime factor otherwise. | ||
|
||
.. csharp:method:: static bool isPrime(dynamic n) | ||
Returns ``true`` if the number is prime, and ``false`` otherwise. | ||
|
||
.. literalinclude:: ../../../../csharp/Euler/include/prime.cs | ||
:language: csharp | ||
:linenos: | ||
|
||
.. tags:: csharp-iterator, prime-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
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,18 @@ | ||
C# Implementation of Problem 7 | ||
============================== | ||
|
||
View source code :source:`csharp/Euler/p0007.cs` | ||
|
||
.. csharp:namespace:: Euler | ||
.. csharp:class:: p0007 | ||
.. csharp:inherits:: Euler.IEuler | ||
.. csharp:method:: object Answer() | ||
.. literalinclude:: ../../../csharp/Euler/p0007.cs | ||
:language: csharp | ||
:linenos: | ||
|
||
.. tags:: prime-number, csharp-iterator |
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,18 @@ | ||
C# Implementation of Problem 10 | ||
=============================== | ||
|
||
View source code :source:`csharp/Euler/p0010.cs` | ||
|
||
.. csharp:namespace:: Euler | ||
.. csharp:class:: p0010 | ||
.. csharp:inherits:: Euler.IEuler | ||
.. csharp:method:: object Answer() | ||
.. literalinclude:: ../../../csharp/Euler/p0010.cs | ||
:language: csharp | ||
:linenos: | ||
|
||
.. tags:: prime-number, csharp-iterator |