[Haskell-cafe] origin of my fibonacci test

Michael Litchard michael at schmong.org
Tue May 10 00:13:42 UTC 2016


That's it thanks.

On Mon, May 9, 2016 at 5:08 PM, Jonne Ransijn <yoyoyonny at gmail.com> wrote:

> Hi Michael,
>
> I suppose this is where you got the code from?
>
> https://www.quora.com/What-is-the-most-efficient-algorithm-to-check-if-a-number-is-a-Fibonacci-Number
>
> It's the sixth result of the Google search "haskell: get if a number is a
> fibonacci number"
> On May 10, 2016 1:59 AM, "Michael Litchard" <michael at schmong.org> wrote:
>
>> I have some code that checks to see if a number is a fibonacci number.
>> It's not mine, I got it from somewhere and I need to credit it. I am pretty
>> sure I got it from Stack Exchange but my search for it went nowhere. If
>> this looks familiar to you, or you can tell me better ways to search,
>> please let me know.
>>
>> isFib :: Integer -> Bool
>> isFib n = n == a where (_, a, _) = unFib (1, 1) n
>>
>> unFib :: (Integer, Integer) -> Integer -> (Integer,Integer,Integer)
>> unFib (a, b) n
>>   | n < a = (0, 0, 1)
>>   | n < e = (2*k, c, d)
>>   | otherwise = (2*k + 1, e, f)
>>       where
>>         (k, c, d) = unFib (fibPlus (a, b) (a, b)) n
>>         (e, f)    = fibPlus (a, b) (c, d)
>>
>> fibPlus :: (Integer, Integer) -> (Integer, Integer) -> (Integer,Integer)
>> fibPlus (a, b) (c, d) = (bd - (b - a)*(d - c), a*c + bd)
>>   where bd = b*d
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160509/61ce4cf7/attachment.html>


More information about the Haskell-Cafe mailing list