gmp_libmpz_fib2_ui Method |
Namespace: Math.Gmp.Native
This function is designed for calculating isolated Fibonacci numbers. When a sequence of values is wanted it’s best to start with mpz_fib2_ui and iterate the defining F[n + 1] = F[n] + F[n - 1] or similar.
The Fibonacci numbers and Lucas numbers are related sequences, so it’s never necessary to call both mpz_fib2_ui and mpz_lucnum2_ui. The formulas for going from Fibonacci to Lucas can be found in GNU MP - Lucas Numbers Algorithm, the reverse is straightforward too.
// Create, initialize, and set the values of fn and fnsub1 to 0. mpz_t fn = new mpz_t(); mpz_t fnsub1 = new mpz_t(); gmp_lib.mpz_inits(fn, fnsub1, null); // Set fnsub1 and fn to the 19'th and 20'th Fibonacci numbers respectively. gmp_lib.mpz_fib2_ui(fn, fnsub1, 20U); // Assert that fnsub1 and fn are respectively 4181 and 6765. Assert.IsTrue(gmp_lib.mpz_get_si(fnsub1) == 4181); Assert.IsTrue(gmp_lib.mpz_get_si(fn) == 6765); // Release unmanaged memory allocated for fn and fnsub1. gmp_lib.mpz_clears(fn, fnsub1, null);