Skip to content

Commit b0cb443

Browse files
authored
Dimik divisor tutorial Added (#469)
* Dimik divisor tutorial Added * Dimik divisor tutorial Added
1 parent 921c3a5 commit b0cb443

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

dimik-divisor/en.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## Dimik Divisor
2+
3+
### What the problem wants
4+
5+
Basically you will be given a number you need to find all the divisor from it
6+
7+
You can find more about divisors [here](https://www.splashlearn.com/math-vocabulary/division/divisor)
8+
9+
### Solution
10+
11+
The most basic way to solve this is as following
12+
13+
- Start a loop from 1 to N , Where N is the number given
14+
- Check if the any number can divide N with a remainder of 0
15+
``` 6 % 3 == 0 ``` That means the remainder of 6 after dividing it by 3 is 0
16+
- You can print them as they are the divisor
17+
> Be careful about newline and extra space. If you are stuck with that problem see the code to know how to handle it
18+
19+
### CPP Code
20+
21+
```cpp
22+
23+
#include<bits/stdc++.h> // includes necessary library
24+
using namespace std;
25+
#define ll long long int
26+
int main()
27+
{
28+
int test;
29+
cin>>test;
30+
for(int t=1;t<=test;t++)
31+
{
32+
int num;
33+
cin>>num;
34+
cout<<"Case "<<t<<": "; // printing Case numbers
35+
for(int i=1;i<num;i++)
36+
{
37+
if(num%i==0)
38+
{
39+
cout<<i<<" ";
40+
}
41+
}
42+
cout<<num<<endl; // the last divisor will be the number itself
43+
}
44+
return 0;
45+
}
46+
47+
```
48+
49+
### Efficiency
50+
51+
This solution can be optimized
52+
53+
Here is an observation
54+
55+
When we look for divisors of a number, they always come in pairs. For example, consider the number 36. The divisors of 36 are: 1 and 36, 2 and 18, 3 and 12, 4 and 9, 6 and 6. Notice that the pairs are formed symmetrically around the square root of the number (6 in this case).
56+
57+
By iterating up to the square root of the number, we ensure that we cover all possible divisors without redundant checks. If we go beyond the square root, we would end up checking the same pairs of divisors in reverse order. This would not yield any new divisors and would waste computational resources.
58+
59+
Therefore, by limiting the iteration to the square root of the number, we optimize the algorithm by avoiding unnecessary iterations and improve its efficiency.
60+
61+
Note that if a number is a perfect square (e.g., 9, 16, 25), the square root will be one of the divisors, and it will be added to the list twice in the algorithm implementation (once in the iteration and once in the check for the division result).
62+
63+
So to find divisors of a number the following algorithm can be followed
64+
65+
- Start with an empty list to store the divisors.
66+
- Input the number for which you want to find the divisors.
67+
- Find the square root of the input number and store it in a variable, let's say sqrtNum. This will be the upper limit for the iteration.
68+
Iterate from 1 to sqrtNum (inclusive):
69+
- For each iteration:
70+
- a. Check if the current iteration value (i) is a divisor of the input number.
71+
- b. To check if i is a divisor, calculate the remainder of the division of the input number by i. If the remainder is 0, then i is a divisor.
72+
- c. If i is a divisor, add it to the list of divisors.
73+
- d. Check if i is different from the division result of the input number by i. If they are different, it means there is another divisor (num / i). In that case, add the division result to the list of divisors as well.
74+
Return the list of divisors.

0 commit comments

Comments
 (0)