From c6510d2e0c6a68a8a7ba0e252e302e809efe56f4 Mon Sep 17 00:00:00 2001 From: AkshatAggEm Date: Sat, 12 Oct 2019 00:09:45 +0530 Subject: [PATCH] made some changes to the mathematical problems/algorithms. --- Mathematical Algorithms/DecToOctal.cpp | 25 ++++++++ Mathematical Algorithms/PythaTriplets.cpp | 44 ++++++++++++++ Mathematical Algorithms/isArmstrong.cpp | 30 ++++++++++ Mathematical Algorithms/isFibonaccian.cpp | 27 +++++++++ .../K-Combinations.cpp | 54 ++++++++++++++++++ Recursion, Backtracking etc/Permutations.cpp | 57 +++++++++++++++++++ Recursion, Backtracking etc/Power Set.cpp | 51 +++++++++++++++++ 7 files changed, 288 insertions(+) create mode 100644 Mathematical Algorithms/DecToOctal.cpp create mode 100644 Mathematical Algorithms/PythaTriplets.cpp create mode 100644 Mathematical Algorithms/isArmstrong.cpp create mode 100644 Mathematical Algorithms/isFibonaccian.cpp create mode 100644 Recursion, Backtracking etc/K-Combinations.cpp create mode 100644 Recursion, Backtracking etc/Permutations.cpp create mode 100644 Recursion, Backtracking etc/Power Set.cpp diff --git a/Mathematical Algorithms/DecToOctal.cpp b/Mathematical Algorithms/DecToOctal.cpp new file mode 100644 index 0000000..f4b80f4 --- /dev/null +++ b/Mathematical Algorithms/DecToOctal.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +void decToOct(int num) +{ int arr[50], i = 0; + + while (num != 0) { + arr[i] = num % 8; + num /= 8; + i++; + } + + for (int j = i - 1; j >= 0; j--) + cout << arr[j]; +} + +int main() +{ + int n; + cin >>n; + + decToOct(n); + + return 0; +} \ No newline at end of file diff --git a/Mathematical Algorithms/PythaTriplets.cpp b/Mathematical Algorithms/PythaTriplets.cpp new file mode 100644 index 0000000..f33a799 --- /dev/null +++ b/Mathematical Algorithms/PythaTriplets.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +int main() { + int num; + cin>>num; + + if(num == 1 || num == 2){ + cout<<"-1"; + } + + /* + for pythagorean triplets, + + if n is even, + the triplets could be given by, + n^2/4-1, n and n^2/4+1 + so if a variable say, var = n^2/4-1, then + + a = var + b = n + c = var + 2 + + if n is odd, + the triplets could be given by, + (n^2-1)/2, n and (n^2+1)/2 + so if a variable say, var = (n^2-1)/2, then + + a = var + b = n + c = var + 1 + */ + + else if(num%2==0){ + long m=num/2; + long n=1; + cout<<(m*m-n*n)<<" "<<(m*m+n*n); + }else{ + long m=(num + 1)/2; + long n=(num - 1)/2; + cout<<(2*m*n)<<" "<<(m*m+n*n); + } + return 0; +} \ No newline at end of file diff --git a/Mathematical Algorithms/isArmstrong.cpp b/Mathematical Algorithms/isArmstrong.cpp new file mode 100644 index 0000000..e9eaeb1 --- /dev/null +++ b/Mathematical Algorithms/isArmstrong.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +bool isArmstrong(int num){ + int temp = num, + sum = 0, + power; + + power = std::to_string(temp).length(); + /* + std::to_string(integer) coverts our number into a string so as to operate upon it various string + functionalities + */ + // cout<=1){ + sum = sum + pow((num%10), power); + num = num/10; + } + return(sum == temp); +} + +int main(){ + int n; + cin>>n; + + isArmstrong(n)?cout<<"true":cout<<"false"; + return 0; +} diff --git a/Mathematical Algorithms/isFibonaccian.cpp b/Mathematical Algorithms/isFibonaccian.cpp new file mode 100644 index 0000000..b0e227f --- /dev/null +++ b/Mathematical Algorithms/isFibonaccian.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +//checks if it is perfect square or not +bool isSqrt(int num){ + int s = sqrt(num); + return(s*s == num); +} + +//checks if it the two terms 5n^2 + 4 are perfect squares or not +bool isFibonacci(int n){ + return isSqrt(5*n*n + 4) || isSqrt(5*n*n - 4); +} + +int main(){ + //the main principle behind whether a number is a fibonacci series element or not is, + // say the number is n, it is in fibonacci, if and only if, one or both, (5n^2 + 4) and (5n^2 - 4) are perfect squares. + + int number; + + cout<<"Enter the number which you want to search inside the series: "; + cin>>number; + + isFibonacci(number)? cout<<"Yes the number is present in series." : + cout<<"No, it is not present in the list."; +} diff --git a/Recursion, Backtracking etc/K-Combinations.cpp b/Recursion, Backtracking etc/K-Combinations.cpp new file mode 100644 index 0000000..8038234 --- /dev/null +++ b/Recursion, Backtracking etc/K-Combinations.cpp @@ -0,0 +1,54 @@ +/* + Petar 'PetarV' Velickovic + Algorithm: K-Combinations +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long lld; + +int n, k; +int skup[100]; +bool inSet[100]; + +//Program koji generise sve kombinacije od po K elemenata datog skupa +//Slozenost: O((n choose k)) + +void kCombinations(int pos, int amt) +{ + if (n-pos +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long lld; + +int n; +int niz[100]; +bool inPerm[100]; +int currPerm[100]; + +//Algoritam koji generise sve permutacije datog niza +//Slozenost: O(n!) + +void generatePermutations(int pos) +{ + if (pos == n) + { + for (int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long lld; + +//Metoda koja generise partitivni skup nekog skupa +//Slozenost: O(2^n) + +int n; +int skup[100]; +bool inSet[100]; + +void powerSet(int pos) +{ + if (pos==n) + { + for (int i=0;i