├── .gitignore ├── Elshorpagi.h ├── README.md ├── math_algorithms.cpp └── test ├── input.txt └── output.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.vscode -------------------------------------------------------------------------------- /Elshorpagi.h: -------------------------------------------------------------------------------- 1 | 2 | // My Headers, and you can modify them 3 | 4 | // C 5 | #include 6 | #include 7 | #include 8 | 9 | // C++ 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | // STL in C++ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | // macros 33 | using namespace std; 34 | 35 | typedef long long ll; 36 | typedef long double ld; 37 | typedef unsigned long long ull; 38 | typedef pair pii; 39 | typedef pair pll; 40 | typedef vector vi; 41 | typedef vector vc; 42 | typedef vector vb; 43 | typedef vector vvi; 44 | typedef vector vll; 45 | typedef vector vvll; 46 | typedef vector vpii; 47 | typedef set si; 48 | 49 | #define _CRT_SECURE_NO_DEPRECATE 50 | #define __elshorpagi__ (ios_base::sync_with_stdio(false), cin.tie(NULL)) 51 | #define all(v) ((v).begin()), ((v).end()) 52 | #define sz(v) ((int)((v).size())) 53 | #define cl(v) ((v).clear()) 54 | #define edl '\n' 55 | #define fr(i, x, n) for (int i(x); i < n; ++i) 56 | #define fl(i, x, n) for (int i(x); i > n; --i) 57 | #define fc(it, v) for (auto &(it) : (v)) 58 | #define sq(x) (x) * (x) 59 | #define yes cout << "YES\n" 60 | #define no cout << "NO\n" 61 | #define MOD 1000000007 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Math Algorithms 2 | 3 | * SUMMATION FORMULAS 4 | * CUMULATIVE SUM 5 | * UPDATE RANGE 6 | * FACTORIAL 7 | * COMBINATION 8 | * PERMUTATION 9 | * GCD ( ITERATIVE / RECURSIVE ) 10 | * LCM 11 | * EXTENDED EUCLIDEAN ( ITERATIVE / RECURSIVE ) 12 | * FAST POWER ( ITERATIVE / RECURSIVE ) 13 | * MODULUS MULTIPLICATION ( ADDITION ) 14 | * MODULUR EXPONENTIATION 15 | * PRIME CHECKING 16 | * FACTORIZATION 17 | * PRIME FACTORIZATION 18 | * PERFECT SQUARE 19 | * SIEVE OF ERATOSTHENES 20 | * LINEAR SIEVE OF ERATOSTHENES 21 | * SEGMENTED SIEVE 22 | -------------------------------------------------------------------------------- /math_algorithms.cpp: -------------------------------------------------------------------------------- 1 | // #include 2 | #include "Elshorpagi.h" 3 | 4 | /*-----------------MATH TIPS----------------*/ 5 | // ceil(a / b) a, b must be double; 6 | // ceil(a / b) a, b are integers = ((a + b - 1) / b); 7 | // round(a / b); 8 | // if (a < 0) 9 | // return (a - b / 2) / b; 10 | // else 11 | // return (a + b / 2) / b; 12 | 13 | // a % b = a - (b * (floor(a / b))); 14 | // a % b = ((a % b) + b) % b; for negative numbers 15 | 16 | // (a + b) % c = ((a % c) + (b % c)) % c; 17 | // (a * b) % c = ((a % c) * (b % c)) % c; 18 | // (a - b) % c = ((a % c) - (b % c) + c) % c; 19 | // (a / b) % c = ((a % c) * ((b ^ -1) % c)) % c; 20 | 21 | /*-----------------SUMMATION FORMULAS----------------*/ 22 | int clac_sum(int a1, int an, int n) 23 | { 24 | // sum all ODD numbers from 1 to n = ((n + 1) / 2) * ((n + 1) / 2); 25 | // sum all EVEN numbers from 1 to n -> (m = n / 2) --> (sum = m * (m + 1)); 26 | // sum all number from 1 to n = n * (n + 1) / 2; 27 | /*-----------------------------------------------------------------------*/ 28 | // a1 = first number; 29 | // an = last number -> an = a1 + (n - 1) * (differnce between numbers); 30 | // n = how many numbers; 31 | return (((a1 + an) * n) >> 1); 32 | } 33 | 34 | /*-----------------CUMULATIVE SUM----------------*/ 35 | void cumulative_sum() 36 | { 37 | int n; 38 | cin >> n; 39 | vi arr(n); 40 | fc(it, arr) cin >> it; 41 | fr(i, 1, n) arr[i] += arr[i - 1]; 42 | int q, l, r; 43 | cin >> q; 44 | while (q--) 45 | { 46 | cin >> l >> r; 47 | --l, --r; 48 | cout << (l != 0 ? arr[r] - arr[l - 1] : arr[r]) << edl; 49 | } 50 | } 51 | 52 | /*-----------------UPDATE RANGE----------------*/ 53 | void update_range() 54 | { 55 | // value can be any number based on the problem; 56 | int n, t; 57 | cin >> n >> t; 58 | vi arr(n + 10); // given array of zeros of size n; 59 | int value(1), l, r; 60 | while (t--) 61 | { 62 | cin >> l >> r; 63 | arr[l] += value; 64 | arr[r + 1] -= value; 65 | } 66 | fr(i, 1, n + 1) arr[i] += arr[i - 1]; 67 | fr(i, 1, n + 1) cout << arr[i] << " "; 68 | } 69 | 70 | /*-----------------FACTORIAL----------------*/ 71 | int factorial(int n) 72 | { 73 | return ((!n || n == 1) ? 1 : (n * factorial(n - 1))); 74 | } 75 | 76 | /*-----------------COMBINATION----------------*/ 77 | unsigned int nCr(int n, int r) 78 | { 79 | // we can use -> nCr = factorial(n) / (factorial(r) * factorial(n - r)); 80 | if (r > n) 81 | return 0; 82 | r = max(r, n - r); // nCr(n,r) = nCr(n,n-r) 83 | unsigned int ans(1), div(1), i(r + 1); 84 | while (i <= n) 85 | { 86 | ans *= i, ++i; 87 | ans /= div, ++div; 88 | } 89 | return ans; 90 | } 91 | 92 | /*-----------------PERMUTATION----------------*/ 93 | unsigned int nPr(int n, int r) 94 | { 95 | // we can use -> nPr = factorial(n) / factorial(n - r); 96 | if (r > n) 97 | return 0; 98 | unsigned int p(1), i(n - r + 1); 99 | while (i <= n) 100 | p *= i++; 101 | return p; 102 | } 103 | 104 | /*-----------------GCD ITERATIVE----------------*/ 105 | int gcd_iterative(int A, int B) 106 | { 107 | if (A < B) 108 | { 109 | A ^= B; 110 | B ^= A; 111 | A ^= B; 112 | // you can use swap(A, B) but this way is more faster; 113 | } 114 | while (A && B) 115 | { 116 | int R(A % B); 117 | A = B; 118 | B = R; 119 | } 120 | return A; 121 | } 122 | 123 | /*-----------------GCD RECURSIVE----------------*/ 124 | int gcd_recursive(int A, int B) 125 | { 126 | return (!B ? A : gcd_recursive(B, A % B)); 127 | } 128 | 129 | /*-----------------LCM----------------*/ 130 | int lcm(int A, int B) 131 | { 132 | return A / gcd_recursive(A, B) * B; 133 | } 134 | 135 | /*-----------------EXTENDED EUCLIDEAN ITERATIVE----------------*/ 136 | int extended_euclidean_iterative(int a, int b, int &x_prev, int &y_prev) 137 | { 138 | // a*x + b*y = gcd(a, b); // Bézout's Theorem 139 | // This algorithm return x, y and gcd(a, b); 140 | x_prev = 1, y_prev = 0; 141 | int x(0), y(1); 142 | while (b) 143 | { 144 | int q(a / b); 145 | tie(x, x_prev) = make_tuple(x_prev - q * x, x); 146 | tie(y, y_prev) = make_tuple(y_prev - q * y, y); 147 | tie(a, b) = make_tuple(b, a % b); 148 | } 149 | return a; // a = gcd(a, b); 150 | } 151 | 152 | /*-----------------EXTENDED EUCLIDEAN RECURSIVE----------------*/ 153 | int extended_euclidean_recursive(int a, int b, int &x, int &y) 154 | { 155 | // a*x + b*y = gcd(a, b); // Bézout's Theorem 156 | // This algorithm return x, y and gcd(a, b); 157 | if (!b) 158 | { 159 | x = 1, y = 0; 160 | return a; 161 | } 162 | int x1, y1; 163 | int g(extended_euclidean_recursive(a, a % b, x1, y1)); 164 | x = y1; 165 | y = x1 - y1 * (a / b); // a % b = a - (b * (floor(a / b))); 166 | return g; // g = gcd(a, b); 167 | } 168 | 169 | /*-----------------FAST POWER ITERATIVE----------------*/ 170 | int fast_power_iterative(int b, int p) 171 | { 172 | int ans(1); 173 | while (p) 174 | { 175 | if (p & 1) 176 | ans *= b; 177 | b *= b; 178 | p >>= 1; 179 | } 180 | return ans; 181 | } 182 | 183 | /*-----------------FAST POWER RECURSIVE----------------*/ 184 | int fast_power_recursive(int b, int p) 185 | { 186 | if (!p) 187 | return 1; 188 | int squ(fast_power_recursive(b, p >> 1)); 189 | squ *= squ; 190 | if (p & 1) 191 | squ *= b; 192 | return squ; 193 | } 194 | 195 | /*-----------------MODULUS MULTIPLICATION----------------*/ 196 | int multiplication_mod(int a, int b, int c) 197 | { 198 | // (a * b) % c; 199 | return ((a % c) * (b % c)) % c; 200 | } 201 | 202 | /*-----------------MODULUS MULTIPLICATION BY ADDITION----------------*/ 203 | int multiplication_mod_by_addition(int a, int b, int m) 204 | { 205 | // this func convert the multiplication operation to addition operation; 206 | int res(0), y(a % m); 207 | while (b) 208 | { 209 | if (b & 1) 210 | res = (res + y) % m; 211 | b >>= 1, y <<= 1, y %= m; 212 | } 213 | return res % m; 214 | } 215 | 216 | /*-----------------MODULUR EXPONENTIATION----------------*/ 217 | int modular_exponentiation(int base, int power, int m = MOD) 218 | { 219 | // You can change MOD to any variable that you want and add it to the function parameters; 220 | int result(1); 221 | while (power) 222 | { 223 | if (power & 1) 224 | result = multiplication_mod_by_addition(result, base, m); // you can use multiplication_mod function; 225 | base = multiplication_mod_by_addition(base, base, m); // but this is faster; 226 | power >>= 1; 227 | } 228 | return result; 229 | } 230 | 231 | /*-----------------PRIME CHECKING 0----------------*/ 232 | bool is_prime_0(int n, int k = 500) // Fermat Primality algorithm 233 | { 234 | if (n < 5) 235 | return n == 2 || n == 3; 236 | fr(i, 1, k + 1) 237 | { 238 | int a(2 + rand() % (n - 3)); 239 | int res(modular_exponentiation(a, n - 1, n)); 240 | if (res != 1) 241 | return false; 242 | } 243 | return true; 244 | } 245 | 246 | /*-----------------PRIME CHECKING 1----------------*/ 247 | bool is_prime_1(int n) 248 | { 249 | if (n <= 1) 250 | return false; 251 | for (int i(2); i * i <= n; ++i) 252 | { 253 | if (n % i == 0) 254 | return false; 255 | } 256 | return true; 257 | } 258 | 259 | /*-----------------FACTORIZATION----------------*/ 260 | void factorization(int n) 261 | { 262 | for (int i(1); i * i <= n; ++i) 263 | { 264 | if (n % i == 0) 265 | { 266 | cout << i << " "; 267 | if (i * i != n) 268 | cout << n / i; 269 | cout << edl; 270 | } 271 | } 272 | } 273 | 274 | /*-----------------PRIME FACTORIZATION----------------*/ 275 | void prime_factorization(int n) 276 | { 277 | vi p_factors; 278 | for (int i(2); i * i <= n; ++i) 279 | { 280 | while (n % i == 0) 281 | n /= i, p_factors.emplace_back(i); 282 | } 283 | if (n != 1) 284 | p_factors.emplace_back(n); 285 | fc(it, p_factors) cout << it << " "; 286 | } 287 | 288 | /*-----------------CHECK PERFECT SQUARE----------------*/ 289 | bool perfect_square(int n) 290 | { 291 | double sqrt_n(sqrt(n)); 292 | if (sqrt_n == int(sqrt_n)) 293 | return true; 294 | return false; 295 | } 296 | 297 | /*-----------------SIEVE OF ERATOSTHENES----------------*/ 298 | const int sze(1e6 + 10); 299 | void sieve() 300 | { 301 | bool composite[sze + 1]; 302 | vi prime(sze); 303 | composite[0] = composite[1] = 1; 304 | for (int i(2); i * i <= sze; ++i) 305 | { 306 | if (!composite[i]) 307 | { 308 | for (int j(i * i); j <= sze; j += i) 309 | composite[j] = 1; 310 | } 311 | } 312 | } 313 | 314 | /*-----------------LINEAR SIEVE OF ERATOSTHENES----------------*/ 315 | void linear_sieve() 316 | { 317 | bool composite[sze + 1]; 318 | vi prime(sze); 319 | composite[0] = composite[1] = 1; 320 | fr(i, 2, sze + 1) 321 | { 322 | if (!composite[i]) 323 | prime.push_back(i); 324 | for (int j(0); j < sz(prime) && i * prime[j] <= sze; ++j) 325 | { 326 | composite[i * prime[j]] = 1; 327 | if (i % prime[j] == 0) 328 | break; 329 | } 330 | } 331 | } 332 | 333 | /*-----------------SEGMENTED SIEVE----------------*/ 334 | vb segmented_sieve(int L, int R) 335 | { 336 | int lim(sqrt(R)); 337 | vb mark(lim + 1, false); 338 | vi primes; 339 | fr(i, 2, lim + 1) 340 | { 341 | if (!mark[i]) 342 | { 343 | primes.emplace_back(i); 344 | for (int j(i * i); j <= lim; j += i) 345 | mark[j] = true; 346 | } 347 | } 348 | vb is_composite(R - L + 1, false); 349 | fc(it, primes) 350 | { 351 | for (int i(max(it * it, (L + it - 1) / it * it)); i <= R; i += it) 352 | is_composite[i - L] = true; 353 | } 354 | if (L == 1) 355 | is_composite[0] = true; 356 | return is_composite; 357 | } 358 | 359 | /*-----------------PRINT SEGMENTED SIEVE----------------*/ 360 | void print_segmented_sieve(int L, int R) 361 | { 362 | vb is_composite_print(segmented_sieve(L, R)); 363 | fr(i, L, R + 1) 364 | { 365 | if (!is_composite_print[i - L]) 366 | cout << i << edl; 367 | } 368 | cout << edl; 369 | } 370 | 371 | class Math_Algorithms 372 | { 373 | public: 374 | Math_Algorithms() { __elshorpagi__; } 375 | ~Math_Algorithms() { cout << edl << "DONE" << edl; } 376 | void TEST() 377 | { 378 | // test the MAs here; 379 | } 380 | }; 381 | 382 | int main() 383 | { 384 | Math_Algorithms MA; 385 | // freopen("test/input.txt", "r", stdin); 386 | freopen("test/output.txt", "w", stdout); 387 | int tc(1); 388 | // cin >> tc; 389 | while (tc--) 390 | cout << "Case #" << tc + 1 << edl, MA.TEST(); 391 | return (0); 392 | } -------------------------------------------------------------------------------- /test/input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ali-Elshorpagi/math_algorithms/e1f140c95bfe9e5769f4967eb7f86a044aed5022/test/input.txt -------------------------------------------------------------------------------- /test/output.txt: -------------------------------------------------------------------------------- 1 | Case #1 2 | 27 3 | 4 | DONE 5 | --------------------------------------------------------------------------------