@@ -46,3 +46,96 @@ export function toFixed(number, fixed) {
4646export function getFactorial ( number ) {
4747 return number != 1 ? number * getFactorial ( number - 1 ) : 1 ;
4848}
49+
50+ let factorialCalculate = ( number ) => {
51+ return number > 1 ? number * factorialCalculate ( number - 1 ) : 1 ;
52+ } ;
53+
54+ /**
55+ * Pass a base number, power number and calculate the power of the number
56+ * @param {* } number
57+ */
58+ exports . bigPower = ( base , power ) => {
59+ if ( power === 0 ) return 1 ;
60+ if ( power % 2 === 0 ) {
61+ const multiplier = bigPower ( base , power / 2 ) ;
62+ return multiplier * multiplier ;
63+ }
64+ const multiplier = bigPower ( base , Math . floor ( power / 2 ) ) ;
65+ return multiplier * multiplier * base ;
66+ }
67+
68+ /**
69+ * Return a fibonacci sequence as an array.
70+ * @param number
71+ * @return {number[] }
72+ * console.log(fibSequence(10)) // [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
73+ */
74+ exports . fibSequence = ( number ) => {
75+ let fibSeq = [ 1 ] , currentValue = 1 , previousValue = 0 ;
76+ if ( number === 1 ) return fibSeq ;
77+ let iterationsCounter = number - 1 ;
78+ while ( iterationsCounter ) {
79+ currentValue += previousValue ;
80+ previousValue = currentValue - previousValue ;
81+ fibSeq . push ( currentValue ) ;
82+ iterationsCounter -= 1 ;
83+ }
84+ return fibSeq ;
85+ }
86+
87+ /**
88+ * Calculate fibonacci number at specific position using Dynamic Programming approach.
89+ * @param number
90+ * @return {number }
91+ * console.log(fibonacciNth(10)) // 55
92+ */
93+ exports . fibNth = ( number ) => {
94+ let currentValue = 1 , previousValue = 0 ;
95+ if ( number === 1 ) return 1 ;
96+ let iterationsCounter = number - 1 ;
97+ while ( iterationsCounter ) {
98+ currentValue += previousValue ;
99+ previousValue = currentValue - previousValue ;
100+ iterationsCounter -= 1 ;
101+ }
102+ return currentValue ;
103+ }
104+
105+ /**
106+ * Get pascal Triangle
107+ * @param {number } lineNumber - zero based.
108+ * @return {number[] }
109+ * console.log(pascalTriangle(10))
110+ * [ 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1 ]
111+ */
112+ exports . pascalTriangle = ( lineNumber ) => {
113+ let currentLine = [ 1 ] , currentLineSize = lineNumber + 1 ;
114+ for ( let numIndex = 1 ; numIndex < currentLineSize ; numIndex += 1 ) {
115+ currentLine [ numIndex ] = currentLine [ numIndex - 1 ] * ( lineNumber - numIndex + 1 ) / numIndex ;
116+ }
117+ return currentLine ;
118+ }
119+
120+ /**
121+ * Get prime list
122+ * @param {number }
123+ * @return {primes[] }
124+ * console.log(getPrimeList(20)) // [ 2, 3, 5, 7, 11, 13, 17, 19 ]
125+ */
126+ exports . getPrimeList = ( number ) => {
127+ let isPrime = new Array ( number + 1 ) . fill ( true ) , primes = [ ] ;
128+ isPrime [ 0 ] = false ;
129+ isPrime [ 1 ] = false ;
130+ for ( let num = 2 ; num <= number ; num += 1 ) {
131+ if ( isPrime [ num ] === true ) {
132+ primes . push ( num ) ;
133+ let nextnum = num * num ;
134+ while ( nextnum <= number ) {
135+ isPrime [ nextnum ] = false ;
136+ nextnum += num ;
137+ }
138+ }
139+ }
140+ return primes ;
141+ }
0 commit comments