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

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Multi-base primes step by step in the Julia 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 Julia programming language

The provided Julia code defines a function, maxprimebases, that determines the maximum number of prime-valued bases for base-10 numeric strings of a given length and also finds the corresponding base-10 numeric strings.

Here's a breakdown of the code:

  1. Function Signature:

    function maxprimebases(ndig, maxbase)

    The maxprimebases function takes two arguments:

    • ndig: The length of the numeric strings being considered.
    • maxbase: The maximum base to use for the prime evaluations.
  2. Initialization:

    maxprimebases = [Int[]]
    nwithbases = [0]

    The maxprimebases array will store the bases that yield prime values for each numeric string. The nwithbases array will store the corresponding base-10 numeric strings.

  3. Loop Over Primes:

    for p in div(maxprime + 1, 10):maxprime

    The loop iterates over prime numbers in the range [10^(ndig) - 1, 10^ndig].

  4. Extract Digits:

    dig = digits(p)

    The digits of the current prime number p are extracted.

  5. Find Prime Bases:

    bases = [b for b in 2:maxbase if (isprime(evalpoly(b, dig)) && all(i -> i < b, dig))]

    This comprehension loop iterates over bases from 2 to maxbase and checks two conditions:

    • isprime(evalpoly(b, dig)): Evaluates the prime number p in base b and checks if the result is prime.
    • all(i -> i < b, dig): Checks if all the digits of p are less than b.
  6. Update Results:

    if length(bases) > length(first(maxprimebases))
       maxprimebases = [bases]
       nwithbases = [p]
    elseif length(bases) == length(first(maxprimebases))
       push!(maxprimebases, bases)
       push!(nwithbases, p)
    end

    If the number of prime bases found for the current prime number p is greater than or equal to the current maximum, the maxprimebases and nwithbases arrays are updated accordingly.

  7. Print Results:

    alen, vlen = length(first(maxprimebases)), length(maxprimebases)
    println("\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:")
    for i in eachindex(maxprimebases)
       println(nwithbases[i], " => ", maxprimebases[i])
    end

    After the loop completes, it prints the maximum number of prime-valued bases and the corresponding base-10 numeric strings.

  8. Timing Measurement:

    @time for n in 1:6
       maxprimebases(n, 36)
    end

    This code snippet times the execution of the maxprimebases function for n from 1 to 6 with a maximum base of 36.

  9. Function Signature (Updated):

    function maxprimebases(ndig, maxbase)

    This modified version of the maxprimebases function takes the same two arguments as before.

  10. Updated Implementation: The implementation is similar to the previous version but uses a product-based approach to generate all possible numeric strings of length ndig. It also checks for hopelessness early on to avoid unnecessary computation.

  11. Loop Over Numeric Strings:

for tup in Iterators.product([0:maxbase-1 for _ in 1:ndig - 1]..., 1:maxbase-1)

This loop iterates over all numeric strings of length `ndig` using a product of ranges.

12. **Find Prime Bases**:
 ```julia
foundbases = Int[]
for b in maximum(dig)+1:maxbase
    if isprime(evalpoly(b, dig))
        push!(foundbases, b)
    end
    maxbase - b + length(foundbases) < length(maxprimebases) && break # shortcut if hopeless
end

Similar to the previous version, this loop checks for prime bases. However, it uses a maximum-digit-plus-one-based range to improve efficiency.

  1. Update Results: The update process is largely the same as in the previous version.

  2. Print Results: The code prints the maximum number of prime-valued bases and the corresponding base-10 numeric strings. It also indicates whether the numeric string is represented in base 32 (used for digits greater than 9).

  3. Timing Measurement:

for n in 1:5 maxprimebases(n, 36) maxprimebases(n, 62) end

This code snippet measures the execution time of the updated `maxprimebases` function for `n` from 1 to 5 with maximum bases of 36 and 62. 
<div id="sourcecode"/>

## Source code in the julia programming language

```julia  
using Primes

function maxprimebases(ndig, maxbase)
 maxprimebases = [Int[]]
 nwithbases = [0]
 maxprime = 10^(ndig) - 1
 for p in div(maxprime + 1, 10):maxprime
     dig = digits(p)
     bases = [b for b in 2:maxbase if (isprime(evalpoly(b, dig)) && all(i -> i < b, dig))]
     if length(bases) > length(first(maxprimebases))
         maxprimebases = [bases]
         nwithbases = [p]
     elseif length(bases) == length(first(maxprimebases))
         push!(maxprimebases, bases)
         push!(nwithbases, p)
     end
 end
 alen, vlen = length(first(maxprimebases)), length(maxprimebases)
 println("\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:")
 for i in eachindex(maxprimebases)
     println(nwithbases[i], " => ", maxprimebases[i])
 end
end

@time for n in 1:6
 maxprimebases(n, 36)
end


using Primes

function maxprimebases(ndig, maxbase)
 maxprimebases = [Int[]]
 nwithbases = ["0"]
 for tup in Iterators.product([0:maxbase-1 for _ in 1:ndig - 1]..., 1:maxbase-1)
     dig = collect(tup)
     foundbases = Int[]
     for b in maximum(dig)+1:maxbase
         if isprime(evalpoly(b, dig))
             push!(foundbases, b)
         end
         maxbase - b + length(foundbases) < length(maxprimebases) && break # shortcut if hopeless
     end
     if length(foundbases) > length(first(maxprimebases))
         maxprimebases = [foundbases]
         nwithbases = [prod(string.(reverse(dig), base = any(x -> x > 9, dig) ? 32 : 10))]
     elseif length(foundbases) == length(first(maxprimebases))
         push!(maxprimebases, foundbases)
         push!(nwithbases, prod(string.(reverse(dig), base = any(x -> x > 9, dig) ? 32 : 10)))
     end
 end
 alen, vlen = length(first(maxprimebases)), length(maxprimebases)
 println("\nThe maximum number of prime valued bases for base up to $maxbase numeric strings of length ",
     ndig, " is $alen. The value list of ", vlen > 1 ? "these" : "this", " is:")
 for i in eachindex(maxprimebases)
     println(nwithbases[i], maxprimebases[i][1] > 10 ? "(base 32)" : "", " => ", maxprimebases[i])
 end
end

for n in 1:5
 maxprimebases(n, 36)
 maxprimebases(n, 62)
end




You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the Component Pascal programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Dart programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Swift programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Prolog programming language