CalmoSoft primes: Difference between revisions
→{{header|Wren}}: Made more efficient and general, added stretch goal.
(→{{header|Wren}}: Made more efficient and general, added stretch goal.) |
|||
Line 542:
=={{header|Wren}}==
{{libheader|Wren-math}}
This runs in about 4.3 seconds (cf. Julia 1.3 seconds) on my Core i7 machine. However, 2.6 seconds of that is needed to sieve for primes up to 50 million.
<syntaxhighlight lang="ecmascript">import "./math" for Int, Nums
import "./fmt"for Fmt
var
var
var
var pc = (limit < max) ? primes.count { |p| p <= limit } : primes.count
var eIndices = []▼
var sum = (limit < max) ? Nums.sum(primes.take(pc)) : Nums.sum(primes)
for (i in 0...pc) {▼
▲ var eIndices = []
if (temp < longest) break▼
for (i in
if (pc - i
sum = (i > 0) ? sum -
var
for (j in pc-1..i)
if (temp <
sum2 =
if (Int.isPrime(sum2)) {
longest = temp
sIndices = [i]
eIndices = [j]
sums = [sum2]
sIndices.add(i)
eIndices.add(j)
sums.add(sum2)
}
break
}
▲ break
}
}
return [longest, sIndices, eIndices, sums]
}
System.print("The longest sequence(s) of CalmoSoft primes having a length of %(longest) is/are:\n")▼
for (
var
var
var sIndices = res[1]
var cps = cp.join(" + ") + " = " + sum.toString + " which is prime"▼
var eIndices = res[2]
System.print(cps)▼
var sums = res[3]
▲
Fmt.print("having a length of $,d is/are:\n", longest)
var cp1 = primes[sIndices[i]..sIndices[i]+5]
var cp2 = primes[eIndices[i]-5..eIndices[i]]
Fmt.print("$s = $,d", cps, sums[i])
}
}</syntaxhighlight>
{{out}}
<pre>
having a length of 21 is/are:
7 + 11 + 13 + 17 + 19 + 23 + .. + 67 + 71 + 73 + 79 + 83 + 89 = 953
For primes up to 250 the longest sequence(s) of CalmoSoft primes
having a length of 49 is/are:
11 + 13 + 17 + 19 + 23 + 29 + .. + 223 + 227 + 229 + 233 + 239 + 241 = 5,813
For primes up to 5,000 the longest sequence(s) of CalmoSoft primes
having a length of 665 is/are:
7 + 11 + 13 + 17 + 19 + 23 + .. + 4957 + 4967 + 4969 + 4973 + 4987 + 4993 = 1,543,127
For primes up to 10,000 the longest sequence(s) of CalmoSoft primes
having a length of 1,223 is/are:
3 + 5 + 7 + 11 + 13 + 17 + .. + 9883 + 9887 + 9901 + 9907 + 9923 + 9929 = 5,686,633
7 + 11 + 13 + 17 + 19 + 23 + .. + 9901 + 9907 + 9923 + 9929 + 9931 + 9941 = 5,706,497
For primes up to 500,000 the longest sequence(s) of CalmoSoft primes
having a length of 41,530 is/are:
2 + 3 + 5 + 7 + 11 + 13 + .. + 499787 + 499801 + 499819 + 499853 + 499879 + 499883 = 9,910,236,647
For primes up to 50,000,000 the longest sequence(s) of CalmoSoft primes
having a length of 3,001,117 is/are:
7 + 11 + 13 + 17 + 19 + 23 +
</pre>
|