CalmoSoft primes: Difference between revisions

Content added Content deleted
(Removed Algol 68 stretch - it got the answer wrong ... : {)
(→‎{{header|Go}}: Updated in line with Wren entry of which it is a translation.)
Line 273: Line 273:
"fmt"
"fmt"
"rcu"
"rcu"
"strconv"
"time"
)
)


var start = time.Now()
func main() {

primes := rcu.Primes(100)
const max = 50_000_000
pc := len(primes)

var primes = rcu.Primes(max)

func calmoPrimes(limit int) (int, []int, []int, []int) {
var pc, sum int
if limit < max {
for i := 0; i < len(primes); i++ {
if primes[i] > limit {
pc = i
break
}
}
} else {
pc = len(primes)
}
for i := 0; i < pc; i++ {
sum += primes[i]
}
longest := 0
longest := 0
var sIndices, eIndices []int
var sIndices, eIndices, sums []int
for i := 0; i < pc; i++ {
for i := 0; i < pc; i++ {
if pc-i < longest {
break
}
if i > 0 {
sum -= primes[i-1]
}
sum2 := sum
for j := pc - 1; j >= i; j-- {
for j := pc - 1; j >= i; j-- {
temp := j - i + 1
temp := j - i + 1
Line 287: Line 312:
break
break
}
}
sum := rcu.SumInts(primes[i : j+1])
if j < pc-1 {
if rcu.IsPrime(sum) {
sum2 -= primes[j+1]
}
if rcu.IsPrime(sum2) {
if temp > longest {
if temp > longest {
longest = temp
longest = temp
sIndices = []int{i}
sIndices = []int{i}
eIndices = []int{j}
eIndices = []int{j}
sums = []int{sum2}
} else {
} else {
sIndices = append(sIndices, i)
sIndices = append(sIndices, i)
eIndices = append(eIndices, j)
eIndices = append(eIndices, j)
sums = append(sums, sum2)
}
}
break
break
Line 301: Line 330:
}
}
}
}
return longest, sIndices, eIndices, sums
fmt.Println("The longest sequence(s) of CalmoSoft primes having a length of", longest, "is/are:\n")
}
for i := 0; i < len(sIndices); i++ {

cp := primes[sIndices[i] : eIndices[i]+1]
func main() {
sum := rcu.SumInts(cp)
for _, limit := range []int{100, 250, 5000, 10000, 500000, 50000000} {
cps := ""
for i := 0; i < len(cp); i++ {
longest, sIndices, eIndices, sums := calmoPrimes(limit)
fmt.Println("For primes up to", rcu.Commatize(limit), "the longest sequence(s) of CalmoSoft primes")
cps += strconv.Itoa(cp[i])
fmt.Println("having a length of", rcu.Commatize(longest), "is/are:\n")
if i < len(cp)-1 {
cps += " + "
for i := 0; i < len(sIndices); i++ {
cp1 := primes[sIndices[i] : sIndices[i]+6]
cp2 := primes[eIndices[i]-5 : eIndices[i]+1]
cps := ""
for _, p := range cp1 {
cps += fmt.Sprintf("%d + ", p)
}
}
cps += ".. + "
for _, p := range cp2 {
cps += fmt.Sprintf("%d + ", p)
}
fmt.Printf("%s = %s\n", cps[:len(cps)-3], rcu.Commatize(sums[i]))
}
}
fmt.Println()
cps += " = " + strconv.Itoa(sum) + " which is prime"
fmt.Println(cps)
if i < len(sIndices)-1 {
fmt.Println()
}
}
}
fmt.Printf("Took %d ms\n", time.Since(start).Milliseconds())
}</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 + .. + 49999699 + 49999711 + 49999739 + 49999751 + 49999753 + 49999757 = 72,618,848,632,313


Took 270 ms
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
</pre>
</pre>