Successive prime differences: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (Added Perl example) |
(Added C#) |
||
Line 30: | Line 30: | ||
:#https://www.primepuzzles.net/puzzles/puzz_011.htm |
:#https://www.primepuzzles.net/puzzles/puzz_011.htm |
||
:#https://matheplanet.de/matheplanet/nuke/html/viewtopic.php?topic=232720&start=0 |
:#https://matheplanet.de/matheplanet/nuke/html/viewtopic.php?topic=232720&start=0 |
||
=={{header|C sharp}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
using static System.Linq.Enumerable; |
|||
public static class SuccessivePrimeDifferences |
|||
{ |
|||
public static void Main() { |
|||
var primes = GeneratePrimes(1_000_000).ToList(); |
|||
foreach (var d in new[] { |
|||
new [] { 2 }, |
|||
new [] { 1 }, |
|||
new [] { 2, 2 }, |
|||
new [] { 2, 4 }, |
|||
new [] { 4, 2 }, |
|||
new [] { 6, 4, 2 }, |
|||
}) { |
|||
IEnumerable<int> first = null, last = null; |
|||
int count = 0; |
|||
foreach (var grp in FindDifferenceGroups(d)) { |
|||
if (first == null) first = grp; |
|||
last = grp; |
|||
count++; |
|||
} |
|||
Console.WriteLine($"{$"({string.Join(", ", first)})"}, {$"({string.Join(", ", last)})"}, {count}"); |
|||
} |
|||
IEnumerable<IEnumerable<int>> FindDifferenceGroups(int[] diffs) { |
|||
for (int pi = diffs.Length; pi < primes.Count; pi++) { |
|||
if (Range(0, diffs.Length).All(di => primes[pi-diffs.Length+di+1] - primes[pi-diffs.Length+di] == diffs[di])) { |
|||
yield return Range(pi - diffs.Length, diffs.Length + 1).Select(i => primes[i]); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
(3, 5), (999959, 999961), 8169 |
|||
(2, 3), (2, 3), 1 |
|||
(3, 5, 7), (3, 5, 7), 1 |
|||
(5, 7, 11), (999431, 999433, 999437), 1393 |
|||
(7, 11, 13), (997807, 997811, 997813), 1444 |
|||
(31, 37, 41, 43), (997141, 997147, 997151, 997153), 306 |
|||
</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99}} |
{{works with|Factor|0.99}} |