How to resolve the algorithm Additive primes step by step in the uBasic/4tH programming language

Published on 12 May 2024 09:40 PM

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

Source code in the ubasic/4th programming language

print "Prime", "Digit Sum"
for i = 2 to 499
  if func(_isPrime(i)) then 
     s = func(_digSum(i)) 
     if func(_isPrime(s)) then
       print i, s
     endif
  endif
next
end

_isPrime
  param (1)
  local (1)

  if a@ < 2 then return (0)
  if a@ % 2 = 0 then return (a@ = 2)
  if a@ % 3 = 0 then return (a@ = 3)
  b@ = 5
  do while (b@ * b@) < (a@ + 1)
    if a@ % b@ = 0 then unloop : return (0)
    b@ = b@ + 2
  loop
return (1)
 
_digSum
  param (1)
  local (1)

  b@ = 0
  do while a@
    b@ = b@ + (a@ % 10)
    a@ = a@ / 10
  loop
return (b@)


  

You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the SETL programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the CLU programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Lasso programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the SETL programming language