Equal prime and composite sums: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 34:
=={{header|C++}}==
{{libheader|Primesieve}}
<langsyntaxhighlight lang="cpp">#include <primesieve.hpp>
 
#include <chrono>
Line 96:
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</langsyntaxhighlight>
 
{{out}}
Line 119:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Equal prime and composite sums. Nigel Galloway: March 3rd., 2022
let fN(g:seq<int64>)=let g=(g|>Seq.scan(fun(_,n,i) g->(g,n+g,i+1))(0,0L,0)|>Seq.skip 1).GetEnumerator() in (fun()->g.MoveNext()|>ignore; g.Current)
let fG n g=let rec fG a b=seq{match a,b with ((_,p,_),(_,c,_)) when p<c->yield! fG(n()) b |((_,p,_),(_,c,_)) when p>c->yield! fG a (g()) |_->yield(a,b); yield! fG(n())(g())} in fG(n())(g())
fG(fN(primes64()))(fN(primes64()|>Seq.pairwise|>Seq.collect(fun(n,g)->[1L+n..g-1L])))|>Seq.take 11|>Seq.iter(fun((n,i,g),(e,_,l))->printfn $"Primes up to %d{n} at position %d{g} and composites up to %d{e} at position %d{l} sum to %d{i}.")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 143:
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
Dim As Integer i = 0
Line 175:
IndM += 1
End If
Loop</langsyntaxhighlight>
{{out}}
<pre> sum prime sum composite sum
Line 191:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 244:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 264:
Brute force seems fast enough for this task
 
<langsyntaxhighlight Jlang="j">Pn=: +/\ pn=: p: i.1e6 NB. first million primes pn and their running sum Pn
Cn=: +/\(4+i.{:pn)-.pn NB. running sum of composites starting at 4 and excluding those primes
both=: Pn(e.#[)Cn NB. numbers in both sequences
Line 276:
18859052 2142 5698
93952013 4555 12820
89171409882 118784 403340</langsyntaxhighlight>
 
=={{header|jq}}==
Line 286:
The program given in this entry requires foreknowledge of the appropriate size of the (virtual) Eratosthenes sieve.
 
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] +.;
 
def task($sievesize):
Line 307:
;
 
task(1E5)</langsyntaxhighlight>
{{out}}
<pre>
Line 320:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function getsequencematches(N, masksize = 1_000_000_000)
Line 347:
 
@time getsequencematches(11)
</langsyntaxhighlight>{{out}}
<pre>
Primes up to 5 at position 3 and composites up to 6 at position 2 sum to 10.
Line 364:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">$HistoryLength = 1;
ub = 10^8;
ps = Prime[Range[PrimePi[ub]]];
Line 374:
TableForm[MapThread[Prepend, {Flatten /@ poss, indices}],
TableHeadings -> {None, {"Sum", "Prime Index", "Composite Index"}},
TableAlignments -> Right]</langsyntaxhighlight>
{{out}}
<pre>Sum Prime Index Composite Index
Line 391:
Not especially fast, but minimal memory usage.
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say state>;
Line 419:
$ci++;
redo
}</langsyntaxhighlight>
{{out}}
<pre> 10 - 3rd prime sum, 2nd composite sum
Line 433:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
Line 465:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 484:
=={{header|Raku}}==
Let it run until I got bored and killed it. Time is total accumulated seconds since program start.
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers:ver<2.8.2+>;
 
my $prime-sum = [\+] (2..*).grep: *.is-prime;
Line 499:
++$c-index;
redo;
};</langsyntaxhighlight>
{{out}}
<pre> 10 - 3ʳᵈ prime sum, 2ⁿᵈ composite sum 0.01 seconds
Line 514:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func f(n) {
 
var (
Line 541:
f(8).each_2d {|n, ci, pi|
printf("%12s = %-9s = %s\n", n, "P(#{pi})", "C(#{ci})")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 558:
=={{header|Wren}}==
Takes around 2 minutes, which is respectable for Wren, but uses a lot of memory.
<langsyntaxhighlight lang="ecmascript">import "./math" for Int
import "./sort" for Find
import "/fmt" for Fmt
Line 583:
Fmt.print("$,21d - $,12r prime sum, $,12r composite sum", primeSums[i], i+1, ix+1)
}
}</langsyntaxhighlight>
 
{{out}}
Line 601:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 641:
];
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits