Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Van-Eck-sequence/Swift/van-eck-sequence.swift
Normal file
29
Task/Van-Eck-sequence/Swift/van-eck-sequence.swift
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
struct VanEckSequence: Sequence, IteratorProtocol {
|
||||
private var index = 0
|
||||
private var lastTerm = 0
|
||||
private var lastPos = Dictionary<Int, Int>()
|
||||
|
||||
mutating func next() -> Int? {
|
||||
let result = lastTerm
|
||||
var nextTerm = 0
|
||||
if let v = lastPos[lastTerm] {
|
||||
nextTerm = index - v
|
||||
}
|
||||
lastPos[lastTerm] = index
|
||||
lastTerm = nextTerm
|
||||
index += 1
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
let seq = VanEckSequence().prefix(1000)
|
||||
|
||||
print("First 10 terms of the Van Eck sequence:")
|
||||
for n in seq.prefix(10) {
|
||||
print(n, terminator: " ")
|
||||
}
|
||||
print("\nTerms 991 to 1000 of the Van Eck sequence:")
|
||||
for n in seq.dropFirst(990) {
|
||||
print(n, terminator: " ")
|
||||
}
|
||||
print()
|
||||
Loading…
Add table
Add a link
Reference in a new issue