-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-level-math.cpp
More file actions
570 lines (429 loc) ยท 10.6 KB
/
basic-level-math.cpp
File metadata and controls
570 lines (429 loc) ยท 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// ๐น Basic Arithmetic & Properties
// Prime numbers (check prime) โญ
// Factors / Divisors of a number
// GCD (Euclidean Algorithm) โญโญโญ
// LCM using GCD
// Even / Odd properties
// ๐น Modular Arithmetic (Intro)
// Modulo properties (a+b)%m, (a*b)%m โญโญโญ
// Handling overflow using modulo
// Basic modular addition & multiplication
// ๐น Exponentiation
// Fast exponentiation (Binary Exponentiation) โญโญโญ
// Power with modulo
// ๐น Counting Basics
// Factorial
// Permutations (nPr)
// Combinations (nCr basic)
// ๐น Simple Problem Types
// Sum of digits
// Reverse number
// Palindrome number
// ๐ Goal: Build strong math intuition
// ๐น 1. Prime Numbers (Check Prime) โญ
// ๐ก Concept
// A number is prime if it has exactly 2 divisors: 1 and itself.
// ๐ Key optimization:
// Check divisibility only up to โn
// Reason: factors come in pairs.
// ๐ง Usage in CP/OA
// Number theory problems
// Sieve-based precomputation
// Cryptography basics
// Prime factorization
// โ๏ธ Algorithm
// for i = 2 โ sqrt(n)
// if n % i == 0 โ not prime
#include<bits/stdc++.h>
using namespace std;
bool isPrime(int n){
if(n<=1) return false; // 0 and 1 are not prime
for(int i=2; i*i<=n; i++){
if(n%i==0) return false; // n is divisible by i, hence not prime
}
return true;
}
int main(){
int n;
cout<<"Enter a number: ";
cin>>n;
if(isPrime(n)){
cout<<n<<" is a prime number."<<endl;
} else {
cout<<n<<" is not a prime number."<<endl;
}
return 0;
}
// ๐น 2. Factors / Divisors
// ๐ก Concept
// Divisors come in pairs:
// If i divides n, then n/i also divides n.
// ๐ง Usage
// Divisor count problems
// Optimization problems
// Number theory tasks
// โ๏ธ Algorithm
// Loop till โn
// for i = 1 โ sqrt(n)
// if n % i == 0
// count++ // i is a divisor
// if i != n/i
// count++ // n/i is also a divisor
#include<bits/stdc++.h>
using namespace std;
vector<int>getDivisors(int n){
vector<int>divisors;
for(int i=1;i*i<=n;i++){
if(n%i==0){
divisors.push_back(i); // i is a divisor
if(i!=n/i){ // Avoid adding the square root twice for perfect squares
divisors.push_back(n/i); // n/i is also a divisor
}
}
}
return divisors;
}
int main(){
int n;
cout<<"Enter a number: ";
cin>>n;
vector<int>divisors = getDivisors(n);
cout<<"Divisors of "<<n<<": ";
for(int d : divisors){
cout<<d<<" ";
}
cout<<endl;
return 0;
}
// ๐น 3. GCD (Euclidean Algorithm) โญโญโญ
// ๐ก Concept
// GCD(a, b) = GCD(b, a % b)
// ๐ Based on:
// Greatest common divisor remains same after modulo reduction
// ๐ง Usage
// Fractions simplification
// LCM calculation
// Number theory
// Competitive problems (very common)
// โ๏ธ Algorithm
// Repeat until b = 0
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
if(b==0) return a;// Base case: GCD of a and 0 is a
return gcd(b,a%b);// Recursive call: GCD of b and a mod b
}
int main(){
int a,b;
cout<<"Enter two numbers: ";
cin>>a>>b;
cout<<"GCD of "<<a<<" and "<<b<<" is: "<<gcd(a,b)<<endl;
return 0;
}
// ๐น 4. LCM using GCD
// ๐ก Concept
// LCM(a, b) = (a * b) / GCD(a, b)
// ๐ง Usage
// Scheduling problems
// Cycles
// Repeating patterns
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}
int lcm(int a,int b){
return (a / gcd(a,b)) * b; // To avoid overflow, divide before multiplying
}
int main(){
int a,b;
cout<<"Enter two numbers: ";
cin>>a>>b;
cout<<"LCM of "<<a<<" and "<<b<<" is: "<<lcm(a,b)<<endl;
return 0;
}
// ๐น 5. Even / Odd Properties
// ๐ก Concept
// Even โ divisible by 2
// Odd โ not divisible
// ๐ง Usage
// Bit manipulation
// Pattern problems
// Optimization
// โ๏ธ Algorithm
// if n % 2 == 0 โ even
// else โ odd
// Bitwise check: if (n & 1) โ odd, else โ even
#include<bits/stdc++.h>
using namespace std;
bool isEven(int n){
return (n % 2 == 0); // Check if n is divisible by 2
}
bool isOdd(int n){
return (n % 2 != 0); // Check if n is not divisible by 2
}
bool isEvenFast(int n){
return (n & 1) == 0; // Bitwise check: if the least significant bit is 0, it's even
}
bool isOddFast(int n){
return (n & 1) == 1; // Bitwise check: if the least significant bit is 1, it's odd
}
int main(){
int n;
cout<<"Enter a number: ";
cin>>n;
if(isEven(n)){
cout<<n<<" is an even number."<<endl;
} else {
cout<<n<<" is an odd number."<<endl;
}
// Using bitwise checks
if(isEvenFast(n)){
cout<<n<<" is an even number (bitwise check)."<<endl;
} else {
cout<<n<<" is an odd number (bitwise check)."<<endl;
}
return 0;
}
// ๐น 6. Modular Arithmetic โญโญโญ
// ๐ก Concept
// (a + b) % m = ((a % m) + (b % m)) % m
// (a * b) % m = ((a % m) * (b % m)) % m
// ๐ง Usage
// Avoid overflow
// Huge numbers (10^18+)
// Standard in CP (mod = 1e9+7)
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7; // Common modulus used in competitive programming
int modAdd(int a, int b) {
return ((a % MOD) + (b % MOD)) % MOD; // Modular addition
}
int modMul(int a, int b) {
return ((a % MOD) * (b % MOD)) % MOD; // Modular multiplication
}
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Modular addition: " << modAdd(a, b) << endl;
cout << "Modular multiplication: " << modMul(a, b) << endl;
return 0;
}
// ๐น 7. Fast Exponentiation โญโญโญ
// ๐ก Concept
// Instead of O(n), reduce to O(log n)
// Binary representation of power:
// a^13 = a^(1101)
// ๐ง Usage
// Power calculation
// Modular exponentiation
// Cryptography
// DP optimizations
// โ๏ธ Formula (core)
// if n is even โ a^n = (a^(n/2))^2
// if n is odd โ a^n = a * (a^(n-1))
// Implementation
// Recursive or iterative (bit manipulation)
// Handle modulo if needed
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7; // Common modulus used in competitive programming
long long power(long long a,long long b){
long long result=1;
while(b>0){
if(b&1) result*=a;
a*=a;
b>>=1;
}
return result;
}
int main(){
long long a,b;
cout<<"Enter base and exponent: ";
cin>>a>>b;
cout<<a<<" raised to the power "<<b<<" is: "<<power(a,b)<<endl;
return 0;
}
// ๐น 8. Power with Modulo
// ๐ป Code
#include<bits/stdc++.h>
using namespace std;
long long modPow(long long a, long long b, int mod) {
long long res = 1;
while(b > 0) {
if(b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int main() {
long long a, b;
int mod;
cout << "Enter base, exponent and modulus: ";
cin >> a >> b >> mod;
cout << a << " raised to the power " << b << " modulo " << mod << " is: " << modPow(a, b, mod) << endl;
return 0;
}
// ๐น 9. Factorial
// ๐ก Concept
// n! = n * (n-1) * ... * 1
// ๐ง Usage
// Permutations & combinations
// Counting problems
// DP optimizations
// โ๏ธ Algorithm
// Iterative or recursive
// Handle large n with modulo if needed
// Precompute factorials for multiple queries
#include<bits/stdc++.h>
using namespace std;
long long factorial(int n) {
long long res = 1;
for(int i = 2; i <= n; i++)
res *= i;
return res;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Factorial of " << n << " is: " << factorial(n) << endl;
return 0;
}
// ๐น 10. Permutations (nPr)
// ๐ก Formula
// nPr = n! / (n-r)!
// ๐ง Usage
// Arrangements
// Counting problems
// โ๏ธ Algorithm
// Calculate factorials
// Handle large n with modulo if needed
// Precompute factorials for multiple queries
#include<bits/stdc++.h>
using namespace std;
long long factorial(int n) {
long long res = 1;
for(int i = 2; i <= n; i++)
res *= i;
return res;
}
long long nPr(int n, int r) {
if(r > n) return 0; // More items than available
return factorial(n) / factorial(n - r); // n! / (n-r)!
}
int main() {
int n, r;
cout << "Enter n and r: ";
cin >> n >> r;
cout << "nPr of " << n << " and " << r << " is: " << nPr(n, r) << endl;
return 0;
}
// ๐น 11. Combinations (nCr)
// ๐ก Formula
// nCr = n! / (r! * (n-r)!)
// ๐ง Usage
// Counting subsets
// Probability
// DP problems
#include<bits/stdc++.h>
using namespace std;
long long nCr(int n, int r) {
if(r > n) return 0;
long long res = 1;
for(int i = 1; i <= r; i++) {
res = res * (n - i + 1) / i;
}
return res;
}
int main() {
int n, r;
cout << "Enter n and r: ";
cin >> n >> r;
cout << "nCr of " << n << " and " << r << " is: " << nCr(n, r) << endl;
return 0;
}
// ๐น 12. Sum of Digits
// ๐ก Concept
// Repeatedly extract digits using %10 and /10
// ๐ง Usage
// Digit-based problems
// โ๏ธ Algorithm
// int sumOfDigits(int n) {
// int sum = 0;
// while(n > 0) {
// sum += n % 10; // Add last digit to sum
// n /= 10; // Remove last digit
// }
#include<bits/stdc++.h>
using namespace std;
int sumOfDigits(int n) {
int sum = 0;
while(n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Sum of digits of " << n << " is: " << sumOfDigits(n) << endl;
return 0;
}
//๐น 13. Reverse Number
// ๐ก Concept
// Extract digits and build reversed number
// ๐ง Usage
#include<bits/stdc++.h>
using namespace std;
int reverseNumber(int n) {
int rev = 0;
while(n > 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
return rev;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Reversed number of " << n << " is: " << reverseNumber(n) << endl;
return 0;
}
// ๐น 14. Palindrome Number
// ๐ก Concept
// Number == Reverse(number)
// ๐ง Usage
// Palindrome checks
// String-based problems
// โ๏ธ Algorithm
// Reverse the number and compare with original
// Handle negative numbers (not palindrome)
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(int n) {
int original = n;
int rev = 0;
while(n > 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
return original == rev;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if(isPalindrome(n)) {
cout << n << " is a palindrome number." << endl;
} else {
cout << n << " is not a palindrome number." << endl;
}
return 0;
}