forked from priyankchheda/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivisors.cpp
39 lines (36 loc) · 808 Bytes
/
divisors.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
/**
* Find all divisors of a natural number
*
* Given a natural number n, print all distinct divisors of it.
*
* Examples:
* Input : n = 10
* Output: 1 2 5 10
*
* Input: n = 100
* Output: 1 2 4 5 10 20 25 50 100
*
* Input: n = 125
* Output: 1 5 25 125
*
* URL: https://www.geeksforgeeks.org/find-divisors-natural-number-set-1/
* https://www.geeksforgeeks.org/find-all-divisors-of-a-natural-number-set-2/
*
*/
#include <iostream>
void getDivisors(int n)
{
for (int i = 1; i * i <= n; i++) { // time complexity: sqrt(n)
if (n % i == 0) {
if (n/i == i)
std::cout << i << " ";
else std::cout << i << " " << n/i << " ";
}
}
}
int main()
{
int n = 100;
getDivisors(n);
return 0;
}