How to resolve the algorithm Honaker primes step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Honaker primes step by step in the 11l programming language

Table of Contents

Problem Statement

A Honaker prime is a prime whose digital sum is equal to the digital sum of its position in the sequence of primes.

If you look at the sequence of positive integer primes the first prime is 2 at position 1. The digital sums of 2 and 1 are not equal, so 2 is not a Honaker prime. The prime at position 32: 131 is a Honaker prime. The digital sum of 32 (5) is equal to the digital sum of 131 (5).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Honaker primes step by step in the 11l programming language

Source code in the 11l programming language

F primes_up_to_limit(Int limit)
   [Int] r
   I limit >= 2
      r.append(2)

   V isprime = [1B] * ((limit - 1) I/ 2)
   V sieveend = Int(sqrt(limit))
   L(i) 0 .< isprime.len
      I isprime[i]
         Int p = i * 2 + 3
         r.append(p)
         I i <= sieveend
            L(j) ((p * p - 3) >> 1 .< isprime.len).step(p)
               isprime[j] = 0B
   R r

F digitsum(num)
   ‘ Digit sum of an integer (base 10) ’
   R sum(String(num).map(c -> Int(c)))

F generate_honaker(limit = 5'000'000)
   ‘ Generate the sequence of Honaker primes with their sequence and primepi values ’
   V honaker = enumerate(primes_up_to_limit(limit)).filter((i, p) -> digitsum(p) == digitsum(i + 1)).map((i, p) -> (i + 1, p))
   R enumerate(honaker).map((hcount, pp) -> (hcount + 1, pp[0], pp[1]))

print(‘First 50 Honaker primes:’)
L(p) generate_honaker()
   I p[0] < 51
      print(f:‘{String(p):<16}’, end' I p[0] % 5 == 0 {"\n"} E ‘’)
   E I p[0] == 10'000
      print(f:"\nThe 10,000th Honaker prime is the {commatize(p[1])}th one, which is {commatize(p[2])}.")
      L.break

  

You may also check:How to resolve the algorithm Power set step by step in the Clojure programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the jq programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the OCaml programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Ruby programming language