From 369ae6cd85173c1b4c2c2d4acd50c91468ae5b94 Mon Sep 17 00:00:00 2001 From: AJ Date: Fri, 22 Jan 2016 10:08:21 -0800 Subject: [PATCH] initial commit --- sum-of-squares.rkt | 30 ++++++++++++++++++++++++++++++ sum-of-squares.scm | 13 +++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 sum-of-squares.rkt create mode 100644 sum-of-squares.scm diff --git a/sum-of-squares.rkt b/sum-of-squares.rkt new file mode 100644 index 0000000..d0bf853 --- /dev/null +++ b/sum-of-squares.rkt @@ -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) \ No newline at end of file diff --git a/sum-of-squares.scm b/sum-of-squares.scm new file mode 100644 index 0000000..f929da2 --- /dev/null +++ b/sum-of-squares.scm @@ -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) \ No newline at end of file