Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Leonardo-numbers/Swift/leonardo-numbers.swift
Normal file
24
Task/Leonardo-numbers/Swift/leonardo-numbers.swift
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
struct Leonardo: Sequence, IteratorProtocol {
|
||||
private let add : Int
|
||||
private var n0: Int
|
||||
private var n1: Int
|
||||
|
||||
init(n0: Int = 1, n1: Int = 1, add: Int = 1) {
|
||||
self.n0 = n0
|
||||
self.n1 = n1
|
||||
self.add = add
|
||||
}
|
||||
|
||||
mutating func next() -> Int? {
|
||||
let n = n0
|
||||
n0 = n1
|
||||
n1 += n + add
|
||||
return n
|
||||
}
|
||||
}
|
||||
|
||||
print("First 25 Leonardo numbers:")
|
||||
print(Leonardo().prefix(25).map{String($0)}.joined(separator: " "))
|
||||
|
||||
print("First 25 Fibonacci numbers:")
|
||||
print(Leonardo(n0: 0, add: 0).prefix(25).map{String($0)}.joined(separator: " "))
|
||||
Loading…
Add table
Add a link
Reference in a new issue