Summarize primes: Difference between revisions

Added C# example
(Added C# example)
 
(5 intermediate revisions by 5 users not shown)
Line 595:
The sum of 162 primes in [2, 953] is 70241 which is also prime
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}}==
Line 662 ⟶ 752:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
procfunc prime x . rn .
forif in =mod 2 to= sqrt0 xand n > 2
if x mod i =return 0
r = 0.
i = break 23
while i <= sqrt n
if n mod i = 0
return 0
.
i += 2
.
r =return 1
.
for i = 2 to 999
callif prime i r= 1
if r = 1
ind += 1
sum += i
callif prime sum r= 1
if r = 1
print ind & ": " & sum
.
Line 959 ⟶ 1,051:
│162 953 70241│
└─────────────┘</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public final class SummarizePrimes {
 
public static void main(String[] args) {
final int start = 1;
final int finish = 1_000;
 
int sum = 0;
int count = 0;
int summarizedCount = 0;
 
for ( int p = start; p < finish; p++ ) {
if ( isPrime(p) ) {
count += 1;
sum += p;
if ( isPrime(sum) ) {
String word = ( count == 1 ) ? " prime" : " primes";
System.out.println(
"The sum of " + count + word + " in [2, " + p + "] is " + sum + ", which is also prime.");
summarizedCount++;
}
}
}
System.out.println(System.lineSeparator() +
"There are " + summarizedCount + " summarized primes in [" + start + ", " + finish + "].");
}
private static boolean isPrime(int number) {
if ( number < 2 ) {
return false;
}
if ( number % 2 == 0 ) {
return number == 2;
}
if ( number % 3 == 0 ) {
return number == 3;
}
 
int test = 5;
while ( test * test <= number ) {
if ( number % test == 0 ) {
return false;
}
test += 2;
if ( number % test == 0 ) {
return false;
}
test += 4;
}
return true;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
The sum of 1 prime in [2, 2] is 2, which is also prime.
The sum of 2 primes in [2, 3] is 5, which is also prime.
The sum of 4 primes in [2, 7] is 17, which is also prime.
The sum of 6 primes in [2, 13] is 41, which is also prime.
The sum of 12 primes in [2, 37] is 197, which is also prime.
The sum of 14 primes in [2, 43] is 281, which is also prime.
The sum of 60 primes in [2, 281] is 7699, which is also prime.
The sum of 64 primes in [2, 311] is 8893, which is also prime.
The sum of 96 primes in [2, 503] is 22039, which is also prime.
The sum of 100 primes in [2, 541] is 24133, which is also prime.
The sum of 102 primes in [2, 557] is 25237, which is also prime.
The sum of 108 primes in [2, 593] is 28697, which is also prime.
The sum of 114 primes in [2, 619] is 32353, which is also prime.
The sum of 122 primes in [2, 673] is 37561, which is also prime.
The sum of 124 primes in [2, 683] is 38921, which is also prime.
The sum of 130 primes in [2, 733] is 43201, which is also prime.
The sum of 132 primes in [2, 743] is 44683, which is also prime.
The sum of 146 primes in [2, 839] is 55837, which is also prime.
The sum of 152 primes in [2, 881] is 61027, which is also prime.
The sum of 158 primes in [2, 929] is 66463, which is also prime.
The sum of 162 primes in [2, 953] is 70241, which is also prime.
 
There are 21 summarized primes in [1, 1000].
</pre>
 
=={{header|jq}}==
Line 1,385 ⟶ 1,566:
158 -> 66463
162 -> 70241</pre>
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ number$
space 6 of swap join
-7 split nip echo$ ] is rjust ( n --> )
 
say " index prime sum" cr
say " ----- ----- ---" cr
[] 1000 times
[ i^ isprime if [ i^ join ] ]
0 swap
witheach
[ dup dip +
over isprime iff
[ i^ 1+ rjust
rjust
dup rjust
cr ]
else drop ]
drop
</syntaxhighlight>
 
{{out}}
 
<pre> index 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|Raku}}==
Line 1,822 ⟶ 2,053:
</pre>
 
=={{header|Uiua}}==
<syntaxhighlight lang="uiua">
Primes ← ◌⍢(⊃(▽±◿⊙.⊸⊢|⊂:⊢)|≠0⧻)↘2⇡⊙[]80000
IsPrime ← ∊:Primes
⊙⊟⊚⊸IsPrime\+.Primes # Raw results
⍉⇌⊂:+1⟜⍜(⊙⍉|⊏) # Formatted
</syntaxhighlight>
{{out}}
<pre>
╭─
╷ 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|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
var primes = Int.primeSieve(999)
9

edits