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

Published on 12 May 2024 09:40 PM

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

Source code in the yabasic programming language

// Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
// by Galileo, 06/2022

limit = 500

dim flags(limit)

for i = 2 to  limit
    for k = i*i to limit step i 
        flags(k) = 1
    next
    if flags(i) = 0 primes$ = primes$ + str$(i) + " "
next

dim prim$(1)

n = token(primes$, prim$())

for i = 1 to n
    sum = 0
    num$ = prim$(i)
    for j = 1 to len(num$)
        sum = sum + val(mid$(num$, j, 1))
    next
    if instr(primes$, str$(sum) + " ") print prim$(i), " "; : count = count + 1
next

print "\nFound: ", count

  

You may also check:How to resolve the algorithm Tree traversal step by step in the Miranda programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the ALGOL 60 programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the haskell programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the PostScript programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the AWK programming language