-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 369ae6c
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) |
This file contains 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
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) |