Prime numbers p for which the sum of primes less than or equal to p is prime: Difference between revisions
(Added XPL0 example.) |
m (→{{header|REXX}}: fixed closing tag for <lang>) |
||
Line 105: | Line 105: | ||
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ |
||
if @.#<hi then sP= sP + @.# /*maybe add this prime to sum─of─primes*/ |
if @.#<hi then sP= sP + @.# /*maybe add this prime to sum─of─primes*/ |
||
end /*j*/; return</lang |
end /*j*/; return</lang> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
Revision as of 20:53, 7 July 2021
- Task
Find primes p which the sum of primes less or equal to p is prime, where p < 1,000.
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}
REXX
<lang rexx>/*REXX program finds primes in which sum of primes ≤ P is prime, where P < 1.000.*/ parse arg hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 1000 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ call genP /*build array of semaphores for primes.*/ w= 10 /*the width of a number in any column. */ title= ' primes which the sum of primes ≤ P is prime, where P < ' commas(hi) say ' index │' center(title, 1 + cols*(w+1) ) say '───────┼'center("" , 1 + cols*(w+1), '─') found= 0; idx = 1 /*number of primes found (so far); IDX.*/ $=; pSum= 0 /*#: list of primes (so far); init pSum*/
do j=1 for hi-1; p= @.j; pSum= pSum+p /*find summation primes within range. */ if \!.pSum then iterate /*Is sum─of─primes a prime? Then skip.*/ found= found + 1 /*bump the number of found primes. */ if cols<0 then iterate /*Build the list (to be shown later)? */ c= commas(p) /*maybe add commas to the number. */ $= $ right(c, max(w, length(c) ) ) /*add a found prime──►list, allow big #*/ if found//cols\==0 then iterate /*have we populated a line of output? */ say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ idx= idx + cols /*bump the index count for the output*/ end /*j*/
if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say '───────┴'center("" , 1 + cols*(w+1), '─') say say 'Found ' commas(found) title exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? /*──────────────────────────────────────────────────────────────────────────────────────*/ genP: !.= 0; sP= 0 /*prime semaphores; sP= sum of primes.*/
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11 /*define some low primes. */ !.2=1; !.3=1; !.5=1; !.7=1; !.11=1 /* " " " " flags. */ #=5; sq.#= @.# **2 /*number of primes so far; prime². */ /* [↓] generate more primes ≤ high.*/ do j=@.#+2 by 2 until @.#>=hi & @.#>sP /*find odd primes where P≥hi and P>sP.*/ parse var j -1 _; if _==5 then iterate /*J divisible by 5? (right dig)*/ if j// 3==0 then iterate /*" " " 3? */ if j// 7==0 then iterate /*" " " 7? */ do k=5 while sq.k<=j /* [↓] divide by the known odd primes.*/ if j // @.k == 0 then iterate j /*Is J ÷ X? Then not prime. ___ */ end /*k*/ /* [↑] only process numbers ≤ √ J */ #= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */ if @.#<hi then sP= sP + @.# /*maybe add this prime to sum─of─primes*/ end /*j*/; return</lang>
- output when using the default inputs:
index │ primes which the sum of primes ≤ P is prime, where P < 1,000 ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 2 3 7 13 37 43 281 311 503 541 11 │ 557 593 619 673 683 733 743 839 881 929 21 │ 953 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── Found 21 primes which the sum of primes ≤ P is prime, where P < 1,000
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.
XPL0
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number int N, I; [if N <= 1 then return false; for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true; ];
int Count, N, M, Sum; [Count:= 0; for N:= 2 to 1000-1 do
if IsPrime(N) then [Sum:= 0; for M:= 2 to N do if IsPrime(M) then Sum:= Sum + M; if IsPrime(Sum) then [IntOut(0, N); Count:= Count+1; if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\); ]; ];
CrLf(0); IntOut(0, Count); Text(0, " such numbers found below 1000. "); ]</lang>
- Output:
2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953 21 such numbers found below 1000.