summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--notes.md18
-rw-r--r--problems/1-1/main.go39
2 files changed, 57 insertions, 0 deletions
diff --git a/notes.md b/notes.md
index 3b94338..d92f8ed 100644
--- a/notes.md
+++ b/notes.md
@@ -201,3 +201,21 @@ same machine?
14,19600,16384
15,22500,32768
```
+
+Problem 1-1: Comparison of running times
+ For each function `f(n)` and time `t` in the following table, determine the
+ largest size `n` of a problem that can be solved in time `t`, assuming that
+ the algorithm to solve the problem takes `f(n)` microseconds.
+
+
+ ```plaintext
+ | | 1 second | 1 minute | 1 hour | 1 day |
+ | lg n | | | | |
+ | sqrt(n) | | | | |
+ | n | | | | |
+ | nlg(n) | | | | |
+ | n^2 | | | | |
+ | n^3 | | | | |
+ | 2^n | | | | |
+ | n! | | | | |
+ ```
diff --git a/problems/1-1/main.go b/problems/1-1/main.go
new file mode 100644
index 0000000..e867d16
--- /dev/null
+++ b/problems/1-1/main.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+func main() {
+ // 1 second = 1,000,000 microseconds
+ // 1 minute = 60 seconds = 60,000,000 microseconds
+ // 1 hour = 60 minutes = 3600 seconds = 3,600,000,000 microseconds
+ // 1 day = 24 hours = 1440 mins = 86400 seconds = 86,400,000,000 microseconds
+
+ // lg(n)
+ results := [4]float64{0, 0, 0, 0}
+ i := 0
+ for n := 1.0; ; n++ {
+ x := math.Log2(n)
+
+ if i == 0 && x > 1_000_000 {
+ results[i] = x
+ i++
+ }
+ if i == 1 && x > 60_000_000 {
+ results[i] = x
+ i++
+ }
+ if i == 2 && x > 3_600_000_000 {
+ results[i] = x
+ i++
+ }
+ if i == 3 && x > 86_400_000_000 {
+ results[i] = x
+ break
+ }
+ }
+
+ fmt.Printf("lg(n)|%v|%v|%v|%v|\n", results[0], results[1], results[2], results[3])
+}