Skip to content

Commit

Permalink
Add IterableFibonacci
Browse files Browse the repository at this point in the history
  • Loading branch information
yuehhua committed May 3, 2020
1 parent a738dde commit dc02635
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
13 changes: 13 additions & 0 deletions IterableFibonacci/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "IterableFibonacci"
uuid = "cb63a8af-3968-486a-879c-2cdfa3e3e856"
authors = ["Yueh-Hua Tu <[email protected]>"]
version = "0.1.0"

[compat]
julia = "1.4"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
77 changes: 77 additions & 0 deletions IterableFibonacci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 可迭代費氏數列

[費氏數列](https://www.wikiwand.com/zh-hant/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97),又稱黃金分割數列,是自然界中非常常見的數列。它是由以下遞迴式所定義:

<img src="https://i.imgur.com/7ggYrnU.png" height="100">

產生以下數列:

```
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
```

## Iterable

Iterable 是 Julia 所提供的一個介面,只要有實作 `iterate()` 這個 API 的就稱為 Iterable。

這邊提供一個 Iterable 階層的例子作為參考。

### 範例:Iterable 階層

```julia
struct Factorial
n::Int
end

function Base.iterate(fac::Factorial, (el, i)=(1, 1))
if i <= fac.n
return (el, (el*(i+1), i+1))
else
return nothing
end
end
```

`iterate` 的機制是會每次回傳 `(el1, (el2, next_i))`,其中 `el1` 是目前的計算的結果,`el2` 是下一次的計算結果,`next_i` 則是下一次的計算順序。我們可以看到每次都會將下一次的結果計算好,如此一來就可以進行迭代。

```julia
julia> for i in f
println(i)
end
1
2
6
24
120
720
5040
40320
362880
3628800
```

也可以參考文章[從 iterator 及 generator 到 iterable](https://yuehhua.github.io/2020/03/01/from-iterator-and-generator-to-iterable/)

請利用 Julia 所提供的 Iterable 介面撰寫產生費氏數列的程式,輸入為數列的序數,輸出為費氏數列的數值。

## 測試

```julia
@test fib(0) == 0
@test fib(1) == 1
@test fib(2) == 1
@test fib(4) == 3
```

### 測試資料

```julia
fib(0)
fib(1)
fib(2)
fib(4)
fib(10)
fib(50)
fib(80)
fib(90)
```
10 changes: 10 additions & 0 deletions IterableFibonacci/src/IterableFibonacci.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module IterableFibonacci

src = [
]

for s = src
include("$(s).jl")
end

end # module
11 changes: 11 additions & 0 deletions IterableFibonacci/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using IterableFibonacci
using Test

tests = [
]

@testset "IterableFibonacci.jl" begin
for t in tests
include("$(t).jl")
end
end
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

### interface

* [可迭代費氏數列](IterableFibonacci/)

### patterns

### parser and interpreter
Expand Down

0 comments on commit dc02635

Please sign in to comment.