Skip to content

Instantly share code, notes, and snippets.

@john9631
Created October 25, 2013 22:55
Show Gist options
  • Save john9631/7163058 to your computer and use it in GitHub Desktop.
Save john9631/7163058 to your computer and use it in GitHub Desktop.
{
"metadata": {
"language": "Julia",
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"module myfib\n",
"export fib\n",
"fib(n=1) = n <= 1 ? 1 : fib(n-1) + fib(n-2)\n",
"end\n",
"\n",
"using myfib\n",
"\n",
"function gemoize(f)\n",
"\n",
" function mem(x)\n",
" if !haskey(cache, x)\n",
" cache[x] = f(x)\n",
" end\n",
" println (cache)\n",
" return cache[x]\n",
" end\n",
" \n",
" cache = Dict()\n",
" return mem\n",
"end\n",
"\n",
"fib = gemoize(fib)\n",
"\n",
"@time fib(37)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Starting kernel event loops.\n",
"{"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"Warning: imported binding for fib overwritten in module Main\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"37=>39088169}\n",
"elapsed time: 0.382536409 seconds (5981088 bytes allocated)\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"39088169"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@time fib(37)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{37"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"=>39088169}\n",
"elapsed time: 0.000122689 seconds (10556 bytes allocated)\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"39088169"
]
}
],
"prompt_number": 2
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment