CalmoSoft primes: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Made more efficient and general, added stretch goal.)
Line 542: Line 542:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-math}}
{{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
<syntaxhighlight lang="ecmascript">import "./math" for Int, Nums
import "./fmt"for Fmt


var primes = Int.primeSieve(100)
var max = 50000000
var pc = primes.count
var primes = Int.primeSieve(max)

var longest = 0
var sIndices = []
var calmoPrimes = 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) {
for (j in pc-1..i) {
var longest = 0
var temp = j - i + 1
var sIndices = []
var eIndices = []
if (temp < longest) break
var sum = Nums.sum(primes[i..j])
var sums = []
if (Int.isPrime(sum)) {
for (i in 0...pc) {
if (temp > longest) {
if (pc - i < longest) break
longest = temp
sum = (i > 0) ? sum - primes[i-1] : sum
sIndices = [i]
var sum2 = sum
eIndices = [j]
for (j in pc-1..i) {
} else {
var temp = j - i + 1
sIndices.add(i)
if (temp < longest) break
eIndices.add(j)
sum2 = (j < pc -1) ? sum2 - primes[j+1] : sum2
if (Int.isPrime(sum2)) {
if (temp > longest) {
longest = temp
sIndices = [i]
eIndices = [j]
sums = [sum2]
} 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 (i in 0...sIndices.count) {
for (limit in [100, 250, 5000, 10000, 500000, 50000000]) {
var cp = primes[sIndices[i]..eIndices[i]]
var res = calmoPrimes.call(limit)
var sum = Nums.sum(cp)
var longest = 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()
Fmt.print("For primes up to $,d the longest sequence(s) of CalmoSoft primes", limit)
Fmt.print("having a length of $,d is/are:\n", longest)
for (i in 0...sIndices.count) {
var cp1 = primes[sIndices[i]..sIndices[i]+5]
var cp2 = primes[eIndices[i]-5..eIndices[i]]
var cps = cp1.join(" + ") + " + .. + " + cp2.join(" + ")
Fmt.print("$s = $,d", cps, sums[i])
}
System.print()
}</syntaxhighlight>
}</syntaxhighlight>


{{out}}
{{out}}
<pre>
<pre>
The longest sequence(s) of CalmoSoft primes having a length of 21 is/are:
For primes up to 100 the longest sequence(s) of CalmoSoft primes
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 + 31 + 37 + 41 + 43 + 47 + 53 + 59 + 61 + 67 + 71 + 73 + 79 + 83 + 89 = 953 which is prime
7 + 11 + 13 + 17 + 19 + 23 + .. + 49999699 + 49999711 + 49999739 + 49999751 + 49999753 + 49999757 = 72,618,848,632,313
</pre>
</pre>