Skip to content
Open
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
9 changes: 9 additions & 0 deletions Basic_math/Description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This Basic Math folder, designed to strengthen the mathematical foundation required for solving Data Structure and Algorithm (DSA) problems efficiently.
The folder contains clean, beginner-friendly, and optimized implementations of some of the most essential mathematical algorithms that frequently act as building blocks in competitive programming, coding interviews, and algorithmic problem-solving.

This Folder contains the following algorithm which is very important for every coder.

1.Power of Two Check — Efficient method using Euclidean properties
2.HCF & LCM — Fast calculation using Euclidean Algorithm
3.Prime Numbers (Sieve of Eratosthenes) — Generate all primes up to n efficiently
4.Divisor Calculation — Optimized O(√n) approach
26 changes: 26 additions & 0 deletions Basic_math/Divisor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
using namespace std;

void findDivisor(int n){
cout<<"The Divisors of "<<n<<" are: ";
for(int i=1;i<=sqrt(n);i++){
if(n%i==0){
cout<<i<<" ";
if(i!=n/i){
cout<<n/i<<" ";
}
}
}
cout<<endl;
}

int main(){
int n;
cout<<"Enter the value of n:";
cin>>n;
findDivisor(n);
return 0;
}

//Time Complexity: O(sqrt(n));
//Space Complexity: O(1);
19 changes: 19 additions & 0 deletions Basic_math/Hcf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<bits/stdc++.h>
using namespace std;

int getHCF(int a,int b){
if(b==0) return a;
return getHCF(b,a%b);
}

int main(){
int a,b;
cout<<"Enter the value of a and b: ";
cin>>a>>b;
int answer=getHCF(a,b);
cout<<"The HCF of "<<a<<" and "<<b<<" is "<<answer<<endl;
return 0;
}

//Time Complexity: O(log(b));
//Space Complexity: O(1);
23 changes: 23 additions & 0 deletions Basic_math/Lcm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<bits/stdc++.h>
using namespace std;

int getHCF(int a,int b){
if(b==0) return a;
return getHCF(b,a%b);
}

int main(){
int a,b;
cout<<"Enter the value of a and b: ";
cin>>a>>b;
int HCF=getHCF(a,b);

// LCM=(a*b)/hcf(a,b);

int LCM=(a*b)/HCF;
cout<<"The LCM of "<<a<<" and "<<b<<" is "<<LCM<<endl;
return 0;
}

//Time Complexity: O(log(b));
//Space Complexity: O(1);
27 changes: 27 additions & 0 deletions Basic_math/Power.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<bits/stdc++.h>
using namespace std;

int getPower(int a,int b){
int ans=1;
while(b>0){
if(b&1){
ans=ans*a;
}
a=a*a;
b/=2;
}
return ans;
}

int main(){
int a,b;
cout<<"Enter a and b: ";
cin>>a>>b;
int answer=getPower(a,b);
cout<<"The value of "<<a<<" to the power "<<b<< " is "<<answer<<endl;
return 0;
}


// Time complexity=O(log(b));
// Space Complexity=O(1);
32 changes: 32 additions & 0 deletions Basic_math/Prime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<bits/stdc++.h>
using namespace std;
int countPrime(int n){
if(n==0) return 0;
int count=0;
vector<bool>prime(n,true);
prime[0]=prime[1]=false;
for(int i=2;i<n;i++){
if(prime[i]){
count++;
int j=2*i;
while(j<n){
prime[j]=false;
j+=i;
}
}
}
return count;
}

int main(){
int a,b;
cout<<"Enter a and b :";
cin>>a>>b;
int ans1=countPrime(a+1);
int ans2=countPrime(b);
cout<<"The total number of prime number between a and b is:"<<(ans2-ans1)<<endl;
return 0;
}

//Time Complexity: O(nlog(log(n)));
//Space Complexity: O(n);