Square numbers are those that take the form of \$n^2\$ where \$n\$ is an integer. These are also called perfect squares, because when you take their square root you get an integer.
The first 10 square numbers are: (OEIS)
0, 1, 4, 9, 16, 25, 36, 49, 64, 81
Triangular numbers are numbers that can form an equilateral triangle. The n-th triangle number is equal to the sum of all natural numbers from 1 to n.
The first 10 triangular numbers are: (OEIS)
0, 1, 3, 6, 10, 15, 21, 28, 36, 45
Square triangular numbers are numbers that are both square and triangular.
The first 10 square triangular numbers are: (OEIS)
0, 1, 36, 1225, 41616, 1413721, 48024900, 1631432881, 55420693056, 1882672131025, 63955431761796
There is an infinite number of square numbers, triangle numbers, and square triangular numbers.
Write a program or named function that given an input (parameter or stdin) number \$n\$, calculates the \$n\$th square triangular number and outputs/returns it, where n is a positive nonzero number. (For \$n=1\$ return 0)
For the program/function to be a valid submission it should be able to return at least all square triangle numbers smaller than \$2^{31}-1\$.
Bonus
-4 bytes for being able to output all square triangular numbers less than 2^63-1
-4 bytes for being able to theoretically output square triangular numbers of any size.
+8 byte penalty for solutions that take nonpolynomial time.
Bonuses stack.
This is code-golf challenge, so the answer with the fewest bytes wins.
n
steps, and in each step the arithmetic takes linear time because the number of digits grows linearly inn
. I don't think linear time is possible. Unless you're saying arithmetic operations are constant time? \$\endgroup\$