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 primesmax = Int.primeSieve(100)50000000
var pcprimes = primesInt.countprimeSieve(max)
 
var longest = 0
var sIndicescalmoPrimes = []Fn.new { |limit|
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) {
forvar (jlongest in= pc-1..i) {0
var tempsIndices = j - i + 1[]
var eIndices = []
if (temp < longest) break
var sumsums = Nums.sum(primes[i..j])
for (i in if (Int0.isPrime(sum)..pc) {
if (pc - i if (temp >< longest) {break
sum = (i > 0) ? sum - longestprimes[i-1] =: tempsum
var sIndicessum2 = [i]sum
for (j in pc-1..i) eIndices = [j]{
}var elsetemp {= j - i + 1
if (temp < sIndices.add(ilongest) break
sum2 = eIndices.add(j < pc -1) ? sum2 - primes[j+1] : sum2
if (Int.isPrime(sum2)) {
if (temp <> longest) break{
longest = temp
sIndices = [i]
eIndices = [j]
sums = [sum2]
break } else {
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 (ilimit in 0...sIndices.count[100, 250, 5000, 10000, 500000, 50000000]) {
var cpres = primes[sIndices[i]calmoPrimes..eIndices[i]]call(limit)
var sumlongest = Nums.sum(cp)res[0]
var sIndices = res[1]
var cps = cp.join(" + ") + " = " + sum.toString + " which is prime"
var eIndices = res[2]
System.print(cps)
var sums = res[3]
if (i < sIndices.count - 1) System.print()
System Fmt.print("TheFor primes up to $,d the longest sequence(s) of CalmoSoft primes", having a length of %(longest) is/are:\n"limit)
Fmt.print("having a length of $,d is/are:\n", longest)
for (i in 0...pcsIndices.count) {
var cp1 = primes[sIndices[i]..sIndices[i]+5]
var cp2 = primes[eIndices[i]-5..eIndices[i]]
var cps = cpcp1.join(" + ") + " = " + sum.toString. + " which+ iscp2.join(" + prime")
Fmt.print("$s = $,d", cps, sums[i])
}
System.print(cps)
}</syntaxhighlight>
 
{{out}}
<pre>
TheFor primes up to 100 the longest sequence(s) of CalmoSoft primes having a length of 21 is/are:
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 + 29.. + 3149999699 + 3749999711 + 4149999739 + 4349999751 + 4749999753 + 53 + 59 + 61 + 67 + 71 + 73 + 79 + 83 + 8949999757 = 953 which is prime72,618,848,632,313
</pre>
 
9,485

edits