Sort by

recency

|

389 Discussions

|

  • + 0 comments

    Python3:

    def isFibo(n):
        test_1 = 5 * (n**2) + 4
        test_2 = 5 * (n**2) - 4
        
        # If n is a fibonacci number then test_1 or test_2 must be a perfect square
        
        if int(math.sqrt(test_1)) ** 2 == test_1 or int(math.sqrt(test_2)) ** 2 == test_2:
            return 'IsFibo'
        else:
            return 'IsNotFibo'
    
  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM_)
    import qualified Data.Set  as S
    
    fibos :: S.Set Integer
    fibos = S.fromList $ takeWhile (<= 10000000000) fibos'
      where
        fibos' = 1 : 1 : zipWith (+) fibos' (tail fibos')
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            n <- readLn :: IO Integer
            putStrLn $ if n `S.member` fibos then "IsFibo" else "IsNotFibo"
    
  • + 0 comments

    here is my solution ! very time efficient without using lists

    def isFibo(n):
        # Write your code here
        if n==0 or n==1:
            return "IsFibo"
        i_th=0
        i2_th=1
        index=1
        while index<=n:
            if i_th+i2_th==n:
                return "IsFibo"
            i_th = i2_th
            i2_th = index
            index = i_th + i2_th
        if index==n:
            return "IsFibo"
        return "IsNotFibo"
    
  • + 0 comments

    the problem is actually quite easy. the bigger problem is how to handle the time limit. that's where we needed the "cache". golang map is excellent in this

    Golang :

    var fiboVal = make(map[int64]int64)
    
    func isFibo(n int64) string {
        // Write your code here
        var isFiboVar = make(map[int64]bool)
        
        if _, isExist := isFiboVar[0]; !isExist {
            isFiboVar[0] = true
        }
        
        if _, isExist := isFiboVar[1]; !isExist {
            isFiboVar[1] = true
        }
        
        for i := int64(2);;i++ {
            val := fibo(i)
            isFiboVar[val] = true
           
            if val > n {
                break
            }
        }
    
        if isFiboVar[n] {
            return "IsFibo"
        }
    
        return "IsNotFibo"
    }
    
    func fibo(n int64) int64 {
        if _, isExist := fiboVal[n]; isExist {
            return fiboVal[n]
        }
        
        if n == 0 {
            fiboVal[0] = 0
            return 0
        }
        
        if n == 1 {
            fiboVal[1] = 1
            return 1
        }
        
        if n > 1 {
            fiboVal[n] = fibo(n-1) + fibo(n-2)
            return fiboVal[n]
        }
        
        return 0
    }
    
  • + 0 comments
    import os
    
    
    def get_fibs():
        maxi = 10 ** 10
        first, last = 1, 1
        fibs = [first, last]
        while last <= maxi:
            last_new = first + last
            fibs.append(last_new)
            first = last
            last = last_new
        return fibs
    
    def isFibo(fibs, n):
        Y, N = "IsFibo", "IsNotFibo"
        if n in fibs:
            return Y
        else:
            return N
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        t = int(input().strip())
        
        fibs = get_fibs()
    
        for t_itr in range(t):
            n = int(input().strip())
            
            result = isFibo(fibs, n)
    
            fptr.write(result + '\n')
    
        fptr.close()