Skip to content

MATH #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2022
Merged

MATH #232

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
24 changes: 24 additions & 0 deletions Maths/CountDigit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;

int countDigits(int n)
{
return (log10(n) + 1);
}

int main()
{
int n;
cin >> n;
if (n == 0)
{
cout << 0;
}
else
{
n = abs(n);
cout << countDigits(n);
}

return 0;
}
44 changes: 44 additions & 0 deletions Maths/highest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// You are given a 2D string array A of dimensions N x 2,

// where each row consists of two strings: first is the name of the student, second is their marks.

// You have to find the maximum average mark. If it is a floating point, round it down to the nearest integer less than or equal to the number.

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

int main()
{
int n;
cin >> n;
vector<vector<string>> A(n, vector<string>(2));
for (int i = 0; i < n; i++)
{
cin >> A[i][0] >> A[i][1];
}

map<string, int> m;
map<string, int> count;
for (int i = 0; i < n; i++)
{
m[A[i][0]] += stoi(A[i][1]);
cout << m[A[i][0]] << endl;
count[A[i][0]]++;
}
int max = 0;
string name;
for (auto i : m)
{
// cout << i.first << " " << i.second << endl;
// cout << i.first << " " << round(i.second / count[i.first]) << endl;
if (round (1.0 * i.second / count[i.first]) > max)
{
max = round(i.second / count[i.first]);
name = i.first;
}
}
cout << name << " " << max << endl;

return 0;
}

38 changes: 38 additions & 0 deletions Maths/nextSimilar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Given a number A in a form of string.

// You have to find the smallest number that has same set of digits as A and is greater than A.

// If A is the greatest possible number with its set of digits, then return -1.

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

int main()
{
string A;
cin >> A;
int n = A.length();
int i;
for (i = n - 1; i > 0; i--)
{
if (A[i] > A[i - 1])
break;
}
if (i == 0)
{
cout << -1 << endl;
return 0;
}
int x = A[i - 1], smallest = i;
for (int j = i + 1; j < n; j++)
{
if (A[j] > x && A[j] < A[smallest])
smallest = j;
}

swap(A[smallest], A[i - 1]);
sort(A.begin() + i, A.end());
cout << A << endl;

return 0;
}
20 changes: 20 additions & 0 deletions Maths/power.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <bits/stdc++.h>

using namespace std;

int power(int m,int n){
if(n==0){
return 1;

}
if(n%2==0){
return power(m*m,n/2);
}
return m*power(m*m,(n-1)/2);
}

int main(){
int a,b;
cin>>a>>b;
cout<<pow(a, b);
}
30 changes: 30 additions & 0 deletions Maths/powerIterative.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 2 ^ 11 , x^n
// 11 -> 1 0 1 1 ;
// x = 2 , n=11 , res =1
// 11/2 = 5 -> 1 res = res*x =2 n=5; x=x*x=4
// 5/2 = 2 -> 1 res = 2 *4 = 8 n=2 x = 16
// 2/2 =1 -> 0 res = 8 n=1 x = 16*16 =256
// 1/2 =0 -> 1 res = 8 *256
// res = 2 * 4 *256

#include <bits/stdc++.h>

using namespace std;

int IterativePower(int x,int n){
int res = 1;
while( n ){
if(n & 1){
res = res * x ;
}
x = x * x ;
n = n>>1;
}
return res;
}

int main(){
int x,n;
cin>>x>>n;
cout<<IterativePower(x, n);
}
47 changes: 47 additions & 0 deletions Maths/primeFactor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <bits/stdc++.h>
using namespace std;

void PrimeFactor(int n)
{
if (n == 1)
return;

int i = 2;
while (n % i == 0)
{
cout << i << ' ';
n = n / i;
}

i = 3;
while (n % i == 0)
{
cout << i << ' ';
n = n / i;
}

for (int i = 5; i * i <= n; i = i + 6)
{
while (n % i == 0)
{
cout << i << ' ';
n = n / i;
}
while (n % (i + 2) == 0)
{
cout << i + 2 << ' ';
n = n / (i + 2);
}
}
if (n > 3)
cout << n << " ";
}

int main()
{
int n;
cin >> n;
PrimeFactor(n);

return 0;
}
30 changes: 30 additions & 0 deletions Maths/primeNo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;

bool isPrime(int n)
{
if (n == 1)
return false;

if (n == 2 || n == 3)
return true;

if ((n % 2 == 0 )|| (n % 3) == 0)
return false;

for (int i = 5; i * i <= n; i = i + 6)
{
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
}
return true;
}

int main()
{
int n;
cin >> n;
cout << isPrime(n);

return 0;
}
23 changes: 23 additions & 0 deletions Maths/reverseInteger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// You are given an integer N and the task is to reverse the digits of the given integer. Return 0 if the result overflows and does not fit in a 32 bit signed integer

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

int main()
{
int n;
cin >> n;
int rev = 0;
while (n != 0)
{
int rem = n % 10;
n /= 10;
if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && rem > 7))
return 0;
if (rev < INT_MIN / 10 || (rev == INT_MIN / 10 && rem < -8))
return 0;
rev = rev * 10 + rem;
}
cout << rev << endl;
return 0;
}
22 changes: 22 additions & 0 deletions Maths/seiveOfEratothenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <bits/stdc++.h>
using namespace std;

void printAllPrime(int n){
vector<bool> isPrime(n+1,true);
for(int i=2; i<=n ; i++){
if(isPrime[i]){
cout<<i<<" ";
for(int j= i*i; j<=n; j=j+i){
isPrime[j] = false;
}
}
}

}

int main(){
int n;
cin>>n;
printAllPrime(n);
return 0;
}
21 changes: 21 additions & 0 deletions Maths/trailingZeroInFactorial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <bits/stdc++.h>
using namespace std;

int countTrailingZeroes(int n)
{
int c =1;
int countZeroes = 0;
while((n/(pow(5,c)))){
countZeroes += (n/(pow(5,c)));
c++;
}
return countZeroes;
}

int main()
{
int n;
cin >> n;
cout << countTrailingZeroes(n);
return 0;
}