summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2021-09-06 18:52:54 -0600
committermo khan <mo@mokhan.ca>2021-09-06 18:52:54 -0600
commit5419f9c6ff5cb23f8ee07d786b31b93e4253e565 (patch)
tree8c3a9b38001a2ad5f8bf8444a2d474ce2a660981
parent18618f7bf9ee47c1eb7605d17dbe7156f518067a (diff)
solve for smallest n
-rw-r--r--exercises/1.2-3/main.go21
-rw-r--r--notes.md42
2 files changed, 63 insertions, 0 deletions
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
+ ```