www.fatihkabakci.com

Personal Website and Computer Science TUR EN

fibonacci recursive memorization

Last update: 9/26/2016 11:42:00 PM

By Fatih KABAKCI

The famous popular numbers which are known as Fibonacci Numbers in Computer Science can be calculated faster than used usual methods until somewhere in Fibonacci series. Over and over, the recalculation of numbers in usual methods takes long time. However, the idea that saving some small numbers in a memory during calculation process avoids recalculation in each fibonacci recursive method invoking. This technic is called as memorizing. In this method, each fibonacci value is calculated once, and then it is saved in memory. After that, whenever need to use those values, they are not calculated on CPU again and again, basically they are get from storage. Thus, latency is avoided and program runs faster.

For instance, in a test scenario

it has been seen that while usual fibonacci recursive method has returned Fibonacci(54) = 86267571272 in approximately 20 minutes in a 64-bit computer, that value have been get in miliseconds level in the same machine by using memorizing method

This technic can be implemented in Java as below.

import java.util.HashMap;

public class FibonacciMemorization {
    /**
     * @author fkabakci 
     * Java recursive fibonacci implementation using memorization method
     */
    private static HashMap<Long, Long> fiboMap = new HashMap<Long, Long>();

    /**
     * 
     * @param k
     * @return
     */
    public static long fibonacci(long k) {
        if (k == 0 || k == 1) {
            return k;
        }

        try {
            // do not calculate anymore, just return it
            return fiboMap.get(k);
        } catch (NullPointerException np) {
            // has to be calculated once, then save it
            // calculation part
            long value = fibonacci(k - 1) + fibonacci(k - 2);
            // saving part
            fiboMap.put(k, value);
            return value;
        }
    }

    public static void main(String[] args) {
        for (long i = 0; i < 100; i++)
            System.out.println(i + " " + fibonacci(i));
    }
}

The following output shows up in miliseconds.

0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
11 89
12 144
13 233
14 377
15 610
16 987
17 1597
18 2584
19 4181
20 6765
21 10946
22 17711
23 28657
24 46368
25 75025
26 121393
27 196418
28 317811
29 514229
30 832040
31 1346269
32 2178309
33 3524578
34 5702887
35 9227465
36 14930352
37 24157817
38 39088169
39 63245986
40 102334155
41 165580141
42 267914296
43 433494437
44 701408733
45 1134903170
46 1836311903
47 2971215073
48 4807526976
49 7778742049
50 12586269025
51 20365011074
52 32951280099
53 53316291173
54 86267571272
55 139583862445
56 225851433717
57 365435296162
58 591286729879
59 956722026041
60 1548008755920
61 2504730781961
62 4052739537881
63 6557470319842
64 10610209857723
65 17167680177565
66 27777890035288
67 44945570212853
68 72723460248141
69 117669030460994
70 190392490709135
71 308061521170129
72 498454011879264
73 806515533049393
74 1304969544928657
75 2111485077978050
76 3416454622906707
77 5527939700884757
78 8944394323791464
79 14472334024676221
80 23416728348467685
81 37889062373143906
82 61305790721611591
83 99194853094755497
84 160500643816367088
85 259695496911122585
86 420196140727489673
87 679891637638612258
88 1100087778366101931
89 1779979416004714189
90 2880067194370816120
91 4660046610375530309
92 7540113804746346429
93 -6246583658587674878
94 1293530146158671551
95 -4953053512429003327
96 -3659523366270331776
97 -8612576878699335103
98 6174643828739884737
99 -2437933049959450366

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.