Playing with prime numbers

Arjen Markus (7 february 2022) The other weekend I read a column in the newspaper that mentioned the number 73939133. (It is a column devoted to number, by the way). While it seems perhaps just a random number with too many digits, it is special in the following sense:

First of all the number itself is a prime. But if you take off the "3" at the end, the result 7393913 is again a prime and you can go on all the way to the leading 7. The underlying arithmetic is nicely described in this Youtube movie (thanks to kbk for the link).

The script below is a slight variant on the procedure. In stead of adding digits to the left or to the right, it determines a series of primes that start with a single prime, not necessarily a decimal digit. Also, it allows you to experiment with bases other than 10. Try 30 for instance.

The constructed primes are:

p, n*p+a, n*(n*p+a)+b, ...

where p is the initial prime, n is the base and the numbers a and b are chosen (via brute force) to make the number `n*p+a` a prime.

For the starting prime 7, it gives this output:

(6) 7 71 719 7193 71933 719333
(4) 7 73 733 7331
(5) 7 73 733 7333 73331
(8) 7 73 739 7393 73939 739391 7393913 73939133
(7) 7 73 739 7393 73939 739393 7393931
(7) 7 73 739 7393 73939 739393 7393933
(6) 7 73 739 7393 73939 739397
(6) 7 73 739 7393 73939 739399
(3) 7 79 797

But starting with 19 gives a slightly longer sequence:

(9) 19 197 1979 19793 197933 1979339 19793393 197933933 1979339333
(9) 19 197 1979 19793 197933 1979339 19793393 197933933 1979339339
(5) 19 199 1993 19937 199373
(5) 19 199 1993 19937 199379
(5) 19 199 1997 19973 199739
(6) 19 199 1997 19979 199799 1997999
(4) 19 199 1999 19991
(5) 19 199 1999 19993 199931
(7) 19 199 1999 19993 199933 1999331 19993319
(6) 19 199 1999 19993 199933 1999339
(4) 19 199 1999 19997

If you do that with base 30, the result is two sequences of 22 primes:

(22) 19 593 17807 534229 16026877 480806317 14424189523 432725685703 12981770571113 389453117133419 11683593514002577 350507805420077339 10515234162602320187 315457024878069605623 9463710746342088168703 283911322390262645061091 8517339671707879351832737 255520190151236380554982117 7665605704537091416649463527 229968171136112742499483905823 6899045134083382274984517174697 206971354022501468249535515240911
(22) 19 593 17807 534229 16026877 480806317 14424189523 432725685703 12981770571113 389453117133419 11683593514002577 350507805420077339 10515234162602320187 315457024878069605623 9463710746342088168703 283911322390262645061091 8517339671707879351832737 255520190151236380554982117 7665605704537091416649463527 229968171136112742499483905823 6899045134083382274984517174697 206971354022501468249535515240921

Written in a base-30 "decimal" format, you could strip off the trailing 21 times.

Here is the code:

# chain_primes.tcl --
#     Determine a "chain" of primes of the form p, n*p+a, ...
#     where 0 < a < p chosen so that the number n*p+a is prime
#
package require math::numtheory

proc nextTry {start multiple chain} {
    set print 1
    for {set a 1} {$a < $multiple} {incr a} {
        set next [expr {$multiple * $start + $a}]

        if { [::math::numtheory::isprime $next] } {
            set new_chain [concat $chain $next]

            nextTry $next $multiple $new_chain
            set print 0
        }
    }

    if { $print } {
        puts "([llength $chain]) $chain"
    }
}

foreach start {2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 79 83 89 97 101} {
    nextTry $start 10 [list $start]
}

Does it serve any practical purpose? Not that I know of. It was merely fun to do.