{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Quiz 2 : Batch 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the value of $\\pi$ by computing the first $20$ terms of the series\n", "\n", "\\begin{equation}\n", "\\sqrt{12} \\sum_{k=0}^{\\infty} \\left(- \\frac{1}{3}\\right)^k \\frac{1}{2k+1}\n", "\\end{equation}\n", "\n", "and print the answer to the first 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", "\n", "def compute_pi(N) :\n", " Const = math.sqrt(12)\n", " power_of_third = 1.0\n", " ith_odd = 1.0\n", " sum_for_pi = 0\n", " for i in range(N+1) :\n", " sum_for_pi += power_of_third / ith_odd\n", " power_of_third *= - 1.0 / 3.0\n", " ith_odd += 2\n", " val_of_pi = Const * sum_for_pi\n", " return val_of_pi" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The value of pi is %7.5f.\" % compute_pi(20)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of pi is 3.14159.\n" ] } ], "prompt_number": 2 } ], "metadata": {} } ] }