Showing posts with label change of base. Show all posts
Showing posts with label change of base. Show all posts

Monday, 25 August 2025

Primes from Primes

Following on from my previous post, let's consider primes such that, when we add their consecutive pairs of digits together and concatenate them, we end up with another prime. Let's consider an example. I recently turned \( \textbf{27901}\) days old. This number is prime and this is what happens when we apply these operations to the number:$$  \underbrace{2+7}_{ \Large 9} \quad \underbrace{7+9}_{\Large 16} \quad  \underbrace{9+0}_{ \Large 9} \quad \underbrace{0+1}_{ \Large 1} \rightarrow 91691 \text{ a prime number}  $$In the range up to 40000, there are 403 numbers that satisfy the criteria. I'll show the 101 numbers from 27901 to 40000 below (permalink):

27901, 28081, 28163, 28201, 28229, 28447, 28607, 28621, 28627, 28661, 28703, 28867, 28927, 28961, 29009, 29021, 29063, 29129, 29167, 29201, 29207, 29221, 29401, 29567, 29789, 29881, 29921, 30029, 30089, 30109, 30427, 30509, 30643, 30649, 30763, 30781, 30809, 30829, 30983, 31249, 31627, 31663, 31849, 31883, 32009, 32089, 32189, 32401, 32429, 32563, 32609, 32749, 32843, 32909, 33029, 33149, 33289, 33349, 33529, 33601, 33829, 33889, 34147, 34261, 34327, 34483, 34501, 34583, 34589, 34807, 34963, 35089, 35129, 35401, 35461, 35543, 35603, 36109, 36209, 36229, 36721, 36901, 37021, 37189, 37409, 37589, 37747, 37963, 38047, 38149, 38461, 38629, 38707, 38729, 39103, 39229, 39409, 39503, 39749, 39829, 39929

Let's take the last number, 39929, as a second example:$$  \underbrace{3+9}_{ \Large 12} \quad \underbrace{9+9}_{\Large 18} \quad  \underbrace{9+2}_{ \Large 11} \quad \underbrace{2+9}_{ \Large 11} \rightarrow 12181111 \text{ a prime number}  $$For completeness, here are primes that arise from the above primes:

27901 --> 91691
28081 --> 10889
28163 --> 10979
28201 --> 101021
28229 --> 1010411
28447 --> 1012811
28607 --> 101467
28621 --> 101483
28627 --> 101489
28661 --> 1014127
28703 --> 101573
28867 --> 10161413
28927 --> 1017119
28961 --> 1017157
29009 --> 11909
29021 --> 11923
29063 --> 11969
29129 --> 1110311
29167 --> 1110713
29201 --> 111121
29207 --> 111127
29221 --> 111143
29401 --> 111341
29567 --> 11141113
29789 --> 11161517
29881 --> 1117169
29921 --> 1118113
30029 --> 30211
30089 --> 30817
30109 --> 3119
30427 --> 3469
30509 --> 3559
30643 --> 36107
30649 --> 361013
30763 --> 37139
30781 --> 37159
30809 --> 3889
30829 --> 381011
30983 --> 391711
31249 --> 43613
31627 --> 4789
31663 --> 47129
31849 --> 491213
31883 --> 491611
32009 --> 5209
32089 --> 52817
32189 --> 53917
32401 --> 5641
32429 --> 56611
32563 --> 57119
32609 --> 5869
32749 --> 591113
32843 --> 510127
32909 --> 51199
33029 --> 63211
33149 --> 64513
33289 --> 651017
33349 --> 66713
33529 --> 68711
33601 --> 6961
33829 --> 6111011
33889 --> 6111617
34147 --> 75511
34261 --> 7687
34327 --> 7759
34483 --> 781211
34501 --> 7951
34583 --> 791311
34589 --> 791317
34807 --> 71287
34963 --> 713159
35089 --> 85817
35129 --> 86311
35401 --> 8941
35461 --> 89107
35543 --> 81097
35603 --> 81163
36109 --> 9719
36209 --> 9829
36229 --> 98411
36721 --> 91393
36901 --> 91591
37021 --> 10723
37189 --> 108917
37409 --> 101149
37589 --> 10121317
37747 --> 10141111
37963 --> 1016159
38047 --> 118411
38149 --> 119513
38461 --> 1112107
38629 --> 1114811
38707 --> 111577
38729 --> 1115911
39103 --> 121013
39229 --> 1211411
39409 --> 121349
39503 --> 121453
39749 --> 12161113
39829 --> 12171011
39929 --> 12181111

What if we convert the prime numbers in base 10 to numbers in other bases. These numbers will remain prime of course and then we apply the operations but in our chosen base. Let's work with base 9. In the range from 27901 up to 40000, 121 numbers satisfy (permalink):

28123, 28211, 28279, 28297, 28463, 28513, 28517, 28549, 28621, 28663, 28697, 28711, 28789, 28859, 28933, 29167, 29221, 29251, 29269, 29399, 29527, 29581, 29669, 29683, 29759, 29761, 29917, 29921, 30013, 30133, 30139, 30187, 30553, 30677, 30763, 30839, 30841, 30871, 30949, 31159, 31181, 31219, 31249, 31271, 31357, 31397, 31489, 31541, 32059, 32077, 32083, 32099, 32353, 32491, 32497, 32621, 32653, 32783, 32917, 32939, 32987, 33149, 33331, 33461, 33479, 33581, 33617, 33619, 33641, 33851, 34019, 34033, 34127, 34211, 34303, 34429, 34537, 34607, 34757, 34897, 34913, 34919, 35081, 35291, 35363, 35401, 35423, 35509, 35617, 35671, 35747, 35837, 35869, 35923, 35963, 35993, 36011, 36389, 36479, 36571, 36767, 36857, 36929, 37199, 37273, 37307, 37313, 37361, 37397, 37997, 38083, 38261, 38321, 38447, 38461, 38677, 38933, 39041, 39233, 39239, 39857

Let's look at 39239, the second last number in that list. It becomes 58738 in base 9.$$ \begin{align} 28219_{\, 10} = 58738_{ \, 9} &\rightarrow \underbrace{5+8}_{ \Large 14} \quad \underbrace{8+7}_{\Large 16} \quad \underbrace{7+3}_{ \Large 11} \quad \underbrace{3+8}_{ \Large 12}\\ \\ &\rightarrow 14161112_{\,9} \text{ a prime number} \\ \\ &= 7007969_{\,10} \text{ also prime} \end{align}$$The details are as follows (first column is base 10 prime, second column is prime in base 9, third prime is the prime resulting from the operations of addition and concatenation in base 9 and the fourth column is the prime converted to a prime in base 10.

28123 --> 42517 --> 6768 --> 5003
28211 --> 42625 --> 6887 --> 5101
28279 --> 42711 --> 61082 --> 40169
28297 --> 42731 --> 610114 --> 360949
28463 --> 43035 --> 7338 --> 5381
28513 --> 43101 --> 7411 --> 5437
28517 --> 43105 --> 7415 --> 5441
28549 --> 43141 --> 7455 --> 5477
28621 --> 43231 --> 7554 --> 5557
28663 --> 43277 --> 751015 --> 446891
28697 --> 43325 --> 7657 --> 5641
28711 --> 43341 --> 7675 --> 5657
28789 --> 43437 --> 77711 --> 51607
28859 --> 43525 --> 7877 --> 5821
28933 --> 43617 --> 71078 --> 46727
29167 --> 44007 --> 8407 --> 6163
29221 --> 44067 --> 84614 --> 55903
29251 --> 44111 --> 8522 --> 6257
29269 --> 44131 --> 8544 --> 6277
29399 --> 44285 --> 861114 --> 512581
29527 --> 44447 --> 88812 --> 58979
29581 --> 44517 --> 81068 --> 53279
29669 --> 44625 --> 81187 --> 53377
29683 --> 44641 --> 811115 --> 479777
29759 --> 44735 --> 812118 --> 480509
29761 --> 44737 --> 8121111 --> 4324519
29917 --> 45031 --> 10534 --> 6997
29921 --> 45035 --> 10538 --> 7001
30013 --> 45147 --> 106512 --> 63839
30133 --> 45301 --> 10831 --> 7237
30139 --> 45307 --> 10837 --> 7243
30187 --> 45361 --> 108107 --> 64969
30553 --> 45817 --> 1014108 --> 541007
30677 --> 46065 --> 116612 --> 70481
30763 --> 46171 --> 11788 --> 7937
30839 --> 46265 --> 118812 --> 72101
30841 --> 46267 --> 118814 --> 72103
30871 --> 46311 --> 111042 --> 66377
30949 --> 46407 --> 111147 --> 66463
31159 --> 46661 --> 1113137 --> 599353
31181 --> 46685 --> 11131514 --> 5394289
31219 --> 46737 --> 11141111 --> 5400523
31249 --> 46771 --> 1114158 --> 600101
31271 --> 46805 --> 111585 --> 66821
31357 --> 47011 --> 12712 --> 8597
31397 --> 47055 --> 127511 --> 77689
31489 --> 47167 --> 128714 --> 78583
31541 --> 47235 --> 121058 --> 72953
32059 --> 47871 --> 1216168 --> 660617
32077 --> 48001 --> 13801 --> 9397
32083 --> 48007 --> 13807 --> 9403
32099 --> 48025 --> 13827 --> 9421
32353 --> 48337 --> 1312611 --> 717103
32491 --> 48511 --> 131462 --> 79841
32497 --> 48517 --> 131468 --> 79847
32621 --> 48665 --> 13151312 --> 6470129
32653 --> 48711 --> 131682 --> 80021
32783 --> 48865 --> 13171512 --> 6483413
32917 --> 50134 --> 5147 --> 3769
32939 --> 50158 --> 51614 --> 34033
32987 --> 50222 --> 5244 --> 3847
33149 --> 50422 --> 5464 --> 4027
33331 --> 50644 --> 56118 --> 37277
33461 --> 50808 --> 5888 --> 4373
33479 --> 50828 --> 581111 --> 348553
33581 --> 51052 --> 6157 --> 4507
33617 --> 51102 --> 6212 --> 4547
33619 --> 51104 --> 6214 --> 4549
33641 --> 51128 --> 62311 --> 41077
33851 --> 51382 --> 641211 --> 381439
34019 --> 51588 --> 661417 --> 394729
34033 --> 51614 --> 6775 --> 5009
34127 --> 51728 --> 681011 --> 407521
34211 --> 51832 --> 610125 --> 360959
34303 --> 52044 --> 7248 --> 5309
34429 --> 52204 --> 7424 --> 5449
34537 --> 52334 --> 7567 --> 5569
34607 --> 52422 --> 7664 --> 5647
34757 --> 52608 --> 7868 --> 5813
34897 --> 52774 --> 7101512 --> 3780281
34913 --> 52802 --> 71182 --> 46811
34919 --> 52808 --> 71188 --> 46817
35081 --> 53108 --> 8418 --> 6173
35291 --> 53362 --> 86108 --> 56951
35363 --> 53452 --> 87107 --> 57679
35401 --> 53504 --> 8854 --> 6529
35423 --> 53528 --> 88711 --> 58897
35509 --> 53634 --> 810107 --> 479041
35617 --> 53764 --> 8111411 --> 4318201
35671 --> 53834 --> 812127 --> 480517
35747 --> 54028 --> 104211 --> 62137
35837 --> 54138 --> 105412 --> 63029
35869 --> 54174 --> 105812 --> 63353
35923 --> 54244 --> 10668 --> 7109
35963 --> 54288 --> 1061117 --> 571633
35993 --> 54332 --> 10765 --> 7187
36011 --> 54352 --> 10787 --> 7207
36389 --> 54822 --> 1013114 --> 540283
36479 --> 55032 --> 11535 --> 7727
36571 --> 55144 --> 11658 --> 7829
36767 --> 55382 --> 1181211 --> 643879
36857 --> 55502 --> 111152 --> 66467
36929 --> 55582 --> 11111411 --> 5381083
37199 --> 56022 --> 12624 --> 8527
37273 --> 56114 --> 12725 --> 8609
37307 --> 56152 --> 12767 --> 8647
37313 --> 56158 --> 127614 --> 77773
37361 --> 56222 --> 12844 --> 8707
37397 --> 56262 --> 12888 --> 8747
37997 --> 57108 --> 13818 --> 9413
38083 --> 57214 --> 131035 --> 79493
38261 --> 57432 --> 131275 --> 79691
38321 --> 57508 --> 131358 --> 79757
38447 --> 57658 --> 13141214 --> 6463489
38461 --> 57674 --> 13141412 --> 6463649
38677 --> 58044 --> 14848 --> 10169
38933 --> 58358 --> 1412814 --> 776317
39041 --> 58488 --> 14131317 --> 6988453
39233 --> 58732 --> 1416115 --> 778667
39239 --> 58738 --> 14161112 --> 7007969
39857 --> 60605 --> 6665 --> 4919

Thursday, 14 August 2025

Coding and Integers

The thought struck me to write a program that looked at all base 10 numbers in the range from 10 to 40000 that, when converted to a number base between 2 and 9, returned a number with the same digits as the base 10 number but in a different order. It's a simple enough program to write in SageMath and this is what I created originally:

T=[("number","permutation","base")]
T.append(("","",""))
for n in [10..40000]:
    for b in [2..9]:
        number=int(n.str(base=b))
        if sorted(n.digits())==sorted(number.digits()):
            L.append(n)
            T.append((n,n.str(base=b),b))
print(L)
print(len(L))
print()
print(table(T))

The program will not run because there is a problem with "sorted(number.digits())". I get the error message "AttributeError: 'int' object has no attribute 'digits' ". I know from experience how to fix the problem. I simply replace "int" with "Integer" but I've never fully understood why this change is necessary. I queried Gemini and it came up with the following explanation:

Of course. Your code requires SageMath's Integer type because it uses special mathematical methods that Python's standard int type doesn't have.

The specific line of code that causes the issue is if sorted(n.digits())==sorted(number.digits()):.


SageMath's Integer vs. Python's int

Think of Python's built-in int as a basic calculator and SageMath's Integer as a powerful scientific calculator. While both can store whole numbers, the SageMath version comes with a lot of extra functions built specifically for number theory and advanced mathematics.

Python int: A standard data type for storing whole numbers. It's great for general programming, loops, and basic arithmetic. However, it does not have a built-in method called .digits().
SageMath Integer: A more specialized object. It's designed for mathematical computation and has many useful methods, including:
.digits(): Returns a list of the number's digits.
.str(base=b): Returns the string representation of the number in a given base.
.is_prime(): Checks if the number is prime.
.divisors(): Returns a list of the number's divisors.

Why Your Code Fails with int

In your loop, the variable n is a SageMath Integer because it's created by SageMath's range notation ([10..40000]).
When you write number = int(n.str(base=b)), you successfully convert the base-b string representation of n into a standard Python int and store it in the number variable.
The program then fails at the next line: sorted(number.digits()). Since number is a plain Python int, it doesn't have the .digits() method, and SageMath throws an AttributeError.

Why Your Code Works with Integer

When you use number = Integer(n.str(base=b)), you are using SageMath's Integer constructor.
This creates a SageMath Integer object and stores it in the number variable.
Now, when the program reaches sorted(number.digits()), the call is valid because SageMath Integer objects do have the .digits() method. 👍

In short, you must use Integer to ensure that both n and number are SageMath objects that understand the special mathematical methods your algorithm relies on.

So now I know. So what is the output from my program when it is run? It turns out that there are 76 numbers that satisfy and they are (permalink):

13, 23, 46, 158, 227, 265, 316, 445, 1030, 1045, 1135, 1234, 1236, 1273, 1366, 1380, 1431, 1454, 1653, 2027, 2060, 2116, 2154, 2315, 2534, 3160, 3161, 3162, 3163, 3164, 3165, 3166, 3167, 3226, 5270, 5567, 5637, 5783, 10144, 10235, 10342, 10453, 10542, 11425, 11750, 12415, 12450, 12564, 12651, 13045, 13245, 13260, 13402, 13534, 13620, 14610, 15226, 15643, 16255, 16273, 16546, 16633, 21322, 21753, 21763, 21835, 23568, 26804, 30576, 31457, 32348, 34582, 35001, 35081, 35228, 37465

These numbers are the initial members of OEIS A090144 (I've discounted the trivial numbers from 1 to 8). Here are the details:

  number   permutation   base
  13       31            4
  23       32            7
  46       64            7
  158      185           9
  227      272           9
  265      526           7
  316      631           7
  445      544           9
  1030     3001          7
  1045     4501          6
  1135     5131          6
  1234     3412          7
  1236     1623          9
  1273     2371          8
  1366     3661          7
  1380     1803          9
  1431     4113          7
  1454     4145          7
  1653     3165          8
  2027     2702          9
  2060     6002          7
  2116     6112          7
  2154     4152          8
  2315     3152          9
  2534     3425          9
  3160     6130          8
  3161     6131          8
  3162     6132          8
  3163     6133          8
  3164     6134          8
  3165     6135          8
  3166     6136          8
  3167     6137          8
  3226     6232          8
  5270     7205          9
  5567     7565          9
  5637     7653          9
  5783     7835          9
  10144    41401         7
  10235    15032         9
  10342    42103         7
  10453    15304         9
  10542    42510         7
  11425    45211         7
  11750    17105         9
  12415    51124         7
  12450    51204         7
  12564    51426         7
  12651    51612         7
  13045    53014         7
  13245    53421         7
  13260    20163         9
  13402    20341         9
  13534    54313         7
  13620    20613         9
  14610    60411         7
  15226    62251         7
  15643    63415         7
  16255    65251         7
  16273    37621         8
  16546    66145         7
  16633    66331         7
  21322    32221         9
  21753    52371         8
  21763    32761         9
  21835    32851         9
  23568    35286         9
  26804    40682         9
  30576    73560         8
  31457    75341         8
  32348    48332         9
  34582    52384         9
  35001    53010         9
  35081    53108         9
  35228    53282         9
  37465    56347         9