< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • Codechef - Basic Math
  • Q1:
  • Notes
  • Q1: Count Prime
  • Segmented Sieve
  • Q2: Must read -> Euclid GCD Algorithm
  • Topic: Modular Arithmetic
  • Q3: Modular Exponentiation --> Notes Page # 05

Was this helpful?

Basic Math - Codechef

Previous2-D ArraysNextRecursion

Last updated 1 year ago

Was this helpful?

Q1:

The chef wants to become fit for which he decides to walk to the office and return home by walking. It is known that Chef's office is X km away from his home.

If his office is open 5 days a week, find the number of kilometers the Chef travels through office trips in a week.

#include <iostream>
using namespace std;

int main() {
	int X;
	int tt;
	cin >> tt;
	while(tt--) {
	    cin>>X;
	    cout << (X*2)*5 << endl;
	}
	return 0;
}
Solve by using Sieve of Eratosthenes
int countPrimes(int n) {
    int cnt = 0;
    vector<bool> Prime(n+1, true);
    Prime[0] = Prime[1] = false;
    
    for(int i = 2; i < n; i++){
        if(Prime[i]){
            cnt++;
            for(int j = 2*i; j < n; j+=i){
                Prime[j] = false; 
            }
        }
    }
    return cnt;
}
      (n/2 + n/3 + n/5 + n/7...)
       n * (1/2 + 1/3 + 1/5 + 1/7...) //Harmonic Progression of Prime Number
T.C = O(n* Log(logn))

Segmented Sieve

GCD Formula: Find gcd until one of the parameters becomes zero

gcd(a - b, b)

gcd(a % b, b)

LCM & GCD Relation: LCM(a, b) * GCD(a, b) = a * b

Greatest Common Factor & Highest Common Factor
int GCD(int a, int b){
    if(a==0)
        return b;
    if(b==0)
        return a;
    while(a != b){
        if(a>b)
            a -= b;
        else{
            b -= a;
        }
    }
    return a;
    
    // recursively solved

    //Method - I
    if(a>b)
        return GCD(a-b, b);
    else
        return GCD(b-a, a);
    
    //Method - II --> for less number of iterations GCD(a,b) = GCD(a%b,b)
    if(a>b)
        return GCD(a%b, b);
    else
        return GCD(b%a, a);
}
int main(){
    int ans = GCD(25, 72);
    cout << ans;
}

( a%m + b%m) % m = (a + b) % m

(a%m − b%m) % m = (a − b) % m

(a%m * b%m) % m = (a * b) % m

Using Fast Exponentiation Algorithm T.C: O(logn)

int modularExponentiation(int n, int pow, int m) {
	int ans = 1;
	while(pow > 0){
		if(!(pow%2 == 0)){
			ans = (1LL * ans * n)%m;
		}
		n = (1LL * n * n)%m;
		pow /= 2;
	}
	return ans;
}

Q1:

Q2: Must read ->

Topic:

Q3: --> Notes Page # 05

👨‍🍳
Codechef - Basic Math
Notes
Count Prime
Euclid GCD Algorithm
Modular Arithmetic
Modular Exponentiation