How to resolve the algorithm Summarize primes step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Summarize primes step by step in the J programming language

Table of Contents

Problem Statement

Considering in order of length, n, all sequences of consecutive primes, p, from 2 onwards, where p < 1000 and n>0, select those sequences whose sum is prime, and for these display the length of the sequence, the last item in the sequence, and the sum.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Summarize primes step by step in the J programming language

Source code in the j programming language

primes=: p: i. _1 p: 1000   NB. all prime numbers below 1000 
sums=: +/\ primes           NB. running sum of those primes 
mask=: 1 p: sums            NB. array of 0s, 1s where sums are primes 

NB. indices of prime sums (incremented for 1-based indexing)
NB. "copy" only the final primes in the prime sums
NB. "copy" only the sums which are prime 
results=: (>: I. mask) ,. (mask # primes) ,. (mask # sums)

NB. pretty-printed "boxed" output 
output=: 2 1 $ ' n prime sum ' ; < results


  

You may also check:How to resolve the algorithm Exceptions step by step in the REXX programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Oz programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Rust programming language
You may also check:How to resolve the algorithm Rep-string step by step in the PL/M programming language
You may also check:How to resolve the algorithm Array length step by step in the Sidef programming language