diff --git a/Basic_math/Description.md b/Basic_math/Description.md new file mode 100644 index 00000000..09df9eb6 --- /dev/null +++ b/Basic_math/Description.md @@ -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 \ No newline at end of file diff --git a/Basic_math/Divisor.cpp b/Basic_math/Divisor.cpp new file mode 100644 index 00000000..8cb3328d --- /dev/null +++ b/Basic_math/Divisor.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +void findDivisor(int n){ + cout<<"The Divisors of "<>n; + findDivisor(n); + return 0; +} + +//Time Complexity: O(sqrt(n)); +//Space Complexity: O(1); \ No newline at end of file diff --git a/Basic_math/Hcf.cpp b/Basic_math/Hcf.cpp new file mode 100644 index 00000000..c83c90dc --- /dev/null +++ b/Basic_math/Hcf.cpp @@ -0,0 +1,19 @@ +#include +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 "< +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 "< +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 "< +using namespace std; +int countPrime(int n){ + if(n==0) return 0; + int count=0; + vectorprime(n,true); + prime[0]=prime[1]=false; + for(int i=2;i>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)<