Sum of primes in odd positions is prime: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Factor)
No edit summary
Line 2: Line 2:


;Task:
;Task:
<br>Let '''p(n)''' be a sequence of prime numbers.
<br>Let '''p(i)''' be a sequence of prime numbers.
<br>Consider the '''p(1),p(3),p(5), ... ,p(i)''', for each '''p(i) < 1,000''' and '''i''' is odd.
<br>Consider the '''p(1),p(3),p(5), ... ,p(i)''', for each '''p(i) < 1,000''' and '''i''' is odd.
<br>Let '''sum''' be summarize of these primes.
<br>Let '''sum''' be summarize of these primes.

Revision as of 14:00, 1 September 2021

Sum of primes in odd positions is prime 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.
Task


Let p(i) be a sequence of prime numbers.
Consider the p(1),p(3),p(5), ... ,p(i), for each p(i) < 1,000 and i is odd.
Let sum be summarize of these primes.
If sum is prime then print p(i) and sum



Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: assocs assocs.extras kernel math.primes math.statistics prettyprint sequences.extras ;

1000 primes-upto <evens> dup cum-sum zip [ prime? ] filter-values .</lang>

Output:
{
    { 2 2 }
    { 5 7 }
    { 31 89 }
    { 103 659 }
    { 149 1181 }
    { 331 5021 }
    { 467 9923 }
    { 499 10909 }
    { 523 11941 }
    { 653 17959 }
    { 823 26879 }
}

Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "p" + " sum" + nl

nr = 0 sum = 0 limit = 1000

for n = 2 to limit

   if isprime(n)
      nr++
      if nr%2 = 1
         sum += n
         if isprime(sum)
            see "" + n + " " + sum + nl
         ok
      ok
   ok

next

see "done..." + nl </lang>

Output:
working...
p  sum
2 2
5 7
31 89
103 659
149 1181
331 5021
467 9923
499 10909
523 11941
653 17959
823 26879
done...