Summarize primes: Difference between revisions
Content added Content deleted
(Added Uiua solution) |
(Added C# example) |
||
Line 595: | Line 595: | ||
The sum of 162 primes in [2, 953] is 70241 which is also prime |
The sum of 162 primes in [2, 953] is 70241 which is also prime |
||
There are 21 summerized primes in [1, 1000)</pre> |
There are 21 summerized primes in [1, 1000)</pre> |
||
=={{header|C sharp|C#}}== |
|||
<syntaxhighlight lang="csharp"> |
|||
internal class Program |
|||
{ |
|||
private static void Main(string[] args) |
|||
{ |
|||
int sequenceCount = 0; |
|||
List<long> primes = new(); |
|||
long primeCandidate = 1; |
|||
Console.WriteLine(" N Prime Sum"); |
|||
while (primeCandidate < 1000) |
|||
{ |
|||
if (IsPrime(primeCandidate)) |
|||
{ |
|||
sequenceCount++; |
|||
primes.Add(primeCandidate); |
|||
long sequenceSum = primes.Sum(); |
|||
if (IsPrime(sequenceSum)) |
|||
{ |
|||
Console.WriteLine($"{sequenceCount,4} {primeCandidate,7} {sequenceSum,7}"); |
|||
} |
|||
} |
|||
primeCandidate++; |
|||
} |
|||
} |
|||
public static bool IsPrime(long number) |
|||
{ |
|||
if (number < 2) |
|||
{ |
|||
return false; |
|||
} |
|||
if (number % 2 == 0) |
|||
{ |
|||
return number == 2; |
|||
} |
|||
if (number % 3 == 0) |
|||
{ |
|||
return number == 3; |
|||
} |
|||
int delta = 2; |
|||
long k = 5; |
|||
while (k * k <= number) |
|||
{ |
|||
if (number % k == 0) |
|||
{ |
|||
return false; |
|||
} |
|||
k += delta; |
|||
delta = 6 - delta; |
|||
} |
|||
return true; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
N Prime Sum |
|||
1 2 2 |
|||
2 3 5 |
|||
4 7 17 |
|||
6 13 41 |
|||
12 37 197 |
|||
14 43 281 |
|||
60 281 7699 |
|||
64 311 8893 |
|||
96 503 22039 |
|||
100 541 24133 |
|||
102 557 25237 |
|||
108 593 28697 |
|||
114 619 32353 |
|||
122 673 37561 |
|||
124 683 38921 |
|||
130 733 43201 |
|||
132 743 44683 |
|||
146 839 55837 |
|||
152 881 61027 |
|||
158 929 66463 |
|||
162 953 70241 |
|||
</pre> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |