From 5419f9c6ff5cb23f8ee07d786b31b93e4253e565 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 6 Sep 2021 18:52:54 -0600 Subject: solve for smallest n --- exercises/1.2-3/main.go | 21 +++++++++++++++++++++ notes.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 exercises/1.2-3/main.go diff --git a/exercises/1.2-3/main.go b/exercises/1.2-3/main.go new file mode 100644 index 0000000..8abbbb9 --- /dev/null +++ b/exercises/1.2-3/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + fmt.Println("n,100n^2,2^n") + + for n := 1.0; n < 100; n++ { + x := 100 * math.Pow(n, 2) + y := math.Pow(2, n) + + fmt.Printf("%v,%v,%v\n", n, x, y) + + if x < y { + break + } + } +} diff --git a/notes.md b/notes.md index f21eb7e..3b94338 100644 --- a/notes.md +++ b/notes.md @@ -159,3 +159,45 @@ insertion sort beat merge sort? 43,14792,14933.080604940173 44,15488,15373.759438082629 ``` + +1.2-3: What is the smallest value of `n` such that an algorithm whose running +time is 100n^2 runs faster than an algorithm whose running time is 2^n on the +same machine? + + 15. Calculated using: + + ```golang + func main() { + fmt.Println("n,100n^2,2^n") + + for n := 1.0; n < 100; n++ { + x := 100 * math.Pow(n, 2) + y := math.Pow(2, n) + + fmt.Printf("%v,%v,%v\n", n, x, y) + + if x < y { + break + } + } + } + ``` + + ```csv + n,100n^2,2^n + 1,100,2 + 2,400,4 + 3,900,8 + 4,1600,16 + 5,2500,32 + 6,3600,64 + 7,4900,128 + 8,6400,256 + 9,8100,512 + 10,10000,1024 + 11,12100,2048 + 12,14400,4096 + 13,16900,8192 + 14,19600,16384 + 15,22500,32768 + ``` -- cgit v1.2.3