1+ #include < cmath>
2+ #include < bits/stdc++.h>
3+
4+ namespace mathlib {
5+ // Function to add two or more numbers
6+ double add (int a[], int len){
7+ double sum = 0 ;
8+ for (int i = 0 ; i < len; i++){
9+ sum += a[i];
10+ }
11+ return sum;
12+ }
13+ // Function to subtract between two or more numbers
14+ double sub (int a[], int len){
15+ double sum = 0 ;
16+ for (int i = 0 ; i < len; i++){
17+ sum -= a[i];
18+ }
19+ return sum;
20+ }
21+ // Function to multiply two or more numbers
22+ double mul (int a[], int len){
23+ double sum = 1 ;
24+ for (int i = 0 ; i < len; i++){
25+ sum *= a[i];
26+ }
27+ return sum;
28+ }
29+ // Function to divide between two or more numbers
30+ float div (int a[], int len){
31+ float sum = 1 ;
32+ for (int i = 0 ; i < len; i++){
33+ sum /= a[i];
34+ }
35+ return sum;
36+ }
37+ // Function to find average of two or more numbers
38+ float avg (int a[], int len){
39+ float sum = 0 ;
40+ for (int i = 0 ; i < len; i++){
41+ sum += a[i];
42+ }
43+ return sum/len;
44+ }
45+ // Function to find log of x to for custom base
46+ float logx (int base, int x){
47+ return log (x)/log (base);
48+ }
49+ // Function to find root of x to for custom base
50+ float rootx (int base, int x){
51+ return pow (x, 1.0 /base);
52+ }
53+ // Function to generate random number between min and max
54+ int random (int min, int max){
55+ if (min > max){
56+ int temp = min;
57+ min = max;
58+ max = temp;
59+ }
60+ return rand ()%(max-min + 1 ) + min;
61+ }
62+ // Function to find limit of a function
63+ double limit (int tt, double (*fc)(double ))
64+ {
65+ int n;
66+ double a = pow (10 ,-7 );
67+ double lim = (fc (tt - a) + fc (tt + a)) / 2 ;
68+ return lim;
69+ }
70+ // Function to find factorial of a number
71+ double fact (int n){
72+ if (n == 0 ){
73+ return 1 ;
74+ }
75+ return n*fact (n-1 );
76+ }
77+ /* Function to find 2 linear equations solution from a, b, c of both equations. like ax + by = c */
78+ float * linearEq (float x[], float y[]){
79+ static float result[2 ];
80+ float a1 = x[0 ], b1 = x[1 ], c1 = x[2 ], a2 = y[0 ], b2 = y[1 ], c2 = y[2 ];
81+ result[0 ] = (c1 * b2 - c2 * b1) / (a1 * b2 - b1 * a2);
82+ result[1 ] = (a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1);
83+ return result;
84+ }
85+ /* Function to find quadratic equation solution from a, b, c of equation. like ax^2 + bx + c = 0 */
86+ float * quadEq (float a, float b, float c){
87+ float d = pow (b, 2 ) - 4 * a * c;
88+ static float result[2 ];
89+ if (d > 0 ){
90+ result[0 ] = (-b + sqrt (d)) / (2 * a);
91+ result[1 ] = (-b - sqrt (d)) / (2 * a);
92+ }else if (d == 0 ){
93+ result[0 ] = result[1 ] = -b / (2 * a);
94+ }else {
95+ result[0 ] = result[1 ] = nan (" " );
96+ }
97+ return result;
98+ }
99+ // check if a number is prime or not
100+ bool isPrime (int n){
101+ if (n <= 1 ){
102+ return false ;
103+ }
104+ else {
105+ for (int i = 2 ; i <= n; i++){
106+ if (n%i == 0 ){
107+ return false ;
108+ }
109+ }
110+ return true ;
111+ }
112+ }
113+ // check if a number is odd or not
114+ bool isOdd (int n){
115+ if (n%2 == 0 ){
116+ return false ;
117+ }
118+ return true ;
119+ }
120+ // get summation of a series
121+ double sums (int start, int end, double (*fc)(double )){
122+ if (start > end){
123+ int temp = start;
124+ start = end;
125+ end = temp;
126+ }
127+ double sum = 0 ;
128+ for (int i = start; i <= end; i++){
129+ sum += fc (i);
130+ }
131+ return sum;
132+ }
133+ // get combination of nCr
134+ double combo (int n, int r){
135+ if (n>=0 && r>=0 && n>=r){
136+ return fact (n)/(fact (r)*fact (n-r));
137+ }else {
138+ return nan (" " );
139+ }
140+ }
141+ // get permutation of nPr
142+ double permt (int n, int r){
143+ if (n>=0 && r>=0 && n>=r){
144+ return fact (n)/fact (n-r);
145+ }else {
146+ return nan (" " );
147+ }
148+ }
149+ // get gcd of two numbers
150+ int gcd (int a, int b){
151+ if (a == 0 ){
152+ return b;
153+ }
154+ return gcd (b%a, a);
155+ }
156+ // get lcm of two numbers
157+ int lcm (int a, int b){
158+ return (a*b)/gcd (a, b);
159+ }
160+ // get all prime numbers between two numbers
161+ int * primes (int start, int end){
162+ if (start > end){
163+ int temp = start;
164+ start = end;
165+ end = temp;
166+ }
167+ static int result[100 ];
168+ int j = 0 ;
169+ for (int i = start; i <= end; i++){
170+ if (isPrime (i)){
171+ j++;
172+ result[j] = i;
173+ }
174+ }
175+ return result;
176+ }
177+ }
0 commit comments