How to resolve the algorithm Additive primes step by step in the langur programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Additive primes step by step in the langur programming language

Table of Contents

Problem Statement

In mathematics, additive primes are prime numbers for which the sum of their decimal digits are also primes.

Write a program to determine (and show here) all additive primes less than 500. Optionally, show the number of additive primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Additive primes step by step in the langur programming language

Source code in the langur programming language

val .isPrime = f .i == 2 or .i > 2 and not any f(.x) .i div .x, pseries 2 .. .i ^/ 2

val .sumDigits = f fold f{+}, s2n toString .i

writeln "Additive primes less than 500:"

var .count = 0

for .i in [2] ~ series(3..500, 2) {
    if .isPrime(.i) and .isPrime(.sumDigits(.i)) {
        write $"\.i:3;  "
        .count += 1
        if .count div 10: writeln()
    }
}

writeln $"\n\n\.count; additive primes found.\n"

  

You may also check:How to resolve the algorithm Conway's Game of Life step by step in the ABAP programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the GAP programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Determinant and permanent step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Wren programming language