🔥🔥🔥 All Number Theory Topics | Basic to Advance Lavel

Number Theory

  • LCM & GCD
  • Divisors & Prime Number
  • Binary Exponentiation
  • Sieve Alogorithm, Lowest prime factor and Highest prime factor
  • Modular inverse
  • Combinatorics

GCD & LCM

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) {
  if(!a) return b;
  return gcd(b % a, a);
}

ll lcm(ll a, ll b) {
    return a * b / gcd(a, b);
}

int main() {
  cout << gcd(12, 18) << endl;
  cout << gcd(0, 18) << endl;
  cout << lcm(12, 18) << endl;
  return 0;
}

Divisors

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 1e5;
vector<int> divisors[N];

void solve() {
  for(int i = 1; i < N; i++) {
    for(int j = i; j < N; j += i) {
      divisors[j].push_back(i);
    }
  }
}

// brute force find out divisors
void solve(int num) {
  vector<int> div;
  for(int i = 1; i * i <= num; i++) {
    if(num % i == 0) {
      div.push_back(i);
      if(num / i != i) div.push_back(num / i);
    }
  }

  for(auto x : div) cout << x << " ";
}


int main() {
  solve();
  for(int i = 1; i < 10; i++) {
    for(auto x : divisors[i]) cout << x << " ";
    cout << endl;
  }
  solve(36);
  return 0;
}

Binary Exponentiation

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll fastPow(ll a, ll b, ll mod) {
  ll ans = 1;
  a %= mod;

  while(b) {
    if(b&1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}


int main() {
  cout << fastPow(3, 10, 1e9 + 7) << endl;
  return 0;
}

Prime Factors of any number

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;


void primeFactors(int num) {
  vector<int> factors;
  while(num % 2 == 0) {
    factors.push_back(2);
    num /= 2;
  }

  for(int i = 3; i * i <= num; i += 2) {
    while(num % i == 0) {
      factors.push_back(num);
      num /= i;
    }
  }

  if(num > 1) factors.push_back(num);

  for(auto x : factors) cout << x << " ";
}

int main() {
  primeFactors(13); cout << endl;
  primeFactors(24);
}

All basics & Combinatorics

// Author: @rushi_mungse

#include <bits/stdc++.h>
using namespace std;

int mod = 1e9 + 7;
const int MAX = 1e5 + 1;

#define ll long long
#define ull unsigned long long
#define int64 long long int
#define vi vector<int>
#define pii pair<int, int>
#define ppi pair<pii>
#define all(v) v.begin(), v.end()
#define ff first
#define ss second
#define eb emplace_back
#define sz(x) (int(x.size()))
#define mset(dp, x) memset(dp, x, sizeof(dp))

int dir[5] = {0, 1, 0, -1, 0};
int dirI[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dirJ[8] = {0, -1, -1, -1, 0, 1, 1, 1};

ll fact[MAX] = {1};

ll add(ll a, ll b)
{
    return (a % mod + b % mod) % mod;
}

ll sub(ll a, ll b)
{
    return (a % mod - b % mod + mod) % mod;
}

ll mul(ll a, ll b)
{
    return ((a % mod) * (b % mod)) % mod;
}

ll exp(ll a, ll b)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1)
            ans = (ans * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans;
}

ll inv(ll b)
{
    return exp(b, mod - 2) % mod;
}

ll division(ll a, ll b)
{
    return ((a % mod) * (inv(b) % mod)) % mod;
}

ll nCr(ll n, ll r)
{
    if (r > n)
        return 0;
    return division(fact[n], mul(fact[r], fact[n - r]));
}

ll nPr(ll n, ll r)
{
    if (r > n)
        return 0;
    return division(fact[n], fact[n - r]);
}

ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll lcm(ll a, ll b)
{
    return (a * b) / gcd(a, b);
}

void pre(int _mod = mod)
{
    mod = _mod;
    fact[0] = 1;
    for (int i = 1; i < MAX; i++)
    {
        fact[i] = mul(fact[i - 1], i);
    }
}

void solve() {
    cout << "write code here" << "\n";
}

int main() {
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	int tt = 1;
	cin >> tt;
	while (tt--) {
		solve();
	}
	return 0;
}
Comments (3)