Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnadel committed Jan 22, 2016
0 parents commit 369ae6c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
30 changes: 30 additions & 0 deletions sum-of-squares.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#lang racket

;; Gets the square of a number x
(define (square x)
(* x x))
; (* 2 2 2)

; O(n)
(define (sum-of-squares-helper cur upper)
(if (> cur upper)
0
(+ (square cur) (sum-of-squares-helper (+ 1 cur) upper))))

;; Gets the sum of the square of every natural number below and including `upper-bound`
(define (sum-of-squares upper-bound)
(sum-of-squares-helper 0 upper-bound))

(sum-of-squares 1000000000000)

; O(n)
(define (sum-of-squares-tail-helper cur upper sum)
(if (> cur upper)
sum
(sum-of-squares-tail-helper (+ 1 cur) upper (+ sum (square cur)))))

;; Gets the sum of the square of every natural number below and including `upper-bound`
(define (sum-of-squares-tail upper-bound)
(sum-of-squares-tail-helper 0 upper-bound 0))

; (sum-of-squares-tail 2)
13 changes: 13 additions & 0 deletions sum-of-squares.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(define (square x)
(* x x))
;(* 2 2 2)

(define (sum-of-squares-helper cur upper)
(if (> cur upper)
0
(+ (square cur) (sum-of-squares-helper (+ 1 cur) upper))))

(define (sum-of-squares upper-bound)
(sum-of-squares-helper 0 upper-bound))

(sum-of-squares 5)

0 comments on commit 369ae6c

Please sign in to comment.