Perfect Number (INTRN006) : Solution with hints

Hint 1:

Click to view

How do you know whether a number i is a factor of another number n.

Hint 2:

Click to view

What is the range in which the factors of any number n lie? What is the smallest factor of n and what is the largest factor of n?

Hint 3:

Click to view

Observe that factors always occur in pairs. e.g. Consider the factors of 36. If you know that 1 is a factor of 36, you also know that 36 is a factor of 36 (you didn’t need to check it again). Similarly, if you know that 2 is a factor of 36, then so is 18.

Hint 4:

Click to view

Extending the idea of factors occurring in pairs. We can say that if i is a factor of n, then so is n/i.

Hint 5:

Click to view

When do factors start repeating? Till when do you need to check that if i is a factor of n.
Try to enlist factors of various numbers like 12, 24, 36, 40. And try to figure out a point where the factors start repeating.

Hint 6:

Click to view

Spoiler Alert: This provides the answer to the above hint.

We can see that factors start repeating when i = n / i \Rightarrow i^2 = n \Rightarrow i = \sqrt(n).

Hint 7:

Click to view

Now, you know the range in which you need to check whether i is a factor of n or not. Just sum over all factors and check whether the sum is equal to the original number n or not.

Hint 8:

[details=Click to view]

Try to print the sum of factors. Check whether it is correct for square numbers or not. Are you adding something extra.