From 465dfcb4b534bd45f162f98663ac317d85d7e39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 7 Nov 2018 15:22:04 +0100 Subject: [PATCH] exercises for week 3 --- ex_09.py | 18 +++++++++++ ex_10.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ex_11.py | 28 +++++++++++++++++ ex_12.py | 14 +++++++++ 4 files changed, 156 insertions(+) create mode 100644 ex_09.py create mode 100644 ex_10.py create mode 100644 ex_11.py create mode 100644 ex_12.py diff --git a/ex_09.py b/ex_09.py new file mode 100644 index 0000000..5013951 --- /dev/null +++ b/ex_09.py @@ -0,0 +1,18 @@ +#!/usr/bin/python3 +from math import gamma + +def factorial(n): + n = int(n) + result = 1 + if(n < 0): + raise ValueError("n must be greater than 0") + if(n == 0): + return result + for i in range(1, n + 1): + result *= i + return result + +if( __name__ == "__main__"): + print("factorial(5) = ", factorial(5)) + + print("gamma(6) = ", gamma(6)) diff --git a/ex_10.py b/ex_10.py new file mode 100644 index 0000000..3383d35 --- /dev/null +++ b/ex_10.py @@ -0,0 +1,96 @@ +#!/usr/bin/python3 + +from collections import deque +from itertools import count +import decimal + +# Only for reference. +def fibonacci(n): + a = 1 + b = 0 + swp = 0 + + for i in range(n): + swp = a + a += b + b = swp + return a + +_fibonacci_series_context_stack = deque() + +class FibonacciSeriesContext(object): + def __init__(self): + self._pre_calculated = {0: 1, 1: 1} + self._biggest = 1 + + def __contains__(self, other): + return other in self._pre_calculated + + def __getitem__(self, n): + return self._pre_calculated[n] + def __setitem__(self, n, v): + self._pre_calculated[n] = v + + def get_biggest_pair(self): + return ((self._biggest - 1, self._pre_calculated[self._biggest - 1]) + , (self._biggest, self._pre_calculated[self._biggest])) + def __enter__(self): + _fibonacci_series_context_stack.append(self) + return self + def __exit__(self, *exc): + _fibonacci_series_context_stack.pop() + return False + +_fibonacci_series_context_stack.append(FibonacciSeriesContext()) + + +def getfibonacciseriescontext(): + return _fibonacci_series_context_stack[-1] + +def fast_ctx_fibonacci(n, context = getfibonacciseriescontext()): + if(n in context): + return context[n] + + (n_start_minus_one, b), (n_start, a) = context.get_biggest_pair() + + for i in range(n_start, n): + swp = a + a += b + b = swp + context[n] = a + + return a + + + +if( __name__ == "__main__"): + + decimal.getcontext().prec = 5 + golden_ratio = decimal.Decimal((1 + decimal.Decimal(5).sqrt()) / 2) + print("Seeking n, such that F(n + 1) / F(n) = ", golden_ratio) + print("Precision is 1e-5.") + + # Try to approximate the result faster using big steps first. + + # At first try steps of 100. This way we can approximate an interval + # with width 100 where we do the fine approximation. + + for i in count(1, 100): + if(fast_ctx_fibonacci(i) / decimal.Decimal(fast_ctx_fibonacci(i - 1)) == golden_ratio): + break + + n_stop = i + n_start = n_stop - 101 + + print("Found that n is in range({}, {})".format(n_start, n_stop)) + + for i in range(n_start, n_stop): + if(fast_ctx_fibonacci(i) / decimal.Decimal(fast_ctx_fibonacci(i - 1)) == golden_ratio): + break + n = i + 1 + + print("n = ", n) + + print("F(n + 1) / F(n) = %0.6g" % (fast_ctx_fibonacci(n + 1) / fast_ctx_fibonacci(n))) + + diff --git a/ex_11.py b/ex_11.py new file mode 100644 index 0000000..f11d04b --- /dev/null +++ b/ex_11.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 + +import pprint + +from util.io import readvalue + + +def divisors(n): + return [i for i in range(2, n) if not n % i] + +def is_prime(n): + return not divisors(n) + +if( __name__ == "__main__"): + + def positive_int(s): + v = int(s) + if(v > 0): + return v + + number = readvalue("n > ", positive_int) + print("Divisors:") + pprint.pprint(divisors(number)) + + if(is_prime(number)): + print("Also", number, "is a prime.") + else: + print(number, "is not a prime.") diff --git a/ex_12.py b/ex_12.py new file mode 100644 index 0000000..0a4cb2e --- /dev/null +++ b/ex_12.py @@ -0,0 +1,14 @@ +#!/usr/bin/python3 + +def numerical_shitty_integration(func, n, a, b): + epsilon = (b - a) / n + result = 0 + + for i in range(n): + result += epsilon * func( a + epsilon * i) + return result + +if( __name__ == "__main__"): + f = lambda x: (1 - x**2)**0.5 + + print(numerical_shitty_integration(f, 5000, -1, 1))