Created
August 7, 2013 10:27
-
-
Save loosechainsaw/6172893 to your computer and use it in GitHub Desktop.
Using triangles to calculate perfect squares in Racket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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