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 |
var max = 50000000 |
||
var |
var primes = Int.primeSieve(max) |
||
var longest = 0 |
|||
var |
var calmoPrimes = Fn.new { |limit| |
||
var pc = (limit < max) ? primes.count { |p| p <= limit } : primes.count |
|||
⚫ | |||
var sum = (limit < max) ? Nums.sum(primes.take(pc)) : Nums.sum(primes) |
|||
⚫ | |||
var longest = 0 |
|||
var sIndices = [] |
|||
⚫ | |||
⚫ | |||
var sums = [] |
|||
for (i in 0...pc) { |
|||
if (pc - i < longest) break |
|||
sum = (i > 0) ? sum - primes[i-1] : sum |
|||
var sum2 = sum |
|||
for (j in pc-1..i) { |
|||
var temp = j - i + 1 |
|||
if (temp < longest) break |
|||
sum2 = (j < pc -1) ? sum2 - primes[j+1] : sum2 |
|||
if (Int.isPrime(sum2)) { |
|||
⚫ | |||
longest = temp |
|||
sIndices = [i] |
|||
eIndices = [j] |
|||
sums = [sum2] |
|||
⚫ | |||
sIndices.add(i) |
|||
eIndices.add(j) |
|||
sums.add(sum2) |
|||
} |
|||
break |
|||
} |
} |
||
⚫ | |||
} |
} |
||
} |
} |
||
return [longest, sIndices, eIndices, sums] |
|||
} |
} |
||
⚫ | |||
for ( |
for (limit in [100, 250, 5000, 10000, 500000, 50000000]) { |
||
var |
var res = calmoPrimes.call(limit) |
||
var |
var longest = res[0] |
||
var sIndices = res[1] |
|||
⚫ | |||
var eIndices = res[2] |
|||
⚫ | |||
var sums = res[3] |
|||
if (i < sIndices.count - 1) System.print() |
|||
⚫ | |||
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> |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
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 + |
7 + 11 + 13 + 17 + 19 + 23 + .. + 49999699 + 49999711 + 49999739 + 49999751 + 49999753 + 49999757 = 72,618,848,632,313 |
||
</pre> |
</pre> |
||