main.js (2256B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isnan = require( './../../../../base/assert/is-nan' ); 24 var isInteger = require( './../../../../base/assert/is-integer' ); 25 var ln = require( './../../../../base/special/ln' ); 26 var floor = require( './../../../../base/special/floor' ); 27 var PHI = require( '@stdlib/constants/float64/phi' ); 28 var PINF = require( '@stdlib/constants/float64/pinf' ); 29 30 31 // VARIABLES // 32 33 var SQRT_5 = 2.23606797749979; 34 var LN_PHI = ln( PHI ); 35 36 37 // MAIN // 38 39 /** 40 * Computes the nth non-Fibonacci number. 41 * 42 * ## References 43 * 44 * - Gould, H.W. 1965. "Non-Fibonacci Numbers." _Fibonacci Quarterly_, no. 3: 177–83. <http://www.fq.math.ca/Scanned/3-3/gould.pdf>. 45 * - Farhi, Bakir. 2011. "An explicit formula generating the non-Fibonacci numbers." _arXiv_ abs/1105.1127 \[Math.NT\] (May): 1–5. <https://arxiv.org/abs/1105.1127>. 46 * 47 * 48 * @param {NonNegativeInteger} n - the non-Fibonacci number to compute 49 * @returns {NonNegativeInteger} non-Fibonacci number 50 * 51 * @example 52 * var v = nonfibonacci( 1 ); 53 * // returns 4 54 * 55 * @example 56 * var v = nonfibonacci( 2 ); 57 * // returns 6 58 * 59 * @example 60 * var v = nonfibonacci( 3 ); 61 * // returns 7 62 * 63 * @example 64 * var v = nonfibonacci( NaN ); 65 * // returns NaN 66 * 67 * @example 68 * var v = nonfibonacci( 3.14 ); 69 * // returns NaN 70 * 71 * @example 72 * var v = nonfibonacci( -1 ); 73 * // returns NaN 74 */ 75 function nonfibonacci( n ) { 76 var a; 77 var b; 78 if ( 79 isnan( n ) || 80 isInteger( n ) === false || 81 n < 1 || 82 n === PINF 83 ) { 84 return NaN; 85 } 86 n += 1; 87 a = ln( n * SQRT_5 ) / LN_PHI; 88 b = ln( (SQRT_5 * (n+a)) - 5.0 + (3.0/n) ) / LN_PHI; 89 return floor( n + b - 2.0 ); 90 } 91 92 93 // EXPORTS // 94 95 module.exports = nonfibonacci;