Prime numbers p for which the sum of primes less than or equal to p is prime: Difference between revisions
(Added Wren) |
(Added Go) |
||
Line 4: | Line 4: | ||
<br><br> |
<br><br> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"rcu" |
|||
) |
|||
func main() { |
|||
primes := rcu.Primes(1000) |
|||
maxSum := 0 |
|||
for _, p := range primes { |
|||
maxSum += p |
|||
} |
|||
c := rcu.PrimeSieve(maxSum, true) |
|||
primeSum := 0 |
|||
var results []int |
|||
for _, p := range primes { |
|||
primeSum += p |
|||
if !c[primeSum] { |
|||
results = append(results, p) |
|||
} |
|||
} |
|||
fmt.Println("Primes 'p' under 1000 where the sum of all primes <= p is also prime:") |
|||
for i, p := range results { |
|||
fmt.Printf("%4d ", p) |
|||
if (i+1)%7 == 0 { |
|||
fmt.Println() |
|||
} |
|||
} |
|||
fmt.Println("\nFound", len(results), "such primes") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Primes 'p' under 1000 where the sum of all primes <= p is also prime: |
|||
2 3 7 13 37 43 281 |
|||
311 503 541 557 593 619 673 |
|||
683 733 743 839 881 929 953 |
|||
Found 21 such primes |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
Revision as of 18:33, 7 July 2021
- Task
- Find prime numbers p which sum of prime numbers less or equal to p is prime, where p < 1000
Go
<lang go>package main
import (
"fmt" "rcu"
)
func main() {
primes := rcu.Primes(1000) maxSum := 0 for _, p := range primes { maxSum += p } c := rcu.PrimeSieve(maxSum, true) primeSum := 0 var results []int for _, p := range primes { primeSum += p if !c[primeSum] { results = append(results, p) } } fmt.Println("Primes 'p' under 1000 where the sum of all primes <= p is also prime:") for i, p := range results { fmt.Printf("%4d ", p) if (i+1)%7 == 0 { fmt.Println() } } fmt.Println("\nFound", len(results), "such primes")
}</lang>
- Output:
Primes 'p' under 1000 where the sum of all primes <= p is also prime: 2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953 Found 21 such primes
Phix
function sump(integer p, i, sequence s) return is_prime(sum(s[1..i])) end function sequence res = filter(get_primes_le(1000),sump) printf(1,"%d found: %V\n",{length(res),res})
- Output:
21 found: {2,3,7,13,37,43,281,311,503,541,557,593,619,673,683,733,743,839,881,929,953}
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Prime numbers p which sum of prime numbers less or equal to p is prime:" + nl
row = 0 sum = 0 limit = 1000
for n = 1 to limit
if isprime(n) sum = sum + n if isprime(sum) see "" + n + " " row = row + 1 if row%5 = 0 see nl ok ok ok
next
see nl + "Found " + row + " numbers" + nl see "done..." + nl </lang>
- Output:
working... Prime numbers p which sum of prime numbers less or equal to p is prime: 2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953 Found 21 numbers done...
Wren
<lang ecmascript>import "/math" for Int, Nums import "/seq" for Lst import "/fmt" for Fmt
var primes = Int.primeSieve(1000, true) var maxSum = Nums.sum(primes) var c = Int.primeSieve(maxSum, false) var primeSum = 0 var results = [] for (p in primes) {
primeSum = primeSum + p if (!c[primeSum]) results.add(p)
} System.print("Primes 'p' under 1000 where the sum of all primes <= p is also prime:") for (chunk in Lst.chunks(results, 7)) Fmt.print("$4d", chunk) System.print("\nFound %(results.count) such primes.")</lang>
- Output:
Primes 'p' under 1000 where the sum of all primes <= p is also prime: 2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953 Found 21 such primes.