[Haskell-cafe] origin of my fibonacci test
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?
> 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)
>> (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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe