Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
58
Task/Twin-primes/C-sharp/twin-primes-1.cs
Normal file
58
Task/Twin-primes/C-sharp/twin-primes-1.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using System;
|
||||
|
||||
class Program {
|
||||
|
||||
static uint[] res = new uint[10];
|
||||
static uint ri = 1, p = 10, count = 0;
|
||||
|
||||
static void TabulateTwinPrimes(uint bound) {
|
||||
if (bound < 5) return; count++;
|
||||
uint cl = (bound - 1) >> 1, i = 1, j,
|
||||
limit = (uint)(Math.Sqrt(bound) - 1) >> 1;
|
||||
var comp = new bool[cl]; bool lp;
|
||||
for (j = 3; j < cl; j += 3) comp[j] = true;
|
||||
while (i < limit) {
|
||||
if (lp = !comp[i]) {
|
||||
uint pr = (i << 1) + 3;
|
||||
for (j = (pr * pr - 2) >> 1; j < cl; j += pr)
|
||||
comp[j] = true;
|
||||
}
|
||||
if (!comp[++i]) {
|
||||
uint pr = (i << 1) + 3;
|
||||
if (lp) {
|
||||
if (pr > p) {
|
||||
res[ri++] = count;
|
||||
p *= 10;
|
||||
}
|
||||
count++;
|
||||
i++;
|
||||
}
|
||||
for (j = (pr * pr - 2) >> 1; j < cl; j += pr)
|
||||
comp[j] = true;
|
||||
}
|
||||
}
|
||||
cl--;
|
||||
while (i < cl) {
|
||||
lp = !comp[i++];
|
||||
if (!comp[i] && lp) {
|
||||
if ((i++ << 1) + 3 > p) {
|
||||
res[ri++] = count;
|
||||
p *= 10;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
res[ri] = count;
|
||||
}
|
||||
|
||||
static void Main(string[] args) {
|
||||
var sw = System.Diagnostics.Stopwatch.StartNew();
|
||||
string fmt = "{0,9:n0} twin primes below {1,-13:n0}";
|
||||
TabulateTwinPrimes(1_000_000_000);
|
||||
sw.Stop();
|
||||
p = 1;
|
||||
for (var j = 1; j <= ri; j++)
|
||||
Console.WriteLine(fmt, res[j], p *= 10);
|
||||
Console.Write("{0} sec", sw.Elapsed.TotalSeconds);
|
||||
}
|
||||
}
|
||||
63
Task/Twin-primes/C-sharp/twin-primes-2.cs
Normal file
63
Task/Twin-primes/C-sharp/twin-primes-2.cs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class TwinPrimes
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
CountTwinPrimes(Enumerable.Range(1, 9).Select(i => (int)Math.Pow(10, i)).ToArray());
|
||||
}
|
||||
|
||||
private static void CountTwinPrimes(params int[] bounds)
|
||||
{
|
||||
Array.Sort(bounds);
|
||||
int b = 0;
|
||||
int count = 0;
|
||||
string format = "There are {0:N0} twin primes below {1:N0}";
|
||||
foreach (var twin in FindTwinPrimes(bounds[^1])) {
|
||||
if (twin.p2 >= bounds[b]) {
|
||||
Console.WriteLine(format, count, bounds[b]);
|
||||
b++;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
Console.WriteLine(format, count, bounds[b]);
|
||||
}
|
||||
|
||||
private static IEnumerable<(int p1, int p2)> FindTwinPrimes(int bound) =>
|
||||
PrimeSieve(bound).Pairwise().Where(pair => pair.p1 + 2 == pair.p2);
|
||||
|
||||
private static IEnumerable<int> PrimeSieve(int bound)
|
||||
{
|
||||
if (bound < 2) yield break;
|
||||
yield return 2;
|
||||
|
||||
var composite = new BitArray((bound - 1) / 2);
|
||||
int limit = (int)(Math.Sqrt(bound) - 1) / 2;
|
||||
for (int i = 0; i < limit; i++) {
|
||||
if (composite[i]) continue;
|
||||
int prime = 2 * i + 3;
|
||||
yield return prime;
|
||||
for (int j = (prime * prime - 2) / 2; j < composite.Count; j += prime) {
|
||||
composite[j] = true;
|
||||
}
|
||||
}
|
||||
for (int i = limit; i < composite.Count; i++) {
|
||||
if (!composite[i]) yield return 2 * i + 3;
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<(T p1, T p2)> Pairwise<T>(this IEnumerable<T> source)
|
||||
{
|
||||
using var e = numbers.GetEnumerator();
|
||||
if (!e.MoveNext()) yield break;
|
||||
T p1 = e.Current;
|
||||
while (e.MoveNext()) {
|
||||
T p2 = e.Current;
|
||||
yield return (p1, p2);
|
||||
p1 = p2;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue