/* -*- gp-script -*- */ \\% Fibonacci representation. \\ Author: Joerg Arndt \\ License: GPL version 3 or later \\ online at http://www.jjj.de/pari/ \\ version: 2014-October-16 (18:31) fib_n_=55 /* global (change to your needs) */ fib_vec_=vector(fib_n_,k,fibonacci(k+1)); /* global */ fibrep(x)= { /* Fibonacci representation (encoded as binary) */ my(k, d, s, t); k = 1; while ( x>=fib_vec_[k], k++ ); d = 2^(k-1); s = 0; while( k>0, t = fib_vec_[k]; if ( x>=t, x -= t; s += d); k--; d>>=1; ); if ( 0!=x, print("OUCH!"); quit); return ( s ); } /* ----- */ ifibrep(x) = { /* Inverse of fibrep() */ my(s); s = 0; for (k=0, fib_n_, if ( bittest(x,k), s+=fib_vec_[k+1])); return( s ); } /* ----- */ \\ ==== end of file ====