Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/sage/rings/integer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4963,6 +4963,8 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
(2, 2)
sage: 256.perfect_power()
(2, 8)
sage: (26**2).perfect_power()
(26, 2)
"""
cdef long n
# Fast PARI-free path
Expand All @@ -4974,7 +4976,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
if not (n & 1):
if mpz_popcount(self.value) == 1:
return smallInteger(2), smallInteger(mpz_sizeinbase(self.value, 2) - 1)
if n < 1000:
elif n < 1000:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to test against len(_small_primes_table) (or store this in a constant)?

Copy link
Member

@vincentmacri vincentmacri Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to test against len(_small_primes_table) (or store this in a constant)?

_small_primes_table is kind of weird. It's not a list or table of primes themselves. Rather, _small_primes_table[n] is 1 if $2n + 1$ is prime, and 0 if $2n + 1$ is not prime (so _small_primes_table[0] = 0 because 1 is not prime, and _small_primes_table[1] = 1 because 3 is prime).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and similar logic is being employed in three different places in the code, ie, check if value < 1000 and is odd and if so, invoke _small_primes_table[value >> 1]. We should store this 1000 value in a constant.

if _small_primes_table[n >> 1]:
return self, one

Expand Down
Loading