Skip to content
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

made some changes to the mathematical problems/algorithms. #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
25 changes: 25 additions & 0 deletions Mathematical Algorithms/DecToOctal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>
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;
}
44 changes: 44 additions & 0 deletions Mathematical Algorithms/PythaTriplets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
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;
}
30 changes: 30 additions & 0 deletions Mathematical Algorithms/isArmstrong.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<iostream>
#include<math.h>
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<<power;

while(num>=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;
}
27 changes: 27 additions & 0 deletions Mathematical Algorithms/isFibonaccian.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<iostream>
#include<math.h>
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.";
}
54 changes: 54 additions & 0 deletions Recursion, Backtracking etc/K-Combinations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Petar 'PetarV' Velickovic
Algorithm: K-Combinations
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
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<k-amt) return;
if (amt==k)
{
for (int i=0;i<n;i++) if (inSet[i]) printf("%d ",skup[i]);
printf("\n");
return;
}
inSet[pos] = true;
kCombinations(pos+1,amt+1);
inSet[pos] = false;
kCombinations(pos+1,amt);
}

int main()
{
n = 5, k = 2;
skup[0] = 1;
skup[1] = 2;
skup[2] = 3;
skup[3] = 4;
skup[4] = 5;
kCombinations(0, 0);
return 0;
}
57 changes: 57 additions & 0 deletions Recursion, Backtracking etc/Permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Petar 'PetarV' Velickovic
Algorithm: Permutations
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
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<n;i++) printf("%d ",currPerm[i]);
printf("\n");
}
for (int i=0;i<n;i++)
{
if (!inPerm[i])
{
currPerm[pos] = niz[i];
inPerm[i] = true;
generatePermutations(pos+1);
inPerm[i] = false;
}
}
}

int main()
{
n = 3;
niz[0] = 1;
niz[1] = 2;
niz[2] = 3;
generatePermutations(0);
return 0;
}
51 changes: 51 additions & 0 deletions Recursion, Backtracking etc/Power Set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Petar 'PetarV' Velickovic
Algorithm: Power Set
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
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<n;i++) if (inSet[i]) printf("%d ",skup[i]);
printf("\n");
return;
}
inSet[pos] = false;
powerSet(pos+1);
inSet[pos] = true;
powerSet(pos+1);
}

int main()
{
n = 3;
skup[0] = 1;
skup[1] = 2;
skup[2] = 3;
powerSet(0);
return 0;
}