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)
A Double has fewer bits of precision than a 64 bit integer on account of being the same size and needing some space for the exponent. Therefore, you will be losing bits with the larger values of n.