learn go memoization

package main

// 参考文章:
//     https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.12.md

import (
    "fmt"
    "time"
)

const LIM = 41

var fibs [LIM]uint64

func main() {
    var result uint64 = 0
    start := time.Now()
    for i := 0; i < LIM; i++ {
        result = fibonacci(i)
        fmt.Printf("fibonacci(%d) is: %d\n", i, result)
    }

    end := time.Now()
    delta := end.Sub(start)
    fmt.Printf("longCalculation took this amount of time:%s\n", delta)
}

func fibonacci(n int) (res uint64) {
    // memoization: check if fibonacci(n) is already known in array
    if fibs[n] != 0 {
        res = fibs[n]
        return
    }
    if n <= 1 {
        res = 1
    } else {
        res = fibonacci(n - 1) + fibonacci(n - 2)
    }

    fibs[n] = res
    return
}
时间: 2024-10-11 18:01:35

learn go memoization的相关文章

【Machine Learn】决策树案例:基于python的商品购买能力预测系统

决策树在商品购买能力预测案例中的算法实现 作者:白宁超 2016年12月24日22:05:42 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结合视频学习和书籍基础的笔记所得.本系列文章将采用理论结合实践方式编写.首先介绍机器学习和深度学习的范畴,然后介绍关于训练集.测试集等介绍.接着分别介绍机器学习常用算法,分别是监督学习之分类(决策树.临近取样.支持向量机.神经网络算法)监督学习之回归(线性回归.非线性回归)非监督学习(

TF.Learn

TF.Learn 手写文字识别 转载请注明作者:梦里风林Google Machine Learning Recipes 7官方中文博客 - 视频地址Github工程地址 https://github.com/ahangchen/GoogleML欢迎Star,也欢迎到Issue区讨论 mnist问题 计算机视觉领域的Hello world 给定55000个图片,处理成28*28的二维矩阵,矩阵中每个值表示一个像素点的灰度,作为feature 给定每张图片对应的字符,作为label,总共有10个la

Is it too late to learn to code?

Erin Parker, Founder Spitfire Athlete, iOS Engineer 9k upvotes by Francis Chen, Gaurav Baheti, Yue-Wing Yau, Maria Guryanova,(more) It's never too late. So much can happen in a year, it can amaze you. I majored in Economics. When I was about 23, I ra

Learn Python the Hard Way--Exercise 46

0. 缘起 <Learn Python the Hard Way>Exercise 46 要求安装四个python package pip, distribute, nose, virtualenv,(原书作者特别提醒: Do not just donwload these packages and install them by hand. Instead see how other people recommend you install these packages and use th

教程 1:让我们通过例子来学习(Tutorial 1: Let’s learn by example)

通过这第一个教程,我们将引导您从基础完成创建简单的带有注册表单的应用. 我们也将解释框架行为的基本方面.如果您对Phalcon的自动代码生成工具有兴趣, 您可以查看 developer tools. 确认安装(Checking your installation)? We'll assume you have Phalcon installed already. Check your phpinfo() output for a section referencing "Phalcon"

Learn Python From &#39;Head First Python&#39; [3](2) : Pickle

1.the use of 'with open... as ...' 2.the use of pickle(dump and load) for Step1: the 'with open ... as...' is the short format of 'try...except...finally' for Step2: you can store a list with pickle.dump() and get the content again with pickle.load()

javascript memoization递归优化

memoize优化递归 function createRec(callback, cache) { cache = cache || []; var rec = function(n) { (n in cache) || (cache[n] = callback(rec, n)); return cache[n]; } return rec; } 以Fibonacci数列为例子,如何创建一个优化的递归 var fib = createRec(function(cal, n) { return c

Learn Docker

Learn Docker A Container is to VM today, what VM was to Physical Servers a while ago. The workload seems to shifting towards containers, and fast! In case you haven’t started ramping on it yet, you may find it a bit overwhelming to begin with. I thou

Java-集合(没做出来)第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn reverseL

没做出来 第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列. 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn reverseList(list); //调用reverseList 方法之后,list 为Learn World Hello package