-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_fuctions.cpp
87 lines (67 loc) · 1.82 KB
/
math_fuctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
//------------------GCD-----------------
// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find gcd of array of
// numbers
int findGCD(int arr[], int n)
{
int result = arr[0];
for (int i = 1; i < n; i++)
result = gcd(arr[i], result);
return result;
}
//------------------GCD-end----------------
//--------------------PERMUTATION-------------------
//print permutations
void print_permutations(int n){
n = 3;
//fill the vect (must be sorted to get all permuations)
vector <int> vect;
for (int i = 1; i <= n; i++)
vect.push_back(i);
//print the vect
for (int i=0; i<vect.size(); i++)
cout << vect[i] << " ";
cout <<endl;
//print till permutation possible.
while(next_permutation(vect.begin(), vect.end()) ){
for (int i=0; i<n; i++)
cout << vect[i] << " ";
cout<<endl;
}
}
//--------------------PERMUTATION-end-----------------
//---------------------prime-factors------------------
vector<int> primeFactors(int n) {
vector<int> factors;
// Print the number of 2s that divide n
while (n%2 == 0){
factors.push_back(2);
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i+2) {
// While i divides n, print i and divide n
while (n%i == 0) {
factors.push_back(i);
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
factors.push_back(n);
return factors;
}
//--------------------primt-factors-end----------------