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