CalmoSoft primes: Difference between revisions

From Rosetta Code
Content added Content deleted
(Converted to draft task, clarified and tidied the task description and added a Wren solution.)
(Added Go)
Line 6: Line 6:
Find and show here the longest sequence of CalmoSoft primes.
Find and show here the longest sequence of CalmoSoft primes.
<br><br>
<br><br>
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main

import (
"fmt"
"rcu"
"strconv"
)

func main() {
primes := rcu.Primes(100)
pc := len(primes)
longest := 0
var sIndices, eIndices []int
for i := 0; i < pc; i++ {
for j := pc - 1; j >= i; j-- {
temp := j - i + 1
if temp < longest {
break
}
sum := rcu.SumInts(primes[i : j+1])
if rcu.IsPrime(sum) {
if temp > longest {
longest = temp
sIndices = []int{i}
eIndices = []int{j}
} else {
sIndices = append(sIndices, i)
eIndices = append(eIndices, j)
}
break
}
}
}
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]
sum := rcu.SumInts(cp)
cps := ""
for i := 0; i < len(cp); i++ {
cps += strconv.Itoa(cp[i])
if i < len(cp)-1 {
cps += " + "
}
}
cps += " = " + strconv.Itoa(sum) + " which is prime"
fmt.Println(cps)
if i < len(sIndices)-1 {
fmt.Println()
}
}
}</syntaxhighlight>

{{out}}
<pre>
The longest sequence(s) of CalmoSoft primes having a length of 21 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
</pre>

=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang="ring">
<syntaxhighlight lang="ring">

Revision as of 10:04, 7 April 2023

CalmoSoft primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

Let p(1), p(2), p(3), ... , p(n) be consecutive prime numbers, where p(n) < 100. If the sum of any consecutive sub-sequence of these numbers is a prime number, then the numbers in such a sub-sequence are called CalmoSoft primes.

Task

Find and show here the longest sequence of CalmoSoft primes.

Go

Translation of: Wren
Library: Go-rcu
package main

import (
    "fmt"
    "rcu"
    "strconv"
)

func main() {
    primes := rcu.Primes(100)
    pc := len(primes)
    longest := 0
    var sIndices, eIndices []int
    for i := 0; i < pc; i++ {
        for j := pc - 1; j >= i; j-- {
            temp := j - i + 1
            if temp < longest {
                break
            }
            sum := rcu.SumInts(primes[i : j+1])
            if rcu.IsPrime(sum) {
                if temp > longest {
                    longest = temp
                    sIndices = []int{i}
                    eIndices = []int{j}
                } else {
                    sIndices = append(sIndices, i)
                    eIndices = append(eIndices, j)
                }
                break
            }
        }
    }
    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]
        sum := rcu.SumInts(cp)
        cps := ""
        for i := 0; i < len(cp); i++ {
            cps += strconv.Itoa(cp[i])
            if i < len(cp)-1 {
                cps += " + "
            }
        }
        cps += " = " + strconv.Itoa(sum) + " which is prime"
        fmt.Println(cps)
        if i < len(sIndices)-1 {
            fmt.Println()
        }
    }
}
Output:
The longest sequence(s) of CalmoSoft primes having a length of 21 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

Ring

see "works..." + nl
limit = 100
Primes = []
OldPrimes = []
NewPrimes = []
for p = 1 to limit
    if isPrime(p)
       add(Primes,p)
    ok
next

lenPrimes = len(Primes)

for n = 1 to lenPrimes
    num = 0
    OldPrimes = []
    for m = n to lenPrimes  
        num = num + Primes[m]
        add(OldPrimes,Primes[m])
        if isPrime(num)
           if len(OldPrimes) > len(NewPrimes)
              NewPrimes = OldPrimes
           ok
        ok
    next
next

str = "["
for n = 1 to len(NewPrimes)
    if n = len(NewPrimes)
       str = str + newPrimes[n] + "]"
       exit
    ok
    str = str + newPrimes[n] + ", "
next

sum = 0
strsum = ""
for n = 1 to len(NewPrimes)
    sum = sum + newPrimes[n]
    if n = len(NewPrimes)
       strsum = strsum + newPrimes[n] + " = " + sum + " is prime number" 
       exit
    ok
    strsum = strsum + newPrimes[n] + " + "
next

see str + nl
see strsum + nl
see "The longest sequence of CalmoSoft primes = " + len(NewPrimes) + nl
see "done.." + nl

func isPrime num
     if (num <= 1) return 0 ok
     if (num % 2 = 0 and num != 2) return 0 ok
     for i = 3 to floor(num / 2) -1 step 2
         if (num % i = 0) return 0 ok
     next
     return 1
Output:
works...
[7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89]
7 + 11 + 13 + 17 + 19 + 23 + 29 + 31 + 37 + 41 + 43 + 47 + 53 + 59 + 61 + 67 + 71 + 73 + 79 + 83 + 89 = 953 is prime number
The longest sequence of CalmoSoft primes = 21
done..

Wren

Library: Wren-math
import "./math" for Int, Nums

var primes = Int.primeSieve(100)
var pc = primes.count
var longest = 0
var sIndices = []
var eIndices = []
for (i in 0...pc) {
    for (j in pc-1..i) {
        var temp = j - i + 1
        if (temp < longest) break
        var sum = Nums.sum(primes[i..j])        
        if (Int.isPrime(sum)) {
            if (temp > longest) {
                longest = temp
                sIndices = [i]
                eIndices = [j]
            } else {
                sIndices.add(i)
                eIndices.add(j)
            }
            break
        }
    }
}
System.print("The longest sequence(s) of CalmoSoft primes having a length of %(longest) is/are:\n")
for (i in 0...sIndices.count) {
    var cp = primes[sIndices[i]..eIndices[i]]
    var sum = Nums.sum(cp)
    var cps = cp.join(" + ") + " = " + sum.toString + " which is prime"
    System.print(cps)
    if (i < sIndices.count - 1) System.print()
}
Output:
The longest sequence(s) of CalmoSoft primes having a length of 21 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