# This is a Python program which will output a list of
# triangle-square numbers. It can be run online using:
#
# https://www.codechef.com/ide
#
# Paste the code below the line into the box and select
# Python as the language. (PYTH 3 formats the output
# properly, PYTH works but has quotes in the output.)
#
# Triangular-Square Numbers
# Change maxn to find more or less results
# On codechef 15 million is about the highest you can use
# when it's in a good mood. If you get SIGSTOP then try
# lowering maxn
import math
number = 0
maxn = 5000000 # maximum value of i
print("Here are the triangular-square numbers for n up to", maxn)
for i in range(1,maxn):
number = number + i # Find each triangular number
sr = math.sqrt(number)
if sr.is_integer(): # See if its square root is an integer
print(number, "for n=", i) # If it is also a square then print
-----------------------------------------------------------------
;;;; This is a LISP program which will output a list of
;;;; triangle-square numbers. It can be run online using:
;;;; https://www.tutorialspoint.com/execute_lisp_online.php
;;;; It also runs as CLISP (but not LISP sbcl) on the site:
;;;; https://www.codechef.com/ide
;;;; In both cases the highest it will find is for i=1681
;;;; Above that it will end with an overflow or sigstop error
;;;; Set defvar m to the highest value you want to try for
;;;; If it ends without error it will print Done!
;; This function recursively finds each triangular number
(defun triangle (n)
(if (= n 1)
1
(+ n (triangle (- n 1))) )
)
(defvar m 2000) ;; set maximum m here
(format t "Here are the triangle-square numbers up to i= ~D ~%" m)
(format t "i indicates ith triangular number, t is the number ~%~%")
(loop for i from 1 to m
do
(if (= (sqrt(triangle i)) (floor (sqrt(triangle i))) ) ;; check to see if square root is an integer
(format t "i = ~D; t = ~D sqrt(t) = ~D~%~%" i (triangle i) (sqrt (triangle i)))
))
(format t "Done ! ~%")