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

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Summarize primes step by step in the Haskell 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 Haskell programming language

1. Importing Modules

import Data.List (scanl)
import Data.Numbers.Primes (isPrime, primes)

These lines import two modules: Data.List and Data.Numbers.Primes.

  • Data.List provides the scanl function, which is used for accumulating values while iterating over a list.
  • Data.Numbers.Primes provides the isPrime function to check if a number is prime and the primes function to generate a list of prime numbers.

2. indexedPrimeSums Function

The indexedPrimeSums function is the core of the code. It takes an infinite list of prime numbers (primes) and produces a list of tuples (i, p, n). Here's what each element in the tuple represents:

  • i: Index of the prime number
  • p: Prime number at the given index
  • n: Sum of all prime numbers up to and including the prime number at the index i

To generate this list, the function uses scanl, which takes a binary function, an initial value, and a list, and produces a new list by applying the binary function to each element of the list, starting with the initial value.

In this case, the binary function is defined as:

\(i, _, m) p -> (succ i, p, p + m)

It takes the current index i, the current prime number p, and the current accumulated sum m, and produces a new tuple:

  • succ i: Increment the index
  • p: The new prime number
  • p + m: Add the new prime number to the accumulated sum

The scanl function is applied to primes with an initial value of (0, 0, 0) (index 0, prime 0, accumulated sum 0).

3. Filtering with isPrime

The indexedPrimeSums function then filters out the tuples where the third element n is not a prime number using the filter function:

filter (\(_, _, n) -> isPrime n) $ ...

This ensures that only tuples where the sum of prime numbers is also a prime number are included in the final list.

4. main Function

The main function is the entry point of the program. It takes the first 1000 tuples from the indexedPrimeSums list and prints them using mapM_ print.

The takeWhile function is used to filter out tuples where the second element p exceeds 1000.

Output

When run, the program prints the first 1000 tuples of the form (i, p, n), where i is the index, p is a prime number, and n is the sum of all prime numbers up to and including p. For example, one of the outputs is:

(79,673,31607)

This means that the 79th prime number is 673, and the sum of the first 79 prime numbers is 31607.

Source code in the haskell programming language

import Data.List (scanl)
import Data.Numbers.Primes (isPrime, primes)

--------------- PRIME SUMS OF FIRST N PRIMES -------------

indexedPrimeSums :: [(Integer, Integer, Integer)]
indexedPrimeSums =
  filter (\(_, _, n) -> isPrime n) $
    scanl
      (\(i, _, m) p -> (succ i, p, p + m))
      (0, 0, 0)
      primes

--------------------------- TEST -------------------------
main :: IO ()
main =
  mapM_ print $
    takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums


  

You may also check:How to resolve the algorithm Quine step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the zkl programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the zkl programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Sidef programming language
You may also check:How to resolve the algorithm Null object step by step in the FreeBASIC programming language