If you are looking for a more efficient way to find the perfect rooms, you can read the Wikipedia page on the perfect rooms . In it you will find that unknown odd perfect numbers (and you wonβt find using your method) and that all even perfect numbers have the form:
2^(p - 1)*(2^p - 1) where 2^p - 1 is simple and therefore p is simple. Thus, if you want to find even perfect numbers, check the primitiveness of 2^p - 1 for all primes p , if so 2^(p - 1)*(2^p - 1) perfect.
If you just want to find a few small ideal numbers using a simple loop, you can make your approach more efficient by noting that if i divides num , then num / i . That is, you only need to loop to the square root of num and add the i and num / i pairs to sum . Note that if num is square, the square root of num should only be added once.
Note that if you calculate sum this way, this value will be 2 * num for ideal numbers, not num .
source share