3. Series and Sequences - AparaV/math-functions GitHub Wiki
The namespace Sequences consists of the following functions
- fibonacci
- fibonacciList
- sumSeries
- sumMultiples
- factorial
- factorialList
- nextCollatz
- collatzLength
- champernowne
fibonacci
Finds the nth Fibonacci number using an iterative method. This utilizes much less space than the recursive procedure
Parameters:
int64_t n - The nth Fibonacci number to be found
Return value:
int64_t - The nth Fibonacci number
Sample usage (after including header file):
int64_t x = Sequences::fibonacci(1); //returns 1
fibonacciList
Returns an array of n Fibonacci numbers. Once again uses an iterative method to consume less memory. End is signalled by -1
Parameters:
int64_t n - The number of Fibonacci numbers needed
Return value:
int64_t* - A dynamically allocated array of size n + 1 having ith Fibonacci number at the ith index
Sample usage (after including header file):
int64_t* fib = Sequences::fibonacciList(11);
//Returns an array {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, -1}
sumSeries
The sum of a series of n numbers each raised to coeff.
Parameters:
int n - The number of terms
int coeff - The power each term is to be raised
Return value:
int64_t - The sum of first n natural numbers each raised to coeff
Sample usage (after including header file):
int64_t x = Sequences::sumSeries(3, 1); //returns 6
int64_t y = Sequences::sumSeries(3, 2); //returns 14
int64_t z = Sequences::sumSeries(3, 3); //returns 36
sumMultiples
Returns the sum of all multiples of multiple under the limit upperLimit. Runs in constant time.
Parameters:
int64_t upperLimit - The upper limit for summing the multiples
int64_t multiple - The number whose multiples is to be summed
Return value:
int64_t - The sum of multiples of multiple under upperLimit
Sample usage (after including header file):
int64_t x = Sequences::sumMultiples(10, 2); //returns 20
factorial
Returns the factorial of a number. Uses an iterative method to conserve memory usage.
Parameters:
unsigned int n - The input number
Return value:
int64_t - The factorial of n
Sample usage (after including header file):
int64_t x = Sequences::factorial(0); //returns 1
int64_t y = Sequences::factorial(3); //returns 6
factorialList
Returns an array of numbers where ith index contains the factorial of i.
Parameters:
unsigned int n - The size of the array
Return value:
int64_t* - An array of size n + 1
Sample usage (after including header file):
int64_t* x = Sequences::factorialList(5);
//returns array {1, 1, 2, 6, 24, 120}
nextCollatz
Finds the next number in the collatz sequence
Parameters:
int64_t n - The previous number in the sequence
Return value:
int64_t - The number after n in the collatz sequence
Sample usage (after including header file):
int64_t x = Sequences::nextCollatz(5); // 16
collatzLength
Finds the length of the collatz sequence generated by starting at n
Parameters:
int n - The starting number of the sequence
Return value:
int64_t - The length of the collatz sequence generated by starting at n
Sample usage (after including header file):
int64_t x = Sequences::collatzLen(1); // 1
champernowne
Finds the champernowne constant with n digit precision
Parameters:
int64_t n - The length of the constant
Return value:
string - The constant with n digit precision
Sample usage (after including header file):
string champernowne = Sequences::champernowne(13); // "1234567891011"