Skip to content

Instantly share code, notes, and snippets.

@loosechainsaw
Created August 7, 2013 10:27
Show Gist options
  • Save loosechainsaw/6172893 to your computer and use it in GitHub Desktop.
Save loosechainsaw/6172893 to your computer and use it in GitHub Desktop.
Using triangles to calculate perfect squares in Racket
#lang racket
(define (get-triangle row)
(cond
( ( = row 0) 0)
( ( = row 1) 1)
( else (+ row (get-triangle (sub1 row)) ))))
(define (nth-perfect-square count)
(cond
( (= count 1 ) 1)
( else (+ (get-triangle (sub1 count)) (get-triangle count)) )))
(get-triangle 1)
(get-triangle 2)
(get-triangle 3)
(nth-perfect-square 1)
(nth-perfect-square 2)
(nth-perfect-square 3)
(nth-perfect-square 4)
(nth-perfect-square 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment