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

Energetic Numbers Revisited

In June of 2024, I posted about Energetic Numbers which are numbers that can be split into two or more parts and these parts, when raised to appropriate integer powers and added together, equal the original number. An example is 27476:$$ 27476 =27^3 + 4^6 + 7^4 + 6^4 $$In that post I made reference to a text file that listed the first 10000 energetic numbers but did not show how the numbers could be split and recombined.

Fortunately, I was able to get Gemini to create a SageMath program (permalink) that accomplishes this task whenever it is possible. I was prompted to attempt this because 27892, the number associated with my diurnal age today, is one of the numbers listed in the previously mentioned text file. For this number we have:$$27892=27^3 + 8 + 9 + 2^{13}$$Here is a list of some of the upcoming energetic numbers. Notice the run of consecutive numbers from 28260 to 28269 (permalink):

\(27892 = 27^3 + 8 + 9 + 2^{13}\) 
\(27923 = 2^{13} + 7^5 + 9^3 + 2^3 + 3^7\)
\(27962 = 27^3 + 9^2 + 6 + 2^{13}\)
\(27972 = 27^3 + 97 + 2^{13}\)
\(27984 = 2^9 + 7^5 + 9^4 + 8 + 4^6\)
\(28132 = 2^{12} + 8^4 + 1 + 3^9 + 2^8\)
\(28160 = 2^{11} + 8^3 + 160^2\)
\(28203 = 2^9 + 8 + 20^3 + 3^9\)
\(28224 = 28^3 + 2^{11} + 2^7 + 4^6\)
\(28228 = 2^{14} + 82^2 + 2^{10} + 8^4\)
\(28243 = 28^3 + 2^3 + 4^6 + 3^7\)
\(28245 = 2^{13} + 8^3 + 2^5 + 4^7 + 5^5\)
\(28260 = 2^{14} + 8^4 + 2^2 + 6^5 + 0\)
\(28261 = 2^{14 }+ 8^4 + 2^2 + 6^5 + 1\)
\(28262 = 2^{14 }+ 8^4 + 2^2 + 6^5 + 2\)
\(28263 = 2^{14} + 8^4 + 2^2 + 6^5 + 3\)
\(28264 = 2^{14 }+ 8^4 + 2^2 + 6^5 + 4\)
\(28265 = 2^{14 }+ 8^4 + 2^2 + 6^5 + 5\)
\(28266 = 2^{14} + 8^4 + 2^2 + 6^5 + 6\)
\(28267 = 2^{14} + 8^4 + 2^2 + 6^5 + 7\)
\(28268 = 2^{14} + 8^4 + 2^2 + 6^5 + 8\)
\(28269 = 2^{14} + 8^4 + 2^2 + 6^5 + 9\)

27892 also has the property that it can be split into two parts and the two parts, when added together, form a palindromic prime with a prime index:$$ \begin{align} 27892 &\rightarrow 27 + 892 \\  &=919          \\ &=\text{prime}(157) \end{align}$$

Wednesday, 13 August 2025

Numbers As Permutations Of Their Arithmetic Derivatives

A very few numbers have arithmetic derivatives that equal to themselves. These are numbers of the form \(n^n \) where \(n\) is prime. Thus we have \(4 =2^2, 27 =3^3, 3125 =5^5\) etc. What about numbers that have arithmetic derivatives with the same digits but in a different order? The number associated with my diurnal age, \( \textbf{27891}\) today is one such number because:$$ \begin{align} 27891' &= (3^3 \times 1033)' \\ &=(3^3)' \times 1033 + 3^3 \times 1033' \\ &=3^3 \times 1033 + 3^3 \\ &= 27981 + 27 \\&=27918\end{align}$$In the range up to 40000, there are 43 such numbers including those are remain the same such as 4, 27, 3125 etc. The numbers are (permalink):

4, 27, 94, 308, 526, 594, 950, 1208, 1269, 1647, 2403, 3125, 5589, 5643, 5926, 6934, 9369, 10503, 10568, 11084, 11284, 12404, 12447, 13130, 13500, 14024, 14769, 17469, 17847, 18036, 20358, 20547, 20852, 25569, 27891, 28647, 29835, 34803, 36068, 36180, 36747, 38396, 39069

Another sequence can be declared whose members are the arithmetic derivatives themselves. In other words, numbers that are the arithmetic derivatives of numbers with identical digits but in a possibly different order (permalink):

4, 27, 49, 265, 380, 905, 945, 1296, 1674, 1820, 2430, 2965, 3125, 3469, 6453, 9396, 9558, 10331, 10530, 11804, 12474, 14128, 14204, 14796, 15860, 17496, 17874, 20574, 21044, 22508, 25596, 27918, 28674, 32805, 34830, 35100, 36180, 36774, 36860, 39096, 39836, 39852, 80136

Here is a breakdown of each number's factorisation and permutation:

  number   factors              permutation

  4        2^2                  4
  27       3^3                  27
  94       2 * 47               49
  308      2^2 * 7 * 11         380
  526      2 * 263              265
  594      2 * 3^3 * 11         945
  950      2 * 5^2 * 19         905
  1208     2^3 * 151            1820
  1269     3^3 * 47             1296
  1647     3^3 * 61             1674
  2403     3^3 * 89             2430
  3125     5^5                  3125
  5589     3^5 * 23             9558
  5643     3^3 * 11 * 19        6453
  5926     2 * 2963             2965
  6934     2 * 3467             3469
  9369     3^3 * 347            9396
  10503    3^3 * 389            10530
  10568    2^3 * 1321           15860
  11084    2^2 * 17 * 163       11804
  11284    2^2 * 7 * 13 * 31    14128
  12404    2^2 * 7 * 443        14204
  12447    3^3 * 461            12474
  13130    2 * 5 * 13 * 101     10331
  13500    2^2 * 3^3 * 5^3      35100
  14024    2^3 * 1753           21044
  14769    3^3 * 547            14796
  17469    3^3 * 647            17496
  17847    3^3 * 661            17874
  18036    2^2 * 3^3 * 167      36180
  20358    2 * 3^3 * 13 * 29    32805
  20547    3^3 * 761            20574
  20852    2^2 * 13 * 401       22508
  25569    3^3 * 947            25596
  27891    3^3 * 1033           27918
  28647    3^3 * 1061           28674
  29835    3^3 * 5 * 13 * 17    39852
  34803    3^3 * 1289           34830
  36068    2^2 * 71 * 127       36860
  36180    2^2 * 3^3 * 5 * 67   80136
  36747    3^3 * 1361           36774
  38396    2^2 * 29 * 331       39836
  39069    3^3 * 1447           39096

These numbers constitute OEIS A225902. Some of these numbers are also ABCD numbers and these are:

1269, 10503, 12447, 14769, 17469, 17847, 18036, 20358, 20547, 27891, 28647, 29835

For example, 17892 + 27198 + \( \textbf{27891} \) = 72981