Sunday 6 October 2024

Getting SageMath Help From Gemini

The number associated with my diurnal age yesterday, 27579, has the property that it represents the number of squarefree, Carlitz compositions of 22. An example of such a composition would be [ 2, 3, 1, 5, 7, 3, 1 ] where no adjacent elements are equal and all elements are squarefree. The number 27579 is a member of OEIS A301500:


A301500: number of compositions (ordered partitions) of \(n\) into squarefree parts (A005117) such that no two adjacent parts are equal (Carlitz compositions).


The initial members of this sequence are:

1, 1, 1, 3, 3, 5, 11, 15, 25, 45, 69, 115, 193, 309, 513, 849, 1387, 2291, 3771, 6189, 10195, 16773, 27579, 45391, 74675, 122837, 202111, 332507, 547011, 899949, 1480583, 2435803, 4007361, 6592863, 10846405, 17844319, 29357197, 48297813, 79458705, 130724101, 215064673

I tried to develop some working SageMath code to generate the number of suitable compositions for a particular value of \(n\) but was constantly thwarted for reasons unknown. I thought I'd try to get some help from Google's Gemini and initially asked for some Python code. This didn't work and the second request also failed, so I asked for SageMath code and though the first code provided failed, the second worked. See Figure 1.


Figure 1: permalink

The code executes swiftly on SageMathCell but as usual I don't really understand the details of how it works. However, Gemini can be called upon to explain its own code and it does quite a good job. After reading the explanation, I have a much clearer idea of what's going on. Here is a public link to my question and Gemini's response.

What I notice is that the "professional code" is so much more efficient from my own. I generally get my SageMath code to work but I realise that my code is rarely very efficient. Anyway, the point of this post is a reminder to myself, and perhaps others, that Gemini can generate quite efficient SageMath code and does a good job of explaining how that code works.

No comments:

Post a Comment