Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions CPP/algorithms/numbertheory/ETF1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// It is function

// denoted by ɸ(x)

// ɸ(x) = count of number from [1,x] which are coprime with x

// if gcd(x,y) = 1 then x and y are co prime

// ɸ(5) = 4

// 5 means [1,5] = 1,2,3,4,5

// gcd(1,5) = 1 , gcd(2,5) = 1 , gcd(3,5) = 1 , gcd(4,5) = 1 // count is 4 (means 4 numberes are there)

// ɸ(10) = 4


// Prime Number and ETF

// ɸ(2) = 1
// ɸ(3) = 2
// ɸ(5) = 4
// ɸ(7) = 6
// ɸ(11) = 10

// ɸ(PrimeNumber) = PrimeNumber-1 (it is very easy to understand because gcd with Prime will be 1 so from 1 to Prime-1 will be counted)


// ɸ(Pˣ) = ? , where is x is positive integer , x >= 1 && P = prime number

// ɸ(2⁵) = 16

// ɸ(3⁵) = 162

/*
* ɸ(Pˣ) = Pˣ - number of integers that are not co prime with P
* ɸ(Pˣ) = Pˣ - number of integers that are mutilple of P
* ɸ(Pˣ) = Pˣ - number of inetgers that are mutilple of P from 1 to Pˣ (suppose 3 how many multiple of 3 from to 100 , ans = 100/3 )
* ɸ(Pˣ) = Pˣ - Pˣ/P
* ɸ(Pˣ) = Pˣ - Pˣ⁻¹
* ɸ(Pˣ) = Pˣ⁻¹(P-1)


*/


// generalizing the formula


// The one property about ETF

// ETF is a multiplicative function

/*
* multiplcative means

for a f(n) arithmetic function

if f(n*m) = f(n) * f(m) , where gcd(n,m) = 1;
*/

/*

using above property we can derive the generlize formula

let ɸ(x) ETF is multiplicative
using it to find ɸ(n)

n = p₁ˣ₁ X p₂ˣ₂ ... pₖˣₖ () it is prime factorization


ɸ(n) = ɸ(p₁ˣ₁ X p₂ˣ₂ ... pₖˣₖ)

since ETF is multiplicative

ɸ(n) = ɸ(p₁ˣ₁) X ɸ(p₂ˣ₂) X ... ɸ(pₖˣₖ)

since pᵢ is prime and we know ɸ(Pˣ) = Pˣ⁻¹(P-1)

ɸ(n) = P₁ˣ₁⁻¹(P₁-1) X P₂ˣ₂⁻¹(P₂-1) ... X Pₖˣₖ⁻¹(Pₖ-1) // generalized formula TC(O) = sqrt(n) * logn


ɸ(n) = P₁ˣ₁((P₁-1)/P₁) X P₂ˣ₂((P₂-1)/P₂) ... X Pₖˣₖ((Pₖ-1)/Pₖ)
ɸ(n) = P₁ˣ₁ * P₂ˣ₂ ...* Pₖˣₖ ((P₁-1)/P₁) X ((P₂-1)/P₂) ... X ((Pₖ-1)/Pₖ)

but P₁ˣ₁ * P₂ˣ₂ ...* Pₖˣₖ = n
ɸ(n) = n * ((P₁-1)/P₁) X ((P₂-1)/P₂) ... X ((Pₖ-1)/Pₖ) // easy to calculate TC(O) = sqrt(n)


*/




55 changes: 55 additions & 0 deletions CPP/algorithms/numbertheory/ETF2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ɸ(n) = n * ((P₁-1)/P₁) X ((P₂-1)/P₂) ... X ((Pₖ-1)/Pₖ)

#include<bits/stdc++.h>
#define raj_01 ios::sync_with_stdio(false);cin.tie(0);
#define speed1 cin.tie(0);
#define speed2 cout.tie(0);
#define print(val) cout << val << endl;
#define for1(i,n) for(int i = 0; i < n ; i++)
#define fork(i,n,k) for(int i = 0; i < n ; i+=k)
#define printcontainer(i,container) for1(i,container.size()) print(container[i])
#define INF 1e18
#define INIT_MATRIX(rows, cols, value) \
int matrix[rows][cols]; \
for (int i = 0; i < rows; ++i) \
for (int j = 0; j < cols; ++j) \
cin >> matrix[i][j]
#define MOD 1000000007
#define ll long long
#define lli long long int
#define vi vector<int>
#define pii pair<int, int>
#define vvi vector<vi>
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define debug(x) cout << #x << " = " << x << endl
#define debug2(x, y) cout << #x << " = " << x << #y << " = " << y << endl
#define debug3(x, y, z) cout << #x << " = " << x << #y << " = " << y << int << #z << " = " << z << endl
using namespace std;

int phi(int n) {
int res = n;
for(int i = 2 ; i * i <= n ; i++) {
if(n%i==0) {
res /= i;
res *= (i-1);
while(n%i==0) n/=i;
}
}
if(n>1) {
res /= n;
res *= (n-1);
}
return res;
}

int main(){
raj_01
int test,n;
cin >> test;
while(test--) {
cin >> n;
cout << phi(n) << endl;
}
return 0;
}
55 changes: 55 additions & 0 deletions CPP/algorithms/numbertheory/ETF3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// efficient way to calculate all etf values from 1 to 10⁶


#include<bits/stdc++.h>
#define raj_01 ios::sync_with_stdio(false);cin.tie(0);
#define speed1 cin.tie(0);
#define speed2 cout.tie(0);
#define print(val) cout << val << endl;
#define for1(i,n) for(int i = 0; i < n ; i++)
#define fork(i,n,k) for(int i = 0; i < n ; i+=k)
#define printcontainer(i,container) for1(i,container.size()) print(container[i])
#define INF 1e18
#define INIT_MATRIX(rows, cols, value) \
int matrix[rows][cols]; \
for (int i = 0; i < rows; ++i) \
for (int j = 0; j < cols; ++j) \
cin >> matrix[i][j]
#define MOD 1000000007
#define ll long long
#define lli long long int
#define vi vector<int>
#define pii pair<int, int>
#define vvi vector<vi>
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define debug(x) cout << #x << " = " << x << endl
#define debug2(x, y) cout << #x << " = " << x << #y << " = " << y << endl
#define debug3(x, y, z) cout << #x << " = " << x << #y << " = " << y << int << #z << " = " << z << endl
using namespace std;

int ETF[1000001];

void init() {
ETF[0] = -1; // not defined for 0
for(int i = 1 ; i < 1000001 ; i++) ETF[i] = i;

for(int i = 2 ; i < 1000001 ; i++) {
if(ETF[i] == i) {
for(int j = i ; j < 1000001 ;j += i) {
ETF[j] /= i;
ETF[j] *= (i-1);
}
}
}
}
int main(){
raj_01
init();
int t,n;
cin >> t;
while(t--) {
cin >> n , cout << ETF[n] << endl;
}
return 0;
}
30 changes: 30 additions & 0 deletions CPP/algorithms/numbertheory/ETF4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
#define raj_01 ios::sync_with_stdio(false);cin.tie(0);
#define speed1 cin.tie(0);
#define speed2 cout.tie(0);
#define print(val) cout << val << endl;
#define for1(i,n) for(int i = 0; i < n ; i++)
#define fork(i,n,k) for(int i = 0; i < n ; i+=k)
#define printcontainer(i,container) for1(i,container.size()) print(container[i])
#define INF 1e18
#define INIT_MATRIX(rows, cols,value) \
int matrix[rows][cols]; \
for (int i = 0; i < rows; ++i) \
for (int j = 0; j < cols; ++j) \
matrix[i][j] = value;
#define MOD 1000000007
#define ll long long
#define lli long long int
#define vi vector<int>
#define pii pair<int, int>
#define vvi vector<vi>
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define debug(x) cout << #x << " = " << x << endl
#define debug2(x, y) cout << #x << " = " << x << #y << " = " << y << endl
#define debug3(x, y, z) cout << #x << " = " << x << #y << " = " << y << int << #z << " = " << z << endl
using namespace std;
int main(){
raj_01
return 0;
}
115 changes: 115 additions & 0 deletions CPP/algorithms/numbertheory/ETF5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// problem to be solved is

// ans = Σ (1 to N) gcd(i , N) // for q queries


// observation one = GCD(i,N) = one of the divisors of N

// Assume N = 12

// gcd(1,12) = 1
// gcd(2,12) = 2
// gcd(3,12) = 3
// gcd(4,12) = 4
// gcd(5,12) = 1
// gcd(6,12) = 6
// gcd(7,12) = 1
// gcd(8,12) = 4
// gcd(9,12) = 3
// gcd(10,12) = 2
// gcd(11,12) = 1
// gcd(12,12) 12

// sum of gcds = 1 + 2 + 3 + 4 + 1 + 6 +1 + 4 +3 +2 + 1 +12

// sum of gcds = 1*4 + 2*2 + 3*2 + 4*2 + 12*1

// we can say that

// sum of gcd = (for divisor 1 * how many nymber are from 1 to 12 have gcd equal divisor(1))


// like for divisor 2 , how many numbers from 1 to 12 have gcd 2 that will be the contibution of gcd 2 with that count of numbers

// this is the way to calculate gcd sum

// for x1 , x2 , ...xm for a divisor d such that gcd(xi , N) = d

// have to find m (count)

// gcd(xi/d , N/d) = 1;

// means how many xi/d are there in 1 to N such that their gcd with N/D is 1

// 1 means co prime ==> co prime count ==> ETF(N/d) that is the answer

#include<bits/stdc++.h>
#define raj_01 ios::sync_with_stdio(false);cin.tie(0);
#define speed1 cin.tie(0);
#define speed2 cout.tie(0);
#define print(val) cout << val << endl;
#define for1(i,n) for(int i = 0; i < n ; i++)
#define fork(i,n,k) for(int i = 0; i < n ; i+=k)
#define printcontainer(i,container) for1(i,container.size()) print(container[i])
#define INF 1e18
#define INIT_MATRIX(rows, cols, value) \
int matrix[rows][cols]; \
for (int i = 0; i < rows; ++i) \
for (int j = 0; j < cols; ++j) \
cin >> matrix[i][j]
#define MOD 1000000007
#define ll long long
#define lli long long int
#define vi vector<int>
#define pii pair<int, int>
#define vvi vector<vi>
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define debug(x) cout << #x << " = " << x << endl
#define debug2(x, y) cout << #x << " = " << x << #y << " = " << y << endl
#define debug3(x, y, z) cout << #x << " = " << x << #y << " = " << y << int << #z << " = " << z << endl
using namespace std;


int ETF[1000001];

void init() {
ETF[0] = -1; // not defined for 0
for(int i = 1 ; i < 1000001 ; i++) ETF[i] = i;

for(int i = 2 ; i < 1000001 ; i++) {
if(ETF[i] == i) {
for(int j = i ; j < 1000001 ;j += i) {
ETF[j] /= i;
ETF[j] *= (i-1);
}
}
}
}

int getCount(int d , int N) {
return ETF[N/d];
}


int main(){
raj_01
init();
int t , N;
cin >> t;
while(t--){
cin >> N;
int res = 0;
for(int i = 1 ; i*i<=N ; i++) {
if(N%i==0) {
int d1 = i , d2 = N/i;
res += (d1*getCount(d1,N));
if(d1 != d2) res += (d2*getCount(d2,N));
}
} // sqrt(n) = TC(O )
cout << res << endl;
}
return 0;
}


Loading