Arcot

ch7 ex2

Jan 12th, 2022 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. #7.2.1
  2. def factors(num):
  3. listfactors=[]
  4. for i in range(1, num+1):
  5. if num%i==0:
  6. listfactors.append(i)
  7. return listfactors
  8.  
  9. #7.2.2
  10. def prime(n):
  11. if len(factors(n))==2:
  12. return True
  13. else:
  14. return False
  15.  
  16. #7.2.3
  17. def perfect_number(n):
  18. sum=0
  19. for i in factors(n):
  20. sum+=i
  21. sum=sum-n
  22.  
  23. if sum==n:
  24. return True
  25. else:
  26. return False
  27. print(perfect_number(6))
Add Comment
Please, Sign In to add comment