{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Quiz 2 : Batch 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the value of $\\pi$ upto $5$ decimal places by using the following formula.\n", "\n", "\\begin{equation}\n", " \\frac{1}{\\pi} = \\frac{2\\sqrt{2}}{9801} \\sum_{k=0}^{\\infty} \\frac{(4k)! (1103 + 26390k)}{(k!)^4 396^{4k}}\n", "\\end{equation}\n", "\n", "Compute the first $20$ terms of this series to compute $\\pi$ and print the answer upto 5 decimal places." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Grading scheme.\n", "* Total points : 10 marks\n", "* -5 if any kind of loop (while or for) is not used to compute the sum\n", "* -2 if the logic is correct but the code is not running due to syntax error\n", "* -1 is not upto the specification of 5 decimal spaces." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Sample solution" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "def compute_inv_pi(N) :\n", " Const = 2.0 * math.sqrt(2) / 9801\n", " s = 0.0\n", " fact4k = 1.0\n", " factk_to_4 = 1.0\n", " inv_power_396_4k = 1.0 \n", " linterm = 1103.0 # 1103 + 26390k\n", " for k in range(N) :\n", " s += (fact4k * linterm * inv_power_396_4k) / factk_to_4\n", " fact4k *= (4*k + 1) * (4*k + 2) * (4*k + 3) * (4*k + 4)\n", " factk_to_4 *= (k + 1)**4\n", " inv_power_396_4k /= 396**4\n", " linterm += 26390\n", " inv_pi = Const * s\n", " return inv_pi" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "val_of_pi = 1.0 / compute_inv_pi(20)\n", "print \"The value of pi is %7.5f.\" % val_of_pi" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of pi is 3.14159.\n" ] } ], "prompt_number": 2 } ], "metadata": {} } ] }