# Identifying repeating patterns in a list of numbers

I have some generated lists of natural numbers which have a small number of distinct values, ie. referencing the variable "rowToCheck" for each list:

list1: rowToCheck = 3: length=7, distinctvalues=3
list2: rowToCheck = 4: length=47, distinctvalues=5
list3: rowToCheck = 5: length=479, distinctvalues=7
list4: rowToCheck = 6: length=5759, distinctvalues=10
list5: rowToCheck = 7: length=92159, distinctvalues=13
list6: rowToCheck = 8: length=1658879, distinctvalues=16
list7: rowToCheck = 9: length=36495359, distinctvalues=20

ie: list2 = {45,19,3,19,3,13,19,13,3,19,3,13,13,19,13,3,19,13,3,13,29,3,19,3,19,3,29,13,3,13,19,3,13,19,13,13,3,19,3,13,19,13,3,19,3,19,45}

Since there are few distinct values compared to the list length, it seems like these lists could be compressed or patterns identified, ie repeating substrings "3,19,3" in the above list for example.

Is there a built in method in Mathematica that would work to try to find subsequences or patterns in these lists? Also the sequence of distinct values is: 3,5,7,10,13,16,20,... I would like to find more terms for this sequence but ran of of RAM. Is there a way to reduce RAM usage or only check a portion of the list at a time for distinct values?

Here is the code:

rowToCheck = 3;
Primorial[n_] := Times @@ Prime[Range[n]]
row[x0_] :=
Module[{rowToCreate = x0},
Flatten@
Table[With[{a = n/GCD[n, #], b = Numerator[#/n]},
MapIndexed[a First@#2 - b #1 &,
Flatten@Position[GCD[Table[Mod[k, n], {k, n - 1}], n],
1] /. {} -> {1}]] &@EulerPhi@n, {n, rowToCreate,
rowToCreate}] (*Michael De Vlieger,Jun 06 2019*)
]
x = row[Primorial[rowToCheck]];
y = Abs[Differences[x]];
CountDistinct[y]
Length[y]
ListPlot[y]


cheers, Jamie

• check out reference.wolfram.com/language/ref/FindSequenceFunction.html and reference.wolfram.com/language/ref/FindTransientRepeat.html. Neither will solve your task trivially, but maybe a good place to start. Commented Nov 20, 2019 at 21:27
• Not sure if this is what you are looking for. All subsequences of length > 1 (may be overlapping) that repeat more than once. Subsequences[list2] // Select[Length@# > 1 &] // Counts // Select[# > 1 &] // ReverseSort Commented Nov 20, 2019 at 21:55
• Thanks, using your code for rowToCheck=3, output: {1,7,1}->2,{7,1}->2,{1,7}->2. Assigning a unique key to each subsequence: a={1,7,1} b={7,1} c={1,7} d={9} Then reconstructing y={9, 1, 7, 1, 7, 1, 9} with the fewest keys gives: {d,a,c,d} or {d,c,a,d}. Both having 4 keys used. Is it possible to take all the subsequences and find the fewest keys needed to reconstruct the sequence y? Commented Nov 20, 2019 at 22:44