# What algorithm does Mathematica's FindSequenceFunction use, and can it be replicated in Python?

I am curious about the algorithm used by Mathematica's FindSequenceFunction to identify patterns in sequences. This function is powerful in finding closed-form expressions for a wide range of sequences. I would like to know the details of the algorithm it employs to achieve this.

Furthermore, I am interested in replicating this functionality in a programming language like Python. Is the algorithm used by FindSequenceFunction publicly available, or is it a proprietary method developed by Wolfram Research? If it is available, are there any existing Python libraries or implementations that I can use to achieve similar results?

Additionally, I would appreciate any insights into how the function handles different types of sequences, such as those involving rational numbers, and how it determines the complexity of the sequence to decide whether to return a result or not.

Lastly, are there any specific techniques or methods that I can use to write a similar function from scratch in Python, and what would be the key challenges in doing so?

• Some folks a few years ago had a website that (they claimed) outperformed FindSequenceFunction and similar functions in other systems. It did on their examples. Then the website went down, and I can't find any notes about it. I probably thought idiotically that the internet was forever. Commented Jun 17 at 14:44
• Try TracePrint[FindSequenceFunction[{1, 1, 2, 3, 5, 8}, n]]? Commented Jun 17 at 15:41
• "I am interested in replicating this functionality in a programming language like Python." It will not be easy. Look at the history of developments of SageMath and FriCAS. About guessing sequences, FriCAS can do that. Since you ask about Python, have a look at JFriCAS. As a side note, FriCAS has most amazing implementations of some algorithms, such as Risch. Commented Jun 17 at 15:59

By setting

Unprotect[FindSequenceFunction];
Attributes[FindSequenceFunction] =.;


then you can see the codes of FindSequenceFunction as

GeneralUtilitiesPrintDefinitions@FindSequenceFunction


The above code returns a notebook as

Now you can click the grey underlined function with the mouse to open the definition notebook of the function, like findSequenceFunctionParser here. You can see that findSequenceFunctionParser calls findSequenceFunctionDispatcher, findSequenceFunctionDispatcher calls findSequenceFunction, findSequenceFunction calls findPeriodicSequence findPolynomialSequence findRationalSequence findHypergeometricTermsSequenceand so on. Now you can see the original codes of FindSequenceFunction, and I hope you have an enjoyable experience browsing the code.

• You don't actually need any Unprotect, just do GeneralUtilitiesPrintDefinitions[FindSequenceFunction]. Commented Jun 17 at 15:10
• @Domen FindSequenceFunction has the attribute ReadProtected, so without unsetting this attribute, GeneralUtilitiesPrintDefinitions[FindSequenceFunction] returns nothing. Commented Jun 17 at 15:13
• No, it's not about the ReadProtected attribute. It's the fact that you have to use this function at least once before in a kernel session so that it gets loaded (it's not automatically loaded). In a fresh kernel, you can just do for example FindSequenceFunction[]; GeneralUtilitiesPrintDefinitions[FindSequenceFunction]. Commented Jun 17 at 15:25
• @Domen Got it, thank you! Commented Jun 17 at 15:36
• @Domen I have to execute GeneralUtilitiesPrintDefinitions[FindSequenceFunction] twice on a fresh kernel, if I do FindSequenceFunction[] first. But if I execute FindSequenceFunction[{}]` first, then I can print the definitions on the first try. (V14.0.0 Mac ARM). Odd. Often just executing the head symbol loads the internal package for other function. Commented Jun 17 at 16:02