How to resolve the algorithm Multi-base primes step by step in the Sidef programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Multi-base primes step by step in the Sidef programming language

Table of Contents

Problem Statement

Prime numbers are prime no matter what base they are represented in. A prime number in a base other than 10 may not look prime at first glance. For instance: 19 base 10 is 25 in base 7.

Several different prime numbers may be expressed as the "same" string when converted to a different base.

Restricted to bases 2 through 36; find the strings that have the most different bases that evaluate to that string when converting prime numbers to a base. Find the conversion string, the amount of bases that evaluate a prime to that string and the enumeration of bases that evaluate a prime to that string. Display here, on this page, the string, the count and the list for all of the: 1 character, 2 character, 3 character, and 4 character strings that have the maximum base count that evaluate to that string. Should be no surprise, the string '2' has the largest base count for single character strings.

Do the same for the maximum 5 character string.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Multi-base primes step by step in the Sidef programming language

Source code in the sidef programming language

func max_prime_bases(ndig, maxbase=36) {

    var maxprimebases = [[]]
    var nwithbases = [0]
    var maxprime = (10**ndig - 1)

    for p in (idiv(maxprime + 1, 10) .. maxprime) {
        var dig = p.digits
        var bases = (2..maxbase -> grep {|b| dig.all { _ < b } && dig.digits2num(b).is_prime })
        if (bases.len > maxprimebases.first.len) {
            maxprimebases = [bases]
            nwithbases = [p]
        }
        elsif (bases.len == maxprimebases.first.len) {
            maxprimebases << bases
            nwithbases << p
        }
    }

    var (alen, vlen) = (maxprimebases.first.len, maxprimebases.len)

    say("\nThe maximum number of prime valued bases for base 10 numeric strings of length ",
        ndig, " is #{alen}. The base 10 value list of ", vlen > 1 ? "these" : "this", " is:")
    maxprimebases.each_kv {|k,v| say(nwithbases[k], " => ", v) }
}

for n in (1..5) {
    max_prime_bases(n)
}


  

You may also check:How to resolve the algorithm Empty program step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Möbius function step by step in the Python programming language
You may also check:How to resolve the algorithm Align columns step by step in the Java programming language
You may also check:How to resolve the algorithm Break OO privacy step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the Racket programming language