Thứ Hai, 18 tháng 5, 2020

Algorithm ex: Fibonacci Sequence

Write a method that returns a Fibonacci sequence from 1 to n.

The Fibonacci sequence là 1 danh sách các số, mà giá trị tiếp theo trong dãy sốlà tổng của 2 số đàng trước. chuỗi số cũng định nghĩa rằng số đầu tiên là số 0 và số tiếp theo là số 1.


Using return with if Statements

Whenever you have a return statement inside an if statement, you do not need to provide an else clause. If an if statement condition is false, the code execution continues after the block. If the statement is true, the method execution will finish with the return statement. This has the advantage that your code does not need an extra level of indentation due to the redundant else block:

Caching previously computed Fibonacci numbers


Measuring the performance increase


Non cached time: 19211293000 nanoseconds
Cached time: 311000 nanoseconds
-> Quite an improvement: from 19 seconds to 0.000311 seconds.

Write a factorial implementation that does not use recursion.

An iterative implementation of a factorial



Happy coding

thelam92

0 nhận xét:

Đăng nhận xét