{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Quiz 2 : Batch 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the value of $\\pi$ upto $5$ decimal places by using the following formula.\n", "\n", "\\begin{equation}\n", " \\pi = \\sum_{n=0}^{\\infty} \\frac{2^{n+1}}{\\binom{2n}{n} (2n + 1)}\n", "\\end{equation}\n", "\n", "Compute the first 30 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": "code", "collapsed": false, "input": [ "def compute_pi(N) :\n", " s = 0.0\n", " two_power = 2.0\n", " binom2nn = 1.0\n", " nth_odd = 1.0\n", " for n in range(N) :\n", " s += two_power / binom2nn / nth_odd\n", " two_power *= 2\n", " binom2nn *= float((2*n + 1) * (2*n + 2)) / (n + 1)**2\n", " nth_odd += 2\n", " return s" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "val_of_pi = compute_pi(30)\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": {} } ] }