forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisarmstrong.go
32 lines (25 loc) · 809 Bytes
/
isarmstrong.go
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
// isarmstrong.go
// description: Checks if the given number is armstrong number or not
// details: An Armstrong number is a n-digit number that is equal to the sum of each of its digits taken to the nth power.
// ref: https://mathlair.allfunandgames.ca/armstrong.php
// author: Kavitha J
package armstrong
import (
"math"
"strconv"
)
func IsArmstrong(number int) bool {
var rightMost int
var sum int = 0
var tempNum int = number
// to get the number of digits in the number
length := float64(len(strconv.Itoa(number)))
// get the right most digit and break the loop once all digits are iterated
for tempNum > 0 {
rightMost = tempNum % 10
sum += int(math.Pow(float64(rightMost), length))
// update the input digit minus the processed rightMost
tempNum /= 10
}
return number == sum
}