forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding program to check if the number is an Armstrong number (TheAlgo…
…rithms#442) * Adding program to check if the number is an armstrong number * Updated Documentation in README.md * Update loop in isarmstrong Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// isarmstrong_test.go | ||
|
||
package armstrong | ||
|
||
import "testing" | ||
|
||
var testCases = []struct { | ||
name string // test description | ||
input int // user input | ||
expected bool // expected return | ||
}{ | ||
{ | ||
"negative number: Not an armstrong number", | ||
-140, | ||
false, | ||
}, | ||
{ | ||
"positive number: Not an armstrong number", | ||
23, | ||
false, | ||
}, | ||
{ | ||
"smallest armstrong number", | ||
0, | ||
true, | ||
}, | ||
{ | ||
"smallest armstrong number with more than one digit", | ||
153, | ||
true, | ||
}, | ||
{ | ||
"random armstrong number", | ||
407, | ||
true, | ||
}, | ||
{ | ||
"random armstrong number", | ||
9474, | ||
true, | ||
}, | ||
} | ||
|
||
func TestIsArmstrong(t *testing.T) { | ||
for _, test := range testCases { | ||
t.Run(test.name, func(t *testing.T) { | ||
funcResult := IsArmstrong(test.input) | ||
if test.expected != funcResult { | ||
t.Errorf("Expected answer '%t' for the number '%d' but answer given was %t", test.expected, test.input, funcResult) | ||
} | ||
}) | ||
} | ||
} |