Recently I came across a fascinating Numberphile video on truncatable primes
I immediately thought it would be cool to whip a quick Julia code to get the full enumeration of all left truncatable primes, count the number of branches and also get the largest left truncatable prime.
using Primes function get_left_primes(s::String) p_arr=Array{String,1}() for i=1:9 number_s="$i$s" if isprime(parse(BigInt, number_s)) push!(p_arr,number_s) end end p_arr end function get_all_left_primes(l) r_l= Array{String,1}() n_end_points=0 for i in l new_l=get_left_primes(i) isempty(new_l) && (n_end_points+=1) append!(r_l,new_l) next_new_l,new_n=get_all_left_primes(new_l) n_end_points+=new_n # counting the chains append!(r_l,next_new_l) end r_l, n end
The first function just prepends a number (expressed in String for convenience) and checks for it possible primes that can emerge from a single digit prepending. For example:
julia> get_left_primes("17") 2-element Array{String,1}: "317" "617"
The second function, just makes extensive use of the first to get all left truncatable primes and also count the number of branches.
julia> all_left_primes, n_branches=get_all_left_primes([""]) (String["2", "3", "5", "7", "13", "23", "43", "53", "73", "83" … "6435616333396997", "6633396997", "76633396997", "963396997", "16396997", "96396997", "616396997", "916396997", "396396997", "4396396997"], 1442) julia> n_branches 1442 julia> all_left_primes 4260-element Array{String,1}: "2" "3" "5" "7" "13" "23" ⋮ "96396997" "616396997" "916396997" "396396997" "4396396997"
So we the full list of possible left truncatable primes with a length 4260. Also the total number of branches came to 1442.
We now get the largest left truncatable primes with the following one liner:
julia> largest_left_prime=length.(all_left_primes)|>indmax|> x->all_left_primes[x] "357686312646216567629137"
After this fun exploration, I found an implementation in Julia for just getting the largest left truncatable prime for any base in Rosseta Code.