Module: (Python) Subroutines: Procedures and Functions - 2


Problem

5/8

Logic functions. Perfect Numbers

Theory Click to read/hide

Logic functions

Often programmers use boolean functions that return boolean values ​​"true" or "false" (trueor false)
Such functions are useful for   check some property.
Let's consider two examples of writing a logical function that checks if a number is even.
 
Description Subroutine example
1) Better way: the result of the expression n % 2 == 0 will be true (True) or false (False)
def isEven(n):
    return (n % 2 == 0)
2) You can write it like that, but it’s better not to do a longer record anyway
def isEven(n):
    if n % 2 == 0:
        return true;
    else:
        return False

And the last note about working with functions and procedures: the number of functions and procedures in the program is not limited. In addition, one subroutine can call another subroutine and even itself.
 

Problem

A perfect number is a number equal to the sum of all its divisors less than itself (for example, the number 6 = 1 + 2 + 3). Write a program that inputs a natural number N and determines if the number N is perfect . Use  a function to find the sum of the divisors of a number and a logical function to check whether the number is perfect or not.
 

Input
The input string contains a natural number .

Imprint
If the number – perfect, the program should output the word 'YES', otherwise – the word 'NO'.

 

Examples
# Input Output
1 28 YES
2 29 NO