time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (1753B)


      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 MAX_LUCAS = require( '@stdlib/constants/float64/max-safe-nth-lucas' );
     26 var LUCAS = require( './lucas.json' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Computes the nth Lucas number.
     33 *
     34 * @param {NonNegativeInteger} n - the Lucas number to compute
     35 * @returns {NonNegativeInteger} Lucas number
     36 *
     37 * @example
     38 * var y = lucas( 0 );
     39 * // returns 2
     40 *
     41 * @example
     42 * var y = lucas( 1 );
     43 * // returns 1
     44 *
     45 * @example
     46 * var y = lucas( 2 );
     47 * // returns 3
     48 *
     49 * @example
     50 * var y = lucas( 3 );
     51 * // returns 4
     52 *
     53 * @example
     54 * var y = lucas( 4 );
     55 * // returns 7
     56 *
     57 * @example
     58 * var y = lucas( 5 );
     59 * // returns 11
     60 *
     61 * @example
     62 * var y = lucas( 6 );
     63 * // returns 18
     64 *
     65 * @example
     66 * var y = lucas( NaN );
     67 * // returns NaN
     68 *
     69 * @example
     70 * var y = lucas( 3.14 );
     71 * // returns NaN
     72 *
     73 * @example
     74 * var y = lucas( -1.0 );
     75 * // returns NaN
     76 */
     77 function lucas( n ) {
     78 	if (
     79 		isnan( n ) ||
     80 		isInteger( n ) === false ||
     81 		n < 0 ||
     82 		n > MAX_LUCAS
     83 	) {
     84 		return NaN;
     85 	}
     86 	return LUCAS[ n ];
     87 }
     88 
     89 
     90 // EXPORTS //
     91 
     92 module.exports = lucas;