-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforperfectnum1.py
More file actions
49 lines (38 loc) · 1.19 KB
/
forperfectnum1.py
File metadata and controls
49 lines (38 loc) · 1.19 KB
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
40
41
42
43
44
45
46
47
48
49
# check a number if perfect or not
# using optimized solution
n=int(input('enter number: '))
sum=0
for num in range(1,n//2+1):
if n%num==0:
sum+=num
if sum==n:
print('n is a perfect number')
else:
print('n is not a perfect number')
'''
Step 1: Initialization
Input number n = 6.
Initialize sum = 0 to store sum of proper divisors.
Step 2: Start for loop over range(1, n//2 + 1) → range(1, 4) i.e. 1, 2, 3.
Proper divisors of n will be found in this range.
Step 3: First iteration (num = 1)
Check if 6 % 1 == 0 → True (1 is divisor).
Add num (1) to sum → sum = 0 + 1 = 1.
Step 4: Second iteration (num = 2)
Check if 6 % 2 == 0 → True (2 is divisor).
Add num (2) to sum → sum = 1 + 2 = 3.
Step 5: Third iteration (num = 3)
Check if 6 % 3 == 0 → True (3 is divisor).
Add num (3) to sum → sum = 3 + 3 = 6.
Step 6: End of loop
Loop completes after num reaches 3.
Step 7: Check if sum == n
sum = 6 equals n = 6, so condition true.
Step 8: Print output
Print "n is a perfect number".
Summary:
The loop finds all divisors up to n//2.
Divisors are added to sum.
If sum equals the number itself, it is perfect.
For n=6, the divisors are 1, 2, 3 whose sum equals 6, so 6 is perfect.
'''