-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.4_basicMaths.cpp
70 lines (64 loc) · 1.16 KB
/
3.4_basicMaths.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
#include <bits/stdc++.h>
using namespace std;
int reverseNum(int n)
{
int num = 0;
while (n > 0)
{
int lastDigit = n % 10;
num = (num * 10) + lastDigit;
n = n / 10;
}
return num;
}
string palindrome(int n)
{
bool check = (n == reverseNum(n));
if (check)
{
return "yes";
}
else
{
return "no";
}
}
vector<int> divisor(int n)
{
// standard
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
cout << i << " ";
}
}
cout << endl;
// other approach
vector<int> ans;
// instead of sqrt, you can use i <= n*n
// now program doesn't call sqrt and it reduces time
for (int i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
ans.push_back(i);
if ((n / i) != i) // for perfect squares
{
ans.push_back(n / i);
}
}
}
sort(ans.begin(), ans.end());
return ans;
}
int main()
{
int n = 38200;
vector<int> v = divisor(n);
for (auto ite = v.begin(); ite != v.end(); ite++)
{
cout << *(ite) << " ";
}
return 0;
}