time-to-botec

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

main.js (3215B)


      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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     24 var iteratorSymbol = require( '@stdlib/symbol/iterator' );
     25 var isPrime = require( './../../../../base/assert/is-prime' );
     26 var validate = require( './validate.js' );
     27 
     28 
     29 // VARIABLES //
     30 
     31 var MAX_ITER = 245181918813464; // floor(FLOAT64_MAX_SAFE_INTEGER/ln(FLOAT64_MAX_SAFE_INTEGER)) => see https://en.wikipedia.org/wiki/Prime_number_theorem
     32 
     33 
     34 // MAIN //
     35 
     36 /**
     37 * Returns an iterator which generates a sequence of prime numbers.
     38 *
     39 * ## Notes
     40 *
     41 * -   If an environment supports `Symbol.iterator`, the returned iterator is iterable.
     42 *
     43 * @param {Options} [options] - function options
     44 * @param {NonNegativeInteger} [options.iter=245181918813464] - number of iterations
     45 * @throws {TypeError} options argument must be an object
     46 * @throws {TypeError} must provide valid options
     47 * @returns {Iterator} iterator
     48 *
     49 * @example
     50 * var iter = iterPrimesSeq();
     51 *
     52 * var v = iter.next().value;
     53 * // returns 2
     54 *
     55 * v = iter.next().value;
     56 * // returns 3
     57 *
     58 * v = iter.next().value;
     59 * // returns 5
     60 *
     61 * // ...
     62 */
     63 function iterPrimesSeq( options ) {
     64 	var opts;
     65 	var iter;
     66 	var FLG;
     67 	var err;
     68 	var n;
     69 	var i;
     70 
     71 	opts = {
     72 		'iter': MAX_ITER
     73 	};
     74 	if ( arguments.length ) {
     75 		err = validate( opts, options );
     76 		if ( err ) {
     77 			throw err;
     78 		}
     79 	}
     80 	i = 0;
     81 	n = 1;
     82 
     83 	// Create an iterator protocol-compliant object:
     84 	iter = {};
     85 	setReadOnly( iter, 'next', next );
     86 	setReadOnly( iter, 'return', end );
     87 
     88 	// If an environment supports `Symbol.iterator`, make the iterator iterable:
     89 	if ( iteratorSymbol ) {
     90 		setReadOnly( iter, iteratorSymbol, factory );
     91 	}
     92 	return iter;
     93 
     94 	/**
     95 	* Returns an iterator protocol-compliant object containing the next iterated value.
     96 	*
     97 	* @private
     98 	* @returns {Object} iterator protocol-compliant object
     99 	*/
    100 	function next() {
    101 		i += 1;
    102 		if ( FLG || i > opts.iter ) {
    103 			return {
    104 				'done': true
    105 			};
    106 		}
    107 		if ( i < 2 ) {
    108 			return {
    109 				'value': 2,
    110 				'done': false
    111 			};
    112 		}
    113 		n += 2;
    114 		while ( isPrime( n ) === false ) {
    115 			n += 2;
    116 		}
    117 		return {
    118 			'value': n,
    119 			'done': false
    120 		};
    121 	}
    122 
    123 	/**
    124 	* Finishes an iterator.
    125 	*
    126 	* @private
    127 	* @param {*} [value] - value to return
    128 	* @returns {Object} iterator protocol-compliant object
    129 	*/
    130 	function end( value ) {
    131 		FLG = true;
    132 		if ( arguments.length ) {
    133 			return {
    134 				'value': value,
    135 				'done': true
    136 			};
    137 		}
    138 		return {
    139 			'done': true
    140 		};
    141 	}
    142 
    143 	/**
    144 	* Returns a new iterator.
    145 	*
    146 	* @private
    147 	* @returns {Iterator} iterator
    148 	*/
    149 	function factory() {
    150 		return iterPrimesSeq( opts );
    151 	}
    152 }
    153 
    154 
    155 // EXPORTS //
    156 
    157 module.exports = iterPrimesSeq;