18

The $n$-th square-pyramidal number is the sum of the first $n$ squares: $$ P_n = \sum_{k=1}^n k^2 = \frac{n(n+1)(2n+1)}6. $$

Suppose we have a number $K \in \mathbb{N}$. How can we test if $K$ is a square-pyramidal number, that is, $\exists n \in \mathbb{N}, P_n = K$? More specifically, I'm looking for a test that is made up of logical conjunctions of statements such as "$f_i(K)$ is a perfect $p_i$-th power" for some integer polynomials $f_1,...,f_r \in \mathbb{Z}[x]$ and powers $p_1,...,p_r \in \mathbb{N}$.

Some examples of sequences that admit such a test are:

  • The triangular numbers $T_n = \sum_{k=1}^n k = n(n+1)/2$. Given $K \in \mathbb{N}$, it is a triangular number if and only if $8K + 1$ is a perfect square. (This can be shown by considering the polynomial $x(x+1) - 2K$, which has an integer root iff the quadratic discriminant is an odd perfect square.)
  • The Fibonacci numbers $F_n = F_{n-1} + F_{n-2}$. Given $K \in \mathbb{N}$, it is a Fibonacci number if and only if [1]
    • $5K^2 + 4$ is a perfect square,
    • or $5K^2 - 4$ is a perfect square.

Is there such a test for the square-pyramidal numbers as well?

This is for curiosity. (It would probably be more efficient to use binary search for any algorithm actually implementing such a check.) Another way to phrase this question would be "what criteria must there be on the integer $K$ for the polynomial $x(x+1)(2x+1) - 6K$ to admit an integer root", and there doesn't seem to be an easy way to check if cubic equations have integer solutions in general. My attempts at trying to emulate the triangular-number method by using the cubic formula to come up with a list of critera didn't work, as it appears that sub-terms in the cubic formula will produce irrational and/or complex values even for integer polynomials.

virchau13
  • 435
  • You mean, $y^2=n(n+1)(2n+1)/6$ ? – Guruprasad Dec 04 '24 at 13:10
  • 3
    @Guruprasad No, the "square" in "square pyramidal" is just part of the name. – virchau13 Dec 04 '24 at 13:13
  • There is a lot of information that may be useful in the following OEIS: https://oeis.org/A000330 – user25406 Dec 05 '24 at 15:53
  • The square pyramidal numbers are given by adding numbers, two at a time, of the 4th diagonal of Pascal's triangle: $1,4,10,20,35,56,84...$. Adding just two at a time, you get: $0+1=1$, $1+4=5$, $4+10=14$,$10+20=30$,...etc. You may be able to put that into a formula using binomials. – user25406 Dec 05 '24 at 16:26
  • How about using a graph of the function $f(x)=x(x+1)(2x+1)/6$ and the line $y(x)=k$? I tried it on Desmos and it works fine. For large numbers one can use a log scale and then scale back the coordinates of the intersection point. – user25406 Dec 07 '24 at 12:20
  • The problem can in fact be reduced to solving a quadratic equation, so there is no need for solving a cubic. We transform the cubic equation into a quadratic in the following way: $n(n+1)(2n+1)/6=k$ becomes $m_{1}m_{2}=6k$ with $m_{1}=n^2+1$ and $m_{2}=2n+1$. So we know the product of the quadratic so we need to get the sum of the roots of the quadratic. The sum is $s=n^2+1 + 2n +1$. Let's work out the case of $n=3$ so $3(3+1)(23+1)=6k$ or $127=6*14=84$. (cont) – user25406 Dec 09 '24 at 18:58
  • (cont) So we can setup the quadratic as $x^2 -(12+7)x + 127 =0$ with roots of $7,12$. Given a number $N$, we need to multiply it by $6$ to get the product $m_{1}m_{2}$ then factor the number to extract the roots sum. So given $N=14$ we form $84=614=2327=347$. With this simple example, we see that $34=n(n+1)$ and $7=23+1$. So we can conclude that $N=14$ is a square pyramidal number. – user25406 Dec 09 '24 at 19:00

4 Answers4

9

Let $x = \lfloor (3 K)^{1/3} \rfloor$. The natural number $K$ is square pyramidal iff $K = x (x+1) (2x+1)/6$.

Proof: Note that $f(t) = t (t+1) (2t+1)/6$ is increasing on the nonnegative reals. If $z = (3 K)^{1/3}$, then $f(z) > z^3/3 = K$, while $f(z-1) = z^3/3 - z^2/2 + z/6 < K$. Thus the positive root of $f(t) = K$ must be between $z-1$ and $z$; it is an integer if and only if it is $\lfloor z \rfloor$.

Robert Israel
  • 470,583
  • 1
    This is interesting, but it's not quite what I'm looking for. I'm looking for a test which stays "within the integers" in some sense: a test that only uses integer operations and the operation "is-perfect-nth-power". (Or to show that such a test is impossible.) We know due to results like https://en.wikipedia.org/wiki/Casus_irreducibilis that computing all three roots of a cubic must involve complex numbers in some way. My curiosity is about how far we can get with a single-root cubic (the square pyramidal formula) while only sticking to integer arithmetic. – virchau13 Dec 04 '24 at 15:02
  • 2
    @virchau13 if your only concern is sticking with integer arithmetic, Newton's method can be used with just integer operations when it is a polynomial in question, with some care once the numbers get close to the desired root. I made a little program and put that in an answer – Will Jagy Dec 04 '24 at 19:10
7

Let's work this out in general. Suppose we wish to test whether $K = P(n)$, where $P(x)$ is a polynomial in $\mathbb{Q}[x]$. Assume there exists a polynomial $Q(x) \in \mathbb{Q}[x]$ and an integer $m \ge 2$ such that

$$ Q(K) \text{ is a perfect } m\text{th power } \iff \, K = P(n) \text{ for some } n\in \mathbb{Z}. $$

We will see that this places some very strong restrictions on $P(x)$.

From the $\impliedby$ direction, $Q(P(n))$ is a perfect $m$th power for all $n \in \mathbb{Z}$. By Hilbert's irreducibility theorem (or other methods), this implies that $$Q(P(x)) = R(x)^m$$ for some $R(x) \in \mathbb{Q}[x]$.

The left-hand side factors over $\mathbb{C}[x]$ into a product of factors of the form $(P(x) - \alpha_i)^{e_i}$ for each root $\alpha_i$ of $Q(x)$ with multiplicity $e_i$. These factors are pairwise relatively prime, so they must each be a perfect $m$th power in $\mathbb{C}[x]$.

We can assume that $e_i < m$ by dividing out any $m$th powers from $Q(x)$. The above then implies that $P(x) - \alpha_i$ is a perfect power (in $\mathbb{C}[x]$) for each $\alpha_i$. This implies that there are constants $a, b \in \mathbb{Q}$ such that $aP(x) + b$ is a perfect power in $\mathbb{Q}[x]$.

In other words, if there exists such a test that consists of checking whether a polynomial function of $K$ is a perfect $m$th power, then we can take the polynomial to be linear.

For the specific polynomial $P(n) = \dfrac{n(n+1)(2n+1)}{6}$, no such test exists since we there is no $a, b \in \mathbb{Q}$ such that $aP(n) + b$ is a perfect power. (In other words, "completing the cube" fails for this polynomial; the closest we can get is $24P(n)+1 = (2n+1)^3 -2n$.)


The above does not apply to the Fibonacci number test, both because $F_n$ is not a polynomial function of $n$ and also because it involves multiple polynomials. Could such a multiple-polynomial test exist for the square-pyramidal numbers?

Unfortunately this is not possible either, since at least one of the polynomial tests holds for a positive proportion of the $n \in \mathbb{N}$, which is enough for a stronger version of the Hilbert Irreducibility Theorem to apply, leading to the same conclusion.


Expanding on some fiddly details in response to the comments:

  • We can choose $a,b$ to be in $\mathbb{Q}$ (even though $\alpha_i$ is, a priori, a complex number) because $P(x)$ is in $\mathbb{Q}[x]$; the idea is that the $k$th root of a polynomial is unique up to a constant factor, and the $k$th root algorithm keeps the coefficients of the root in $\mathbb{Q}$, provided the leading coefficient is a $k$th power. In a little more detail: Suppose $P(x) - \alpha_i$ is a $k$th power in $\mathbb{C}[x]$. Then so is $aP(x)-a\alpha_i$, where $a$ is chosen so that the leading coefficient of $aP(x)$ is monic. Write $aP(x)-a\alpha_i = S(x)^k$ where $S(x)$ is monic, then solve for the remaining coefficients of $S(x)$. If you do this you'll see that all of the coefficients of $S(x)$ must be rational (so in fact $\alpha_i$ must be rational as well).
  • If $m$ divides all of the $e_i$, then $Q(x)$ is a constant times a $m$th power. In this case either $Q(K)$ is a perfect $m$th power for all $K$ (if the constant factor is a perfect $m$th power), or it is a perfect $m$th power for no $K$ (if the constant factor is not a perfect $m$th power), so here the test can only work in trivial situations (e.g. $P(n) = n$).
  • The "in other words" rephrasing above needs more justification. Here we prove the stronger statement that $Q(x)$ cannot have more than one root: If $Q(x)$ has two distinct roots $\alpha_1$ and $\alpha_2$, then from the above we have that $P(x)-\alpha_1 = S_1(x)^{k_1}$ and $P(x)-\alpha_2 = S_2(x)^{k_2}$ for some $S_1(x), S_2(x) \in \mathbb{C}[x]$ and $k_1, k_2 \ge 2$. Subtracting the two equations, we get $$S_1(x)^{k_1} - S_2(x)^{k_2} = \alpha_2-\alpha_1.$$ The Mason-Stothers theorem states, for $\mathbb{C}[x]$, that if $a, b,$ and $c$ are relatively prime polynomials (at least one of which is not constant) with $a+b=c$, then $$\max\{\deg{a},\deg{b},\deg{c}\} \le \mathrm{rad}(abc) -1 $$ where $\mathrm{rad}(f)$ is the number of distinct roots of $f$. In this case the left-hand side is $\deg{P}$ while the right-hand side is at most $$\frac{\deg{P}}{k_1} + \frac{\deg{P}}{k_2} - 1 \le \frac{\deg{P}}{2} + \frac{\deg{P}}{2} -1 = \deg{P} -1,$$ contradiction. So $Q(x)$ cannot have two distinct roots.
  • For the stronger version of the Hilbert Irreducibility Theorem mentioned above see e.g. this MathOverflow post. Here is a simplified statement for this setting:

    Let $f(X,Y)$ be an irreducible polynomial in $\mathbb{Q}[X,Y]$. Then for each $y_0 \in [-N,N]$, the specialization $f(X,y_0)$ is irreducible in $\mathbb{Q}[X]$ with at most $C\sqrt{N}\log{N}$ exceptions, for some constant $C$ depending on $f$.
Benjamin Wright
  • 2,825
  • 12
  • 15
  • 1
    (+1) Very nice answer! Just one small point I don't follow: if $\alpha_i$ is irrational, how do you guarantee that $a,b$ are in $\mathbb Q$? (I assume $\mathbb Q[x]$ is a typo.) Of course this isn't a serious issue, because the rest of your proof works even if $a,b$ are taken to be in $\mathbb C$ ($a \neq 0$). – Ravi Fernando Dec 05 '24 at 22:52
  • (+1) This is the answer I was looking for! Just some questions: (A) what if $m$ divides all $e_i$, resulting in the dividing out giving 1 = (R(x)/...)^m? (B) Doesn't this turn every such 'test-polynomial' Q(x) only into a combination of linear tests for each $P(x) - \alpha_i$? (Why do we only need one $a,b$ pair to represent all $P(x) - \alpha_i$ being $m$-th powers?) (C) What is the stronger version of Hilbert's irreducibility theorem that you allude to? Thanks for your answer! – virchau13 Dec 06 '24 at 04:32
  • @RaviFernando I did mean $\mathbb{Q}[x]$, but the jump from $\mathbb{C}[x]$ to $\mathbb{Q[x]}$ definitely needed a little more explanation, so I filled in some of the details. – Benjamin Wright Dec 07 '24 at 22:15
  • @virchau13 Edited to address these points. For (B), you're right that I only proved that at least one linear test is necessary (which was enough for this particular problem), but didn't fully prove that "we can take the polynomial to be linear." Added a proof that $Q(x)$ can in fact only have one distinct root. – Benjamin Wright Dec 07 '24 at 22:23
6

as far as finding whether a large positive integer is a cube, note that Newton's method can be implemented using integers only, with the caution that one must switch when consecutive numbers are too close together.

Added: since $x(x+1)(2x+1)$ and its first and second derivatives are positive for large $x,$ we could just as easily have written Newton's method for $x(x+1)(2x+1) - N.$ From Robert's answer there is little difference.

Added 2: it would have taken fewer steps to start with $b=1$ and keep doubling until $b^3 > N,$ then begin the Newton's section. Live and learn.

jagy@gost:~/Desktop/Cplusplus$ ./mse
Wed 04 Dec 2024 10:24:41 AM PST
  start  12345678910111213141516171819
  b  6172839455055606570758085909
  b  3086419727527803285379042954
  b  1543209863763901642689521477
  b  771604931881950821344760738
  b  385802465940975410672380369
  b  192901232970487705336190184
  b  96450616485243852668095092
  b  48225308242621926334047546
  b  24112654121310963167023773
  b  12056327060655481583511886
  b  6028163530327740791755943
  b  3014081765163870395877971
  b  1507040882581935197938985
  b  753520441290967598969492
  b  376760220645483799484746
  b  188380110322741899742373
  b  94190055161370949871186
  b  47095027580685474935593
  b  23547513790342737467796
  b  11773756895171368733898
  b  5886878447585684366949
  b  2943439223792842183474
  b  1471719611896421091737
  b  735859805948210545868
  b  367929902974105272934
  b  183964951487052636467
  b  91982475743526318233
  b  45991237871763159116
  b  22995618935881579558
  b  11497809467940789779
  b  5748904733970394889
  b  2874452366985197444
  b  1437226183492598722
  b  718613091746299361
  b  359306545873149680
  b  179653272936574840
  b  89826636468287420
  b  44913318234143710
  b  22456659117071855
  b  11228329558535927
  b  5614164779267963
  b  2807082389633981
  b  1403541194816990
  b  701770597408495
  b  350885298704247
  b  175442649352123
  b  87721324676061
  b  43860662338030
  b  21930331169015
  b  10965165584507
  b  5482582792253
  b  2741291396126
  b  1370645698063
  b  685322849031
  b  342661424515
  b  171330712257
  b  85665356128
  b  42832678064
  b  21416339032
  b  10708169516
  b  5354084758
  b  2677042379
  b  1338521189
 now too small,   double 
  b  2677042378  b^3  - n  6839494921382780778321222333
  b  2199860938  b^3  - n  -1699697962721519385425358147
  b  2375471770  b^3  - n  1058790283751934172918061181
  b  2281655058  b^3  - n  -467497208811883949703716707
  b  2326555338  b^3  - n  247638813637170495720702653
  b  2303680293  b^3  - n  -120179152912972066018058062
  b  2315003116  b^3  - n  60977062991519060658493077
  b  2309314149  b^3  - n  -30263959183383873860521870
  b  2312151608  b^3  - n  15187768055588131694375893
  b  2310731140  b^3  - n  -7579896718072547910627819
  b  2311440937  b^3  - n  3793433412808666902565134
  b  2311085930  b^3  - n  -1895843450959985712314819
  b  2311263406  b^3  - n  948135804644239436355597
  b  2311174661  b^3  - n  -474020592820554553605038
  b  2311219032  b^3  - n  237019916303949804628949
  b  2311196846  b^3  - n  -118511763392969233668083
  b  2311207939  b^3  - n  59253223240845186505200
  b  2311202392  b^3  - n  -29637495863958319551531
  b  2311205166  b^3  - n  14815822856954633342477
  b  2311203779  b^3  - n  -7410849842168391273680
  b  2311204472  b^3  - n  3694470673556660790229
  b  2311204126  b^3  - n  -1850177920004642059443
  b  2311204299  b^3  - n  922146169259908971080
  b  2311204212  b^3  - n  -472028424617472915691
  b  2311204256  b^3  - n  233071356868784637397
  b  2311204234  b^3  - n  -119478537230212686915
  b  2311204245  b^3  - n  56796408980318834306
  b  2311204239  b^3  - n  -39353561887979467900
  b  2311204240  b^3  - n  -23328566777931147819
  b  2311204241  b^3  - n  -7303571654015602298
  b  2311204242  b^3  - n  8721423483767168669
  b  2311204241  b^3  - n  -7303571654015602298
12345678902807641487500569521  b^3   
12345678910111213141516171819 start

jagy@gost:~/Desktop/Cplusplus$

$$ \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc $$

bound.set_str("12345678910111213141516171819",10) ;
a = 1;  b = bound;

cout << " start " << bound << endl;

while ( bbb > bound) { b /= 2; // cout << " b " << b << " b^3 - n " << bbb - bound << endl; cout << " b " << b << endl; }

cout << " now too small, double " << endl; b = 2; cout << " b " << b << " b^3 - n " << bb*b - bound << endl;

while( abs(a-b) > 10) { a=b; b = (bound + aaa) / ( 2aa);

cout << " b " << b << " b^3 - n " << bbb - bound << endl;

}

while( bbb < bound) { ++b; cout << " b " << b << " b^3 - n " << bbb - bound << endl; }

while( bbb > bound) { --b; cout << " b " << b << " b^3 - n " << bbb - bound << endl;

} cout << bbb << " b^3 " << endl; cout << bound << " start" << endl << endl;

$$ \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc \bigcirc $$

Will Jagy
  • 146,052
1

We can identify a square pyramidal number by comparing it with with an appropriately constructed cube.

  • Multiply the number to be tested by $24$, e.g.

$91×24=2184.$

  • Identify the cube root of this product, which must lie between an even number and the next higher odd one.

Per the OP's request, the cube root has to be taken using integer arithmetic. An algorithm is proposed basedbon Newton's Method for taking such a root.

  1. Start with $m=1$.
  2. Letting $N$ be the product obtained from the first step, compute $(2m+N)/3$ keeping only the integer part of the quotient
  3. Reset $m$ to this quotient and iterate until you reach an $m$ value greater thsn or equal to the previous one.
  4. The cube root will lie between the next-to-last $m$ value and the next higher integer.

For the cube root of $2184$ this method gives successive $m$ values:

$1,728,485,323,215,143,95,63,42,28,19,14,13,\color{blue}{12,13}$

where the $13$ following the $12$ means $12$ is the greatest whole number less than the cube root of $2184$:

$12<\sqrt[3]{2184}<13.$

The lower bound is even and the upper bound is odd as required for a square pyramidal number.

  • If the second test passes, subtract the odd number from its cube:

$13^3-13=2184.$

  • If the result from the preceding step reproduces the original product from the multiplication with $24$, the original number is square pyramidal:

$91×24=\color{blue}{2184}, 13^3-13=\color{blue}{2184}.$

  • If we get a match, the original number equals $n(n+1)(2n+1)/6$ with $2n+1$ equalling the odd cube root generated in this process:

$13=2×6+1\implies 91=(6×7×13)/6.$

The reader is invited to work put the algebra behind this claim. It's based on the observation that a square pyramidal number is the product of three consecutive whole numbers divided by $24$.

Oscar Lanzi
  • 48,208