Test if a number is fibonacci

后端 未结 20 898
深忆病人
深忆病人 2020-11-30 17:58

I know how to make the list of the Fibonacci numbers, but i don\'t know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is genera

20条回答
  •  温柔的废话
    2020-11-30 18:34

    A Scala version-

    def isFib(n: Int): Boolean = {
    
    def checkFib(f1: Int = 1, f2: Int = 1): Boolean = {
    
    if(n == f1 || n == f2) true
    else if(n < f2) false
    else checkFib(f2, f1+f2)
    
    }
    
    checkFib()
    
    }
    

提交回复
热议问题