Equal prime and composite sums: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 34:
=={{header|C++}}==
{{libheader|Primesieve}}
<
#include <chrono>
Line 96:
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</
{{out}}
Line 119:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// 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>
{{out}}
<pre>
Line 143:
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<
Dim As Integer i = 0
Line 175:
IndM += 1
End If
Loop</
{{out}}
<pre> sum prime sum composite sum
Line 191:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 244:
}
}
}</
{{out}}
Line 264:
Brute force seems fast enough for this task
<
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</
=={{header|jq}}==
Line 286:
The program given in this entry requires foreknowledge of the appropriate size of the (virtual) Eratosthenes sieve.
<
def task($sievesize):
Line 307:
;
task(1E5)</
{{out}}
<pre>
Line 320:
=={{header|Julia}}==
<
function getsequencematches(N, masksize = 1_000_000_000)
Line 347:
@time getsequencematches(11)
</
<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}}==
<
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]</
{{out}}
<pre>Sum Prime Index Composite Index
Line 391:
Not especially fast, but minimal memory usage.
{{libheader|ntheory}}
<
use warnings;
use feature <say state>;
Line 419:
$ci++;
redo
}</
{{out}}
<pre> 10 - 3rd prime sum, 2nd composite sum
Line 433:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{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"
my $prime-sum = [\+] (2..*).grep: *.is-prime;
Line 499:
++$c-index;
redo;
};</
{{out}}
<pre> 10 - 3ʳᵈ prime sum, 2ⁿᵈ composite sum 0.01 seconds
Line 514:
=={{header|Sidef}}==
<
var (
Line 541:
f(8).each_2d {|n, ci, pi|
printf("%12s = %-9s = %s\n", n, "P(#{pi})", "C(#{ci})")
}</
{{out}}
<pre>
Line 558:
=={{header|Wren}}==
Takes around 2 minutes, which is respectable for Wren, but uses a lot of memory.
<
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)
}
}</
{{out}}
Line 601:
=={{header|XPL0}}==
<
int N, I;
[if N <= 2 then return N = 2;
Line 641:
];
];
]</
{{out}}
|