Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

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)>
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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.

Clarified benchmark statements.
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95

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.

deleted 2 characters in body
Source Link
Der Kommissar
  • 20.3k
  • 4
  • 70
  • 158
Loading
Fixed wrong initial value in reduce().
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
added 1227 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
added 150 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
Source Link
Martin R
  • 24.2k
  • 2
  • 37
  • 95
Loading
default

AltStyle によって変換されたページ (->オリジナル) /