Skip to content

Instantly share code, notes, and snippets.

@Tsutomu-KKE
Created July 4, 2014 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tsutomu-KKE/ccd3687577cd72a5213f to your computer and use it in GitHub Desktop.
Save Tsutomu-KKE/ccd3687577cd72a5213f to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:282a4b9ddbb7c56b5094b146b446c4b153427d8bec965febc8bce17917019691"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#\u4e8c\u5206\u63a2\u7d22"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def bi_search(n):\n",
" low, high = 0, n\n",
" while high - low > 1e-8:\n",
" x = (low + high) / 2\n",
" if x**2 < n:\n",
" low = x\n",
" else:\n",
" high = x\n",
" return (low + high) / 2"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%time bi_search(2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Wall time: 0 ns\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"1.414213564246893"
]
}
],
"prompt_number": 13
}
],
"metadata": {}
}
]
}
@Tsutomu-KKE
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment