I have created a function that gives the Fibonacci number for a given input. I now want to create a function to check if a given number is in the Fibonacci Sequence.
Here is what I have done so far:
-- Basic Fib Function
fib :: Int -> Int
fib x =
if x < 1
then 0
else if x < 2
then 1
else fib (x - 1) + fib (x - 2)
-- Outputs list of all the functions
fibList :: [Int]
fibList = map fib [1..]
-- function that takes an Integer, and returns True if the argument is a Fibonacci number and False otherwise
isFib :: Int -> [Int] -> Bool
isFib n fibList
|n `elem` fibList = True
|otherwise = False
fibList
works but the computation is taking very long time.
Also, I have done this:
fibList' :: [Int]
fibList' = map fib [1..100]
isFib' :: Int -> [Int] -> Bool
isFib' n fibList'
|n `elem` fibList' = True
|otherwise = False
With a smaller number, it still takes a long time to compute