Skip to content

Commit

Permalink
Adding program to check if the number is an Armstrong number (TheAlgo…
Browse files Browse the repository at this point in the history
…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
kavithajm and github-action authored Nov 19, 2021
1 parent 9fec515 commit 57a20cf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
1. [`Result`](./strings/ahocorasick/ahocorasick.go#L9): No description provided.


---
</details><details>
<summary> <strong> armstrong </strong> </summary>

---

##### Functions:

1. [`IsArmstrong`](./math/armstrong/isarmstrong.go#L14): No description provided.

---
</details><details>
<summary> <strong> avl </strong> </summary>
Expand Down
32 changes: 32 additions & 0 deletions math/armstrong/isarmstrong.go
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
}
53 changes: 53 additions & 0 deletions math/armstrong/isarmstrong_test.go
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)
}
})
}
}

0 comments on commit 57a20cf

Please sign in to comment.