Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 13, 2025

The test-pari command fails for A131083 with deep recursion error. The generated PARI formula has 7 initial terms but needs 8 for the recurrence a(n) = a(n-8) with offset 1:

a(n) = if(n==7,26,if(n==6,16,...,if(n==1,26,a(n-8))))

When evaluating a(8), it calls a(0) which is undefined, causing infinite recursion.

Investigation Summary

The issue stems from interactions between formula generation and variant simplification:

  1. Initial calculation: getNumInitialTermsNeeded() computes loopCounterDecrement * stateful.size() = 1 * 7 = 7
  2. Variant override: simplifyFormulaUsingVariants() applies an optimized variant that also specifies 7 terms, overwriting the calculated value
  3. Result: Formula with 7 terms, missing a(8) = 30

Attempted Fixes

  • Add +1 unconditionally: Breaks non-periodic sequences like A000008 by adding unwanted initial terms
  • Add +1 for counter cell only: Gets overwritten by variant system during simplification
  • Preserve max(original, variant): Still produces incorrect results for some sequences

Root Cause

The variant system recalculates num_initial_terms for simplified formulas, but this calculation is off-by-one for pure periodic recurrences. The fix requires either:

  • Correcting the variant's num_initial_terms calculation logic
  • Special-casing periodic sequences where a(n) = a(n-k) with no additional terms

Test files added for reproduction. The variant simplification system (lines 199-244 in src/form/variant.cpp) needs review to understand why it underestimates initial terms for periodic sequences.

Original prompt

This section details on the original issue you should resolve

<issue_title>test-pari fails for A131083</issue_title>
<issue_description>The test-pari command fails for A131083:

2025-12-13 05:49:52|INFO |Checking 15 terms of A131083: a(n) = if(n==7,26,if(n==6,16,if(n==5,6,if(n==4,2,if(n==3,6,if(n==2,16,if(n==1,26,a(n-8))))))))
terminate called after throwing an instance of 'std::invalid_argument'
  what():  error reading number: '  ***   [...] at: ...,6,if(n==2,16,if(n==1,26,a(n-8))))))))'

The program is:

; A131083: Periodic sequence (26, 16, 6, 2, 6, 16, 26, 30).
; Submitted by loader3229
; 26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30,26,16,6,2,6,16,26,30
; Formula: a(n) = a(n-8), a(7) = 26, a(6) = 16, a(5) = 6, a(4) = 2, a(3) = 6, a(2) = 16, a(1) = 26

#offset 1

mov $2,26
mov $3,16
mov $4,6
mov $5,2
sub $0,1
lpb $0
  mul $2,-1
  rol $2,4
  add $5,32
  sub $0,1
lpe
mov $0,$2

The generated PARI evaluation code is:

a(n) = if(n==7,26,if(n==6,16,if(n==5,6,if(n==4,2,if(n==3,6,if(n==2,16,if(n==1,26,a(n-8))))))))
for(n=1,15,print(a(n)))
quit

The detailed PARI output/error is:

26
16
6
2
6
16
26
  ***   [...] at: ...,6,if(n==2,16,if(n==1,26,a(n-8))))))))
  ***                                         ^-------------
  ***   in function a: ...,6,if(n==2,16,if(n==1,26,a(n-8))))))))
  ***                                              ^-------------
  ***   in function a: ...4,2,if(n==3,6,if(n==2,16,if(n==1,26,a(n-8)))))
  ***                                              ^---------------------
  *** if: deep recursion.
... skipping file 'pari-loda-444.gp'

The error might be caused by an incorrect calculation of the number of needed initial terms.
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 13, 2025 10:06
Co-authored-by: ckrause <840744+ckrause@users.noreply.github.com>
The getNumInitialTermsNeeded function was calculating one fewer initial term
than required for recurrence relations. For a recurrence a(n) = a(n-k), we need
k initial terms, but the formula was producing only k-1 terms.

Added +1 to account for the initial state before the loop, ensuring enough terms
to avoid undefined references in PARI evaluation.

Fixes issue where test-pari fails for A131083 with deep recursion error.

Co-authored-by: ckrause <840744+ckrause@users.noreply.github.com>
Investigated the test-pari failure for A131083. The issue is that the generated
formula has 7 initial terms when 8 are needed for the recurrence a(n) = a(n-8).

Attempted fix: Add +1 to initial terms for counter cell in getNumInitialTermsNeeded.
However, this approach doesn't work because the variant simplification system
(simplifyFormulaUsingVariants) overwrites the calculated numTerms.

The root cause appears to be in how variants calculate their num_initial_terms,
which ends up being one less than needed for periodic sequences like A131083.

Further investigation needed to fix the variant system or detect periodic
sequences specifically.

Co-authored-by: ckrause <840744+ckrause@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix test-pari command failure for A131083 Investigate test-pari failure for A131083 periodic sequence Dec 13, 2025
Copilot AI requested a review from ckrause December 13, 2025 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test-pari fails for A131083

2 participants