{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import division" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "n = int(0)\n", "\n", "float_type = np.float32\n", "\n", "my_sum = float_type(0)\n", "\n", "while True:\n", " n += 1\n", " last_sum = my_sum\n", " my_sum += float_type(1 / n)\n", " \n", " if n % 200000 == 0:\n", " print \"1/n = %g, sum0 = %g\"%(1.0/n, my_sum)\n", " \n", " if last_sum == my_sum:\n", " print \"Stopped changing at %d\" % n\n", " break" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1/n = 5e-06, sum0 = 12.7828\n", "1/n = 2.5e-06, sum0 = 13.4814" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1/n = 1.66667e-06, sum0 = 13.8814" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1/n = 1.25e-06, sum0 = 14.1666" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1/n = 1e-06, sum0 = 14.3574" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1/n = 8.33333e-07, sum0 = 14.5481" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1/n = 7.14286e-07, sum0 = 14.7388" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1/n = 6.25e-07, sum0 = 14.9296" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1/n = 5.55556e-07, sum0 = 15.1203" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1/n = 5e-07, sum0 = 15.311" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Stopped changing at 2097152" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 } ], "metadata": {} } ] }