From 031ba2f4bb7729a16f111192d83992291cb37401 Mon Sep 17 00:00:00 2001 From: pezy Date: Fri, 8 May 2015 18:41:23 +0800 Subject: [PATCH] Finished Chapter 1 --- Foundations/overview/README.md | 2 + Foundations/overview/calc.py | 109 ++++++++++++++++++++++++++++++ Foundations/overview/calc_time.py | 11 --- 3 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 Foundations/overview/calc.py delete mode 100644 Foundations/overview/calc_time.py diff --git a/Foundations/overview/README.md b/Foundations/overview/README.md index 4c7fc08..6e748db 100644 --- a/Foundations/overview/README.md +++ b/Foundations/overview/README.md @@ -70,3 +70,5 @@ for n in range(1, 100): ### Problems 1-1 > 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. + +check and run [calc.py](calc.py) diff --git a/Foundations/overview/calc.py b/Foundations/overview/calc.py new file mode 100644 index 0000000..6cf5c54 --- /dev/null +++ b/Foundations/overview/calc.py @@ -0,0 +1,109 @@ +import math +from decimal import * + +sec = 1000000 +min = 60 * sec +hour = 60 * min +day = 24 * hour +month = 30 * day +year = 365 * day +century = 100 * year + +# n * lg(n) == c +# ==> 2 ** (lg(n)) * lg (n) == c +# ==> lg(n) == W(c) +# ==> n == exp(W(c)) +# see http://en.wikipedia.org/wiki/Lambert_W_function#Example_2 + +def nlgn(c): + lower = 0.0 + upper = 10e13 + while True: + middle = (lower + upper) / 2 + if lower == middle or middle == upper: + return middle + if middle * math.log(middle, 2) > c: + upper = middle + else: + lower = middle + +# n! +def defactorial(c): + n = 0 + while True: + if math.factorial(n) >= c: + return n - 1; + else: + n += 1 + +# lg(n) +print("2 ^ 10 ^ 6") +print("2 ^ ( %d * 10 ^ 6 )" % (min/sec)) +print("2 ^ ( %d * 10 ^ 6 )" % (hour/sec)) +print("2 ^ ( %d * 10 ^ 6 )" % (day/sec)) +print("2 ^ ( %d * 10 ^ 6 )" % (month/sec)) +print("2 ^ ( %d * 10 ^ 6 )" % (year/sec)) +print("2 ^ ( %d * 10 ^ 6 )" % (century/sec)) +print("") +# sqrt(n) +print("{:.1e}".format(Decimal(sec ** 2))) +print("{:.1e}".format(Decimal(min ** 2))) +print("{:.3e}".format(Decimal(hour ** 2))) +print("{:.5e}".format(Decimal(day ** 2))) +print("{:.6e}".format(Decimal(month ** 2))) +print("{:.9e}".format(Decimal(year ** 2))) +print("{:.9e}".format(Decimal(century ** 2))) +print("") +# n +print("{:.1e}".format(Decimal(sec))) +print("{:.1e}".format(Decimal(min))) +print("{:.1e}".format(Decimal(hour))) +print("{:.2e}".format(Decimal(day))) +print("{:.3e}".format(Decimal(month))) +print("{:.4e}".format(Decimal(year))) +print("{:.4e}".format(Decimal(century))) +print("") +# n lg(n) +print(nlgn(sec)) +print(nlgn(min)) +print(nlgn(hour)) +print(nlgn(day)) +print(nlgn(month)) +print(nlgn(year)) +print(nlgn(century)) +print("") +# n^2 +print(math.sqrt(sec)) +print(math.sqrt(min)) +print(math.sqrt(hour)) +print(math.sqrt(day)) +print(math.sqrt(month)) +print(math.sqrt(year)) +print(math.sqrt(century)) +print("") +# n^3 +print(sec ** (1/3.0)) +print(min ** (1/3.0)) +print(hour ** (1/3.0)) +print(day ** (1/3.0)) +print(month ** (1/3.0)) +print(year ** (1/3.0)) +print(century ** (1/3.0)) +print("") +# 2^n +print(math.log(sec, 2)) +print(math.log(min, 2)) +print(math.log(hour, 2)) +print(math.log(day, 2)) +print(math.log(month, 2)) +print(math.log(year, 2)) +print(math.log(century, 2)) +print("") +# n! +print(defactorial(sec)) +print(defactorial(min)) +print(defactorial(hour)) +print(defactorial(day)) +print(defactorial(month)) +print(defactorial(year)) +print(defactorial(century)) diff --git a/Foundations/overview/calc_time.py b/Foundations/overview/calc_time.py deleted file mode 100644 index 48223d9..0000000 --- a/Foundations/overview/calc_time.py +++ /dev/null @@ -1,11 +0,0 @@ -import math - -sec = 1000000 -min = 60 * sec -hour = 60 * min -day = 24 * hour -month = 30 * day -year = 365 * day -century = 100 * year - -print("%3e" % 2 ** sec)