Function to produce the series of square triangular numbers:
R(n) = 2 * R(n-1) + R(n-2) | R(0) = 0, R(1) = 1
ST(n) = (R(n) * R(n) + R(n-1))^2
Java code to calculate the nth square triangular number:
public static BigInteger zero = BigInteger.ZERO; public static BigInteger one = BigInteger.ONE; public static BigInteger two = BigInteger.valueOf(2); public static BigInteger ST(BigInteger n) { BigInteger m = Pell(n); BigInteger m1 = Pell(n.subtract(one)); BigInteger a = m; BigInteger b = m.add(m1); BigInteger product = a.multiply(b); return product.multiply(product); } /* Returns the Pell number of index n / public static BigInteger Pell(BigInteger n) { if (lt(n, zero)) { throw new ArithmeticException("Undefined"); } if (n.equals(zero)) { return zero; } if (n.equals(one)) { return one; } BigInteger m1 = Pell(n.subtract(one)); BigInteger m2 = Pell(n.subtract(two)); return two.multiply(m1).add(m2); } public static boolean lt(BigInteger i, BigInteger i2) { return i.compareTo(i2) < 0; }
Code to calculate Pell's numbers and square triangular numbers using derived formulas and sqrt(2):
static BigDecimal rootTwo = new BigDecimal("1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571");static BigInteger ST(int n) { BigDecimal one = BigDecimal.ONE; BigDecimal four = BigDecimal.valueOf(4); int _2n = 2n; BigDecimal z = rootTwo; / (1 + z)^(2n) - (1 - z)^(2n) / BigDecimal numerator = one.add(z).pow(_2n).subtract(one.subtract(z).pow(_2n)); / (4 * z)^2 */ BigDecimal denominator = four.multiply(z); BigDecimal quotient = numerator.divide(denominator, RoundingMode.CEILING); BigInteger value = quotient.pow(2, new MathContext(z.precision() - 20)).toBigInteger(); return value; }static BigInteger Pell(int n) { BigDecimal one = BigDecimal.ONE; BigDecimal two = BigDecimal.valueOf(2); BigDecimal z = rootTwo; BigDecimal numerator = (one.add(z).pow(n)) .subtract (one.subtract(z).pow(n)); BigDecimal denominator = two.multiply(z); BigDecimal quotient = numerator.divide(denominator, RoundingMode.CEILING); return quotient.pow(1, new MathContext(z.precision() - 20)).toBigInteger(); }
Pell(3) = 5
Pell(5) = 29
5 * 29 = 145 = 12^2 + 1
Pell(4) = 12
Pell(11) = 5741
Pell(13) = 33461
5741 * 33461 = 192099601 = 13860^2 + 1
Pell(12) = 13860
…
floor_sqrt(Pell(n) * Pell(n+2)) = Pell(n+1)