# can anyone make this code better ? Classic List Threaded 5 messages Open this post in threaded view
|

## can anyone make this code better ?

 wrote this program in Swift. I am a beginner and want to develop apps. Can anyone suggest ways to make this code better ?class fibonacci {     var num1 : Int = 0     var num2 : Int = 1     let maxLimit : Int = 90     let limit : Int          init(limit : Int = 20) {         self.limit = limit         calc()     }          func calc (counter : Int = 1) {         if limit > maxLimit {             println("Please enter number less than or equal to 90.")         } else {             if counter <= limit {                 if counter == 1 {                     println(num1)                 } else if counter == 2 {                     println(num2)                 } else {                     let total = num1 + num2                     println(total)                     num1 = num2                     num2 = total                 }                 calc(counter: counter+1)             }         }     } } fibonacci(limit: 30) -- You received this message because you are subscribed to the Google Groups "Swift Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/26cc1675-b386-4c4d-8db2-a91566bfa97b%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: can anyone make this code better ?

 If the goal is just to calculate Fibonacci numbers, then personally I’d go with something like this:var fibDict = [0:0, 1:1, 2:1]func fibonacci(n : Int) -> Int {    if let r = fibDict[n] { return r }    if n < 0 { return -1 }        let a = fibonacci(n/2 + 1)    let b = fibonacci((n-1)/2)        let result : Int    if n%2 == 0 { result = a*a - b*b }    else        { result = a*a + b*b }        fibDict[n] = result;    return result}This works through n=92, beyond that the result exceeds Int.max. Making everything a UInt lets it work for n=93, hardly worth the effort. But making the values be NSDecimalNumber objects? Now that lets it go all the way up to n=793, and still blazing fast!import Foundation   // or Cocoavar fibDecDict : [Int : NSDecimalNumber] = [0:0, 1:1, 2:1]func fibonacciDecimalNumber(n : Int) -> NSDecimalNumber {    if let r = fibDecDict[n] { return r }    if n < 0 { return NSDecimalNumber.notANumber() }        let a = fibonacciDecimalNumber(n/2 + 1)    let a2 = a.decimalNumberByMultiplyingBy(a)        let b = fibonacciDecimalNumber((n-1)/2)    let b2 = b.decimalNumberByMultiplyingBy(b)        let result : NSDecimalNumber    if n%2 == 0 { result = a2.decimalNumberBySubtracting(b2) }    else        { result = a2.decimalNumberByAdding(b2) }        fibDecDict[n] = result    return result} -- You received this message because you are subscribed to the Google Groups "Swift Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/3917ecf0-03ca-4ad8-841a-925710377cdc%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Open this post in threaded view
|

## Re: can anyone make this code better ?

 In reply to this post by Aman Rana Turns out using Double lets it go all the way to n=1476, still as fast as ever:var fibDblDict : [Int : Double] = [0:0.0, 1:1.0, 2:1.0]func fibonacciDouble(n : Int) -> Double {    if let r = fibDblDict[n] { return r }    if n < 0 { return Double.NaN }        let a = fibonacciDouble(n/2 + 1)    let b = fibonacciDouble((n-1)/2)        let result : Double    if n%2 == 0 { result = a*a - b*b }    else        { result = a*a + b*b }        fibDblDict[n] = result    return result} -- You received this message because you are subscribed to the Google Groups "Swift Language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. To post to this group, send email to [hidden email]. To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/918e5c36-c1d3-457d-b60e-3fba768b7459%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.