Returns a sequence formed from first and repeated lazy applications of next.
func sequence<T>(first: T, next: @escaping (T) -> T?) -> UnfoldSequence<T, (T?, Bool)>
Returns a sequence formed from first and repeated lazy applications of next.
func sequence<T>(first: T, next: @escaping (T) -> T?) -> UnfoldSequence<T, (T?, Bool)>
Returns a sequence formed from first and repeated lazy applications of next.
func sequence<T>(first: T, next: @escaping (T) -> T?) -> UnfoldSequence<T, (T?, Bool)>
EBrown demonstrated EBrown demonstrated how to use caching/memoization to increase the performance.
EBrown demonstrated how to use caching/memoization to increase the performance.
EBrown demonstrated how to use caching/memoization to increase the performance.
With this approachThis code, the Collatz length of all numbers up to 1 millioncompiled with Xcode with optimization ("Release" configuration) is computedruns in approx 0.8 secondsabout 0.8 seconds on my 1.2 GHz Intel Core m5 MacBook.
let cacheSize = 1_000_000
var cache = [Int](repeating: 0, count: cacheSize)
cache[1] = 1
func collatzLengthCached(n: Int64) -> Int {
if let smallN = Int(exactly: n), smallN < cacheSize {
if cache[smallN] > 0 {
return cache[smallN]
}
let len = 1 + collatzLengthCached(n: collatzFunc(n: n)!)
cache[smallN] = len
return len
}
return 1 + collatzLengthCached(n: collatzFunc(n: n)!)
}
let (maxN, maxLength) = (1...999_999)
.map { (0,ドル collatzLengthCached(n: 0ドル)) }
.max { 0ドル.1 < 1ドル.1 }!
With this approachThis code, the Collatz length of all numbers up to 1 millioncompiled with Xcode with optimization ("Release" configuration) is computedruns in approx 0.08 secondsabout 0.08 seconds on my 1.2 GHz Intel Core m5 MacBook.
With this approach, the Collatz length of all numbers up to 1 million is computed in approx 0.8 seconds on my MacBook.
let cacheSize = 1_000_000
var cache = [Int](repeating: 0, count: cacheSize)
cache[1] = 1
func collatzLengthCached(n: Int64) -> Int {
if let smallN = Int(exactly: n), smallN < cacheSize {
if cache[smallN] > 0 {
return cache[smallN]
}
let len = 1 + collatzLengthCached(n: collatzFunc(n: n)!)
cache[smallN] = len
return len
}
return 1 + collatzLengthCached(n: collatzFunc(n: n)!)
}
With this approach, the Collatz length of all numbers up to 1 million is computed in approx 0.08 seconds on my MacBook.
This code, compiled with Xcode with optimization ("Release" configuration) runs in about 0.8 seconds on my 1.2 GHz Intel Core m5 MacBook.
let cacheSize = 1_000_000
var cache = [Int](repeating: 0, count: cacheSize)
cache[1] = 1
func collatzLengthCached(n: Int64) -> Int {
if let smallN = Int(exactly: n), smallN < cacheSize {
if cache[smallN] > 0 {
return cache[smallN]
}
let len = 1 + collatzLengthCached(n: collatzFunc(n: n)!)
cache[smallN] = len
return len
}
return 1 + collatzLengthCached(n: collatzFunc(n: n)!)
}
let (maxN, maxLength) = (1...999_999)
.map { (0,ドル collatzLengthCached(n: 0ドル)) }
.max { 0ドル.1 < 1ドル.1 }!
This code, compiled with Xcode with optimization ("Release" configuration) runs in about 0.08 seconds on my 1.2 GHz Intel Core m5 MacBook.