A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x
is an integer that can divide x
evenly.
Given an integer n
, return true
if n
is a perfect number, otherwise return false
.
Example 1:
Input: num = 28 Output: true Explanation: 28 = 1 + 2 + 4 + 7 + 14 1, 2, 4, 7, and 14 are all divisors of 28.
Example 2:
Input: num = 7 Output: false
Constraints:
1 <= num <= 108
program main
implicit none
integer :: n
logical :: result
! Example 1
n = 28
result = is_perfect(n)
if (result) then
print '(A, I0, A)', 'Perfect number: ', n, '.'
else
print '(A, I0, A)', 'Not a perfect number: ', n, '.'
end if
! Example 2
n = 7
result = is_perfect(n)
if (result) then
print '(A, I0, A)', 'Perfect number: ', n, '.'
else
print '(A, I0, A)', 'Not a perfect number: ', n, '.'
end if
contains
function is_perfect(n) result(res)
implicit none
integer, intent(in) :: n
logical :: res
integer :: i
res = .false.
do i = 1, n - 1
if (mod(n, i) == 0) then
res = res .or. is_perfect(i)
end if
end do
if (n == sum(1, n)) then
res = .true.
end if
end function is_perfect
end program main
temp.f95:36:30: 36 | res = res .or. is_perfect(i) | 1 Error: Function โis_perfectโ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:39:21: 39 | if (n == sum(1, n)) then | 1 Error: โarrayโ argument of โsumโ intrinsic at (1) must be an array
```
program perfect_number
implicit none
integer :: n, i, sum
logical :: is_perfect_number
! read input
read *, n
! initialize sum
sum = 0
! loop through divisors
do i = 1, n
if (mod(n, i) == 0) then
sum = sum + i
end if
end do
! check if sum is equal to n
if (sum == n) then
is_perfect_number = .true.
else
is_perfect_number = .false.
end if
! print output
if (is_perfect_number) then
print *, "Yes"
else
print *, "No"
end if
end program perfect_number
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:34:2: 34 | ``` | 1 Error: Invalid character in name at (1)
def checkPerfectNumber(num: int) -> bool:
if num <= 1:
return False
sum = 1
i = 2
while i * i <= num:
if num % i == 0:
sum += i
if i * i != num:
sum += num // i
i += 1
return sum == num
The algorithm checks if a given number num
is a perfect number by iterating through the positive divisors of the number and adding them to a sum. If the number is less than or equal to 1, it returns false as these are not perfect numbers. The loop starts from 2 and goes until the square root of the number. If i
is divisible by num
, it's added to the sum. Also, check if i * i
is not equal to num
, we add num / i
to the sum, this is because we don't want to add the same divisor twice. In the end, it checks if the calculated sum is equal to the given number. If it is, return true, else return false.
bool checkPerfectNumber(int num) {
if (num <= 1) return false;
int sum = 1;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
sum += i;
if (i * i != num) sum += num / i;
}
}
return sum == num;
}
The algorithm checks if a given number num
is a perfect number by iterating through the positive divisors of the number and adding them to a sum. If the number is less than or equal to 1, it returns false as these are not perfect numbers. The loop starts from 2 and goes until the square root of the number. If i
is divisible by num
, it's added to the sum. Also, check if i * i
is not equal to num
, we add num / i
to the sum, this is because we don't want to add the same divisor twice. In the end, it checks if the calculated sum is equal to the given number. If it is, return true, else return false.