-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System; | ||
|
||
namespace Euler | ||
{ | ||
public static class Prime | ||
{ | ||
private static long lastCached = 0; | ||
private static List<long> cache = new(); | ||
|
||
public static IEnumerable<long> Primes(long? stop = null) | ||
{ | ||
if (stop is null) | ||
{ | ||
foreach (var p in cache) | ||
{ | ||
yield return p; | ||
} | ||
} | ||
else | ||
{ | ||
foreach (var p in cache) | ||
{ | ||
if (p < stop) | ||
yield return p; | ||
else | ||
break; | ||
} | ||
} | ||
if (stop is not null && lastCached > stop) | ||
yield break; | ||
foreach (var p in ModifiedEratosthenes()) | ||
{ | ||
if (p <= lastCached) | ||
continue; | ||
if (stop is not null && p > stop) | ||
break; | ||
cache.Add(p); | ||
lastCached = p; | ||
yield return p; | ||
} | ||
} | ||
|
||
private static IEnumerable<long> ModifiedEratosthenes() | ||
{ | ||
yield return 2; | ||
yield return 3; | ||
yield return 5; | ||
yield return 7; | ||
Dictionary<long, long> sieve = new(); | ||
var recurse = ModifiedEratosthenes().GetEnumerator(); | ||
recurse.MoveNext(); | ||
recurse.MoveNext(); | ||
long prime = recurse.Current; | ||
if (prime != 3) | ||
throw new Exception(); | ||
long primeSquared = prime * prime; | ||
long step = 2; | ||
for (long candidate = 9; ; candidate += 2) | ||
{ | ||
if (sieve.ContainsKey(candidate)) | ||
{ | ||
step = sieve[candidate]; | ||
sieve.Remove(candidate); | ||
} | ||
else if (candidate < primeSquared) | ||
{ | ||
yield return candidate; | ||
continue; | ||
} | ||
else | ||
{ | ||
step = prime * 2; | ||
recurse.MoveNext(); | ||
prime = recurse.Current; | ||
primeSquared = prime * prime; | ||
} | ||
long tc = candidate; | ||
do | ||
{ | ||
tc += step; | ||
} while (sieve.ContainsKey(tc)); | ||
sieve.Add(tc, step); | ||
} | ||
} | ||
|
||
public static IEnumerable<long> PrimeFactors(long n) | ||
{ | ||
if (n < 0) | ||
{ | ||
yield return -1; | ||
n = -n; | ||
} | ||
if (n == 0) | ||
{ | ||
yield return 0; | ||
} | ||
else | ||
{ | ||
long root = (long)Math.Ceiling(Math.Sqrt(n)); | ||
foreach (long factor in Primes()) | ||
{ | ||
long modulo = n % factor; | ||
if (modulo == 0) | ||
{ | ||
do | ||
{ | ||
yield return factor; | ||
n /= factor; | ||
modulo = n % factor; | ||
} while (modulo == 0); | ||
root = (long)Math.Ceiling(Math.Sqrt(n)); | ||
} | ||
if (n <= 1) | ||
break; | ||
if (factor > root) | ||
{ | ||
yield return n; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static long isComposite(long n) | ||
{ | ||
var factors = PrimeFactors(n).GetEnumerator(); | ||
factors.MoveNext(); | ||
long first = factors.current; | ||
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (2, ubuntu-latest)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (3, ubuntu-latest)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (5, ubuntu-latest)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (6, ubuntu-latest)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (7, ubuntu-latest)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (8, ubuntu-latest)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (9, ubuntu-latest)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (6, macos-latest)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (6, macos-13)
Check failure on line 128 in csharp/Euler/include/prime.cs GitHub Actions / csharp (6, windows-latest)
|
||
if (first == n) | ||
return 0; | ||
return first; | ||
} | ||
|
||
public static bool isPrime(long n) | ||
{ | ||
if (n < 2) | ||
return false; | ||
return isComposite(n) == 0; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
Project Euler Problem 3 | ||
More lazy functions this time. Took a little while to figure out how to | ||
eliminate the special case for 2. | ||
Problem: | ||
The prime factors of 13195 are 5, 7, 13 and 29. | ||
What is the largest prime factor of the number 600851475143 ? | ||
*/ | ||
using System; | ||
|
||
namespace Euler | ||
{ | ||
public class p0003 : IEuler | ||
{ | ||
public object Answer() | ||
{ | ||
return (short)Enumerable.Max(Prime.PrimeFactors(600851475143)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
prime.cs | ||
======== | ||
|
||
View source code :source:`csharp/include/prime.cs` | ||
|
||
.. csharp:namespace:: Euler | ||
.. csharp:class:: Prime | ||
.. csharp:method:: static IEnumerable<long> Primes(long? stop = null) | ||
Iterate over the prime numbers, optionally stopping before the specified value. Primes are cached between | ||
calls so we don't waste effort. | ||
|
||
.. csharp:method:: static IEnumerable<long> PrimeFactors(long n) | ||
Iterate over the prime factors of a number (but also ``-1`` and ``0`` in some cases). | ||
|
||
.. csharp:method:: static long isComposite(long n) | ||
Returns ``0`` if the given number is prime, and otherwise returns its smallest prime factor. | ||
|
||
.. csharp:method:: static bool isPrime(long n) | ||
Returns ``true`` if a number is prime, and ``false`` otherwise. | ||
|
||
.. literalinclude:: ../../../../csharp/Euler/include/prime.cs | ||
:language: csharp | ||
:linenos: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
C# Implementation of Problem 3 | ||
============================== | ||
|
||
View source code :source:`csharp/Euler/p0003.cs` | ||
|
||
.. csharp:namespace:: Euler | ||
.. csharp:class:: p0003 | ||
.. csharp:inherits:: Euler.IEuler | ||
.. csharp:method:: object Answer() | ||
.. literalinclude:: ../../../csharp/Euler/p0003.cs | ||
:language: csharp | ||
:linenos: | ||
|
||
.. tags:: fibonacci-number, divisibility |