Fibonacci Hash
June 19, 2018
We implement Fibonacci hash in Scheme, which requires an explicit modulo operation because integer operations overflow to bigints instead of wrapping around:
(define (fibhash h)
(let ((two64 (expt 2 64)) (two54 (expt 2 54)))
(quotient (modulo (* h 11400714819323198485) two64)
two54)))
Here are some examples:
> (fibhash 123412341234) 831 > (fibhash 12341234123412341234) 269 > (fibhash 1234123412341234123412341234) 20
You can run the program at https://ideone.com/AB6Q0h.
Here’s a solution in Haskell.
Example (excluding out-of-range literal warning):
Formatted:
Another implementation in D
import std.stdio; ulong fibhash(ulong n, ulong mul=11400714819323198485UL, ulong shift=54UL) { return (n * mul) >> shift; } void main() { 1234567890UL.fibhash.writeln; }449