Question: So I am supposed to write a Python program that will identify and print all the perfect numbers in some closed interval [ 2, n ], one per line. We only have to use nested while loops/ if-else statements. I did it somehow using a for loop, but can't figure out the same using a while loop. I'd appreciate your help if you could show me how to translate my code into a while loop. Thanks guys. Here's what I have:

```
limit = int(input("enter upper limit for perfect number search: "))

for n in range(2, limit + 1):
    sum = 0
    for divisor in range(1, n):
        if not n % divisor:
            sum += divisor
        if sum == n:
            print(n, "is a perfect number")

```

Answer: 
You can replace your `for` loops with the following:

```
n = 2
while n < limit + 1:
   ...
   divisor = 1
   while divisor < n:
      ...
      divisor += 1
   ...
   n += 1

```

Hint: You can also use `n/2` as the upper limit for the second loop as any divisor of `n` cannot be greater than `n/2`.
I am sharing this codes for alternative. Perfect number is of the form [2^{n-1}(2^{n}-1)] where 2^{n}-1 and n both are prime numbers.

```
for num in range(2,101):
if all(num%i!=0 for i in range(2,num)):
    num_2=num*2-1
    for num_2 in range(2,101):
        if all(num_2%i!=0 for i in range(2,num_2)):
            MükemmelSayı=2**(num-1)*(2**num-1)
            print(MükemmelSayı)
            break
        break

```