Skip to content

Commit 18d0713

Browse files
committed
Add some explanation about mu function, add better gcd
1 parent aa1abed commit 18d0713

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

handy-algorithms/mu.explain

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ mu[i] = {0 if i contains a prime more than once
22
1 if i consists of even number of primes
33
-1 otherwise, ie. odd number of primes}
44
mu can be used to count number of things with gcd of 1 using inclution exclution
5+
to do this: sigma_over_possible_j_as_product_of_primes(mu[j] * f(j))
6+
where f(j) returns number of things where their gcd is divisible by every elemnt of j's factorization, ie. 'j'.

mathematics/number-theory.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ sieve(10000000); // can go up to 10^7 (need few seconds)
6060
* O(log10 n): `n = max(a, b)`
6161
6262
```cpp
63-
int gcd(int a, int b) {
64-
return b == 0 ? a : gcd(b, a % b);
63+
long long gcd(long long a, long long b) {
64+
while (a > 0 && b > 0) {
65+
if (a > b) a %= b;
66+
else b %= a;
67+
}
68+
return a + b;
6569
}
6670
int lcm(int a, int b) {
6771
return a * (b / gcd(a, b));

0 commit comments

Comments
 (0)