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