From 57a20cf48ea0d2dcdb63bf50da65977e358189ff Mon Sep 17 00:00:00 2001 From: kavithajm <91771245+kavithajm@users.noreply.github.com> Date: Fri, 19 Nov 2021 22:41:09 +0530 Subject: [PATCH] Adding program to check if the number is an Armstrong number (#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> --- README.md | 10 ++++++ math/armstrong/isarmstrong.go | 32 ++++++++++++++++++ math/armstrong/isarmstrong_test.go | 53 ++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 math/armstrong/isarmstrong.go create mode 100644 math/armstrong/isarmstrong_test.go diff --git a/README.md b/README.md index d8a2b8894..f88a49ea1 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute. 1. [`Result`](./strings/ahocorasick/ahocorasick.go#L9): No description provided. +--- +
+ armstrong + +--- + +##### Functions: + +1. [`IsArmstrong`](./math/armstrong/isarmstrong.go#L14): No description provided. + ---
avl diff --git a/math/armstrong/isarmstrong.go b/math/armstrong/isarmstrong.go new file mode 100644 index 000000000..68055dee8 --- /dev/null +++ b/math/armstrong/isarmstrong.go @@ -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 +} diff --git a/math/armstrong/isarmstrong_test.go b/math/armstrong/isarmstrong_test.go new file mode 100644 index 000000000..bab9601c8 --- /dev/null +++ b/math/armstrong/isarmstrong_test.go @@ -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) + } + }) + } +}