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 standardint
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'sint
PythonThink of Python's built-in
int
as a basic calculator and SageMath'sInteger
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.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()
.
SageMathInteger
: 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.In your loop, the variableWhy Your Code Fails with
int
n
is a SageMathInteger
because it's created by SageMath's range notation ([10..40000]
).
When you writenumber = int(n.str(base=b))
, you successfully convert the base-b
string representation ofn
into a standard Pythonint
and store it in thenumber
variable.
The program then fails at the next line:sorted(number.digits())
. Sincenumber
is a plain Pythonint
, it doesn't have the.digits()
method, and SageMath throws anAttributeError
.When you useWhy Your Code Works with
Integer
number = Integer(n.str(base=b))
, you are using SageMath'sInteger
constructor.
This creates a SageMathInteger
object and stores it in thenumber
variable.
Now, when the program reachessorted(number.digits())
, the call is valid because SageMathInteger
objects do have the.digits()
method. 👍In short, you must use
Integer
to ensure that bothn
andnumber
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 base13 31 423 32 746 64 7158 185 9227 272 9265 526 7316 631 7445 544 91030 3001 71045 4501 61135 5131 61234 3412 71236 1623 91273 2371 81366 3661 71380 1803 91431 4113 71454 4145 71653 3165 82027 2702 92060 6002 72116 6112 72154 4152 82315 3152 92534 3425 93160 6130 83161 6131 83162 6132 83163 6133 83164 6134 83165 6135 83166 6136 83167 6137 83226 6232 85270 7205 95567 7565 95637 7653 95783 7835 910144 41401 710235 15032 910342 42103 710453 15304 910542 42510 711425 45211 711750 17105 912415 51124 712450 51204 712564 51426 712651 51612 713045 53014 713245 53421 713260 20163 913402 20341 913534 54313 713620 20613 914610 60411 715226 62251 715643 63415 716255 65251 716273 37621 816546 66145 716633 66331 721322 32221 921753 52371 821763 32761 921835 32851 923568 35286 926804 40682 930576 73560 831457 75341 832348 48332 934582 52384 935001 53010 935081 53108 935228 53282 937465 56347 9
No comments:
Post a Comment