How to resolve the algorithm Smarandache prime-digital sequence step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Smarandache prime-digital sequence step by step in the Julia programming language

Table of Contents

Problem Statement

The Smarandache prime-digital sequence (SPDS for brevity) is the sequence of primes whose digits are themselves prime. For example 257 is an element of this sequence because it is prime itself and its digits: 2, 5 and 7 are also prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Smarandache prime-digital sequence step by step in the Julia programming language

This code generates Smarandache primes, which are prime numbers that contain only the digits 2, 3, 5, and 7.

The code starts by defining a function, combodigits, which generates all possible combinations of digits from the set {2, 3, 5, 7} of a given length.

The getprimes function then generates Smarandache primes up to a given limit. It does this by iterating through all possible combinations of digits of length 1 to 9, and checking if the resulting number is prime. If it is, and it is not already in the list of Smarandache primes, it is added to the list.

The code then prints the first 25, 100th, and 10000th Smarandache primes.

Here is a step-by-step explanation of the code:

  1. The combodigits function generates all possible combinations of digits from the set {2, 3, 5, 7} of a given length. It does this by using the Combinatorics package to generate all possible combinations, and then removing any duplicates.

  2. The getprimes function generates Smarandache primes up to a given limit. It does this by iterating through all possible combinations of digits of length 1 to 9.

  3. For each combination of digits, the function converts it to a number and checks if it is prime. If it is, and it is not already in the list of Smarandache primes, it is added to the list.

  4. The code then prints the first 25, 100th, and 10000th Smarandache primes.

Source code in the julia programming language

using Combinatorics, Primes

combodigits(len) = sort!(unique(map(y -> join(y, ""), with_replacement_combinations("2357", len))))

function getprimes(N, maxdigits=9)
    ret = [2, 3, 5, 7]
    perms = Int[]
    for i in 1:maxdigits-1, combo in combodigits(i), perm in permutations(combo)
        n = parse(Int64, String(perm)) * 10
        push!(perms, n + 3, n + 7)
    end
        for perm in sort!(perms)
        if isprime(perm) && !(perm in ret)
            push!(ret, perm)
            if length(ret) >= N
                return ret
            end
        end
    end
end

const v = getprimes(10000)
println("The first 25 Smarandache primes are: ", v[1:25])
println("The 100th Smarandache prime is: ", v[100])
println("The 10000th Smarandache prime is: ", v[10000])


  

You may also check:How to resolve the algorithm Variable-length quantity step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Checkpoint synchronization step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the D programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the COBOL programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the PHP programming language