Given an integer n
, return true
if and only if it is an Armstrong number.
The k
-digit number n
is an Armstrong number if and only if the kth
power of each digit sums to n
.
Example 1:
Input: n = 153 Output: true Explanation: 153 is a 3-digit number, and 153 = 13 + 53 + 33.
Example 2:
Input: n = 123 Output: false Explanation: 123 is a 3-digit number, and 123 != 13 + 23 + 33 = 36.
Constraints:
1 <= n <= 108
program armstrong_numbers
implicit none
integer :: n, k, i, sum
logical :: is_armstrong
! Example 1:
n = 153
call check(is_armstrong(n), .true.)
! Example 2:
n = 123
call check(is_armstrong(n), .false.)
contains
function is_armstrong(n) result(res)
implicit none
integer, value :: n
integer :: k, i, sum
logical :: res
k = floor(log10(real(n))) + 1
sum = 0
do i = 1, k
sum = sum + (mod(n, 10)**i)
n = n / 10
end do
res = sum == n
end function is_armstrong
subroutine check(actual, expected)
implicit none
logical, value :: actual, expected
if (actual .eqv. expected) then
write (*, '(A)') "PASSED"
else
write (*, '(A)') "FAILED"
end if
end subroutine check
end program armstrong_numbers
temp.f95:17:26: 17 | function is_armstrong(n) result(res) | 1 Error: Procedure βis_armstrongβ at (1) has an explicit interface from a previous declaration temp.f95:18:21: 18 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:19:27: 19 | integer, value :: n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:20:28: 20 | integer :: k, i, sum | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:21:22: 21 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:37: 23 | k = floor(log10(real(n))) + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:24:15: 24 | sum = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:25:19: 25 | do i = 1, k | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:26:39: 26 | sum = sum + (mod(n, 10)**i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:27:22: 27 | n = n / 10 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:11: 28 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:29:22: 29 | res = sum == n | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:7: 30 | end function is_armstrong | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:33:21: 33 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1)
```
program armstrong_numbers
implicit none
integer :: n, k, i, sum
logical :: is_armstrong
! read input
read *, n
! check if n is an Armstrong number
is_armstrong = .false.
k = floor(log10(real(n))) + 1
do i = 1, k
sum = 0
do while (n > 0)
sum = sum + (mod(n, 10) ** i)
n = n / 10
end do
if (sum == n) then
is_armstrong = .true.
exit
end if
end do
! output result
if (is_armstrong) then
write (*, '(A)') "Yes"
else
write (*, '(A)') "No"
end if
end program armstrong_numbers
```
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 is_armstrong(n: int) -> bool:
power_sum = 0
original = n
k = len(str(n))
while n > 0:
digit = n % 10
power_sum += digit ** k
n //= 10
return power_sum == original
power_sum
to 0 and store the input number into a variable called original
.k
.k
th power of the digit to power_sum
.power_sum
is equal to the original number. If it is, then the number is an Armstrong number, so return true
, otherwise return false
.bool isArmstrong(int n) {
int power_sum = 0, original = n;
int k = to_string(n).length();
while (n > 0) {
int digit = n % 10;
power_sum += pow(digit, k);
n /= 10;
}
return power_sum == original;
}
power_sum
to 0 and store the input number into a variable called original
.k
.k
th power of the digit to power_sum
.power_sum
is equal to the original number. If it is, then the number is an Armstrong number, so return true
, otherwise return false
.