Sunday, April 19, 2015

HP Prime: Sums and Differences of Triangular Numbers, Triangular Numbers functions

HP Prime:  Sums and Differences of Triangular Numbers, Triangular Numbers functions

  


Benjamin Vitale, author of the Fun With Numb3rs blog (https://benvitalenum3ers.wordpress.com) wrote:

Consider the following system of equations:

T_a + T_b = T_c
T_a – T_b = T_d

Where T represents triangular numbers, which is defined by:

T_n = n * (n+1)/2 where n is a positive integer

(Retrieved April 16, 2015)

Then:
T_a + T_b = T_c
T_a – T_b = T_d

Let:
a*(a+1)/2 + b*(b+1)/2 = C
a*(a+1)/2 – b*(b+1)/2 = D

C and D need to be tested to determine if they are both triangular numbers, which is fairly easy to check.  Without loss of generality, let:

C = n*(n+1)/2
2*C = n*(n+1)
2*C = n^2 + n
0 = n^2 + n – 2*C

Solving for n:

n = (-1 ± √(1^2 – 4*-1*2*C))/2

Since we are looking integers where n>0, we can state that:

n = (-1 + √(1^2 – 4*-1*2*C))/2
n = (-1 + √(1 – 8*C))/2
n = -.5 + √((1 – 8*C)/4)
n = -.5 + √(.25 – 2*C)

If frac(n) = 0, we have a solution.   (frac stands the fraction function)

The program TRIINTEST will list pairs of triangular numbers.  The argument of TRIINTEST(n), where n is upper limit of T_n to be tested.
HP Prime Program:  TRIINTEST

EXPORT TRIINTTEST(u)
BEGIN
// 2014-04-18
LOCAL a,b,c,d;
LOCAL A,B,C,D;
// matrix starter
LOCAL m:=[[0,0]];
LOCAL s:=2;
FOR a FROM 2 TO u DO
FOR b FROM 1 TO a DO
A:=(a^2+a)/2;
B:=(b^2+b)/2;
C:=A+B;
D:=A-B;
c:=−.5+√(.25+2*C);
d:=−.5+√(.25+2*D);

IF FP(c)==0 AND FP(d)==0 THEN
ADDROW(m,[A,B],s);
s:=s+1;
END;

END;
END;

// clean up
DELROW(m,1);
RETURN m;
END;


Finding pairs from T_1 to T_50:

[[ 3, 3 ]
[ 21, 15 ]
[ 105, 105 ]
[ 171, 105 ]
[ 703, 378 ]
[ 990, 780 ]]


Here are the functions TRINUM and ITRINUM:

TRINUM(n):  returns the nth triangular number. 

ITRINUM(t):  test whether t is a triangular number.  If t is not a triangular number, -1 is returned.
Program TRINUM:

EXPORT TRINUM(N)
BEGIN
// triangular number
RETURN N*(N+1)/2;
END;


Program ITRINUM:

EXPORT ITRINUM(T)
BEGIN
// inverse triangular number
// returns −1 if not a triangular
// number
LOCAL t:=(−.5+√(.25+2*T));
IF FP(t)==0 THEN
RETURN t;
ELSE
RETURN −1;
END;
END;

Examples:

TRINUM(15) = 120  (15th triangular number)

ITRINUM(276) = 23   (276 is the 23rd triangular number)
ITRINUM(572) returns -1   (572 is not a triangular number)


This blog is property of Edward Shore.  2015

RPN with HP 15C & DM32: Swapping Items in the Stack

RPN with HP 15C & DM32: Swapping Items in the Stack A New Series Welcome to a new series: RPN with the HP 15C & DM32. ...