Skip to content

Commit 8022beb

Browse files
committed
Add armstrong-numbers
1 parent 84d65a7 commit 8022beb

File tree

7 files changed

+216
-0
lines changed

7 files changed

+216
-0
lines changed

armstrong-numbers/README.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Armstrong Numbers
2+
3+
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits.
4+
5+
For example:
6+
7+
- 9 is an Armstrong number, because `9 = 9^1 = 9`
8+
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1`
9+
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
10+
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
11+
12+
Write some code to determine whether a number is an Armstrong number.
13+
14+
15+
## Getting Started
16+
17+
Please refer to the [installation](https://exercism.io/tracks/haskell/installation)
18+
and [learning](https://exercism.io/tracks/haskell/learning) help pages.
19+
20+
## Running the tests
21+
22+
To run the test suite, execute the following command:
23+
24+
```bash
25+
stack test
26+
```
27+
28+
#### If you get an error message like this...
29+
30+
```
31+
No .cabal file found in directory
32+
```
33+
34+
You are probably running an old stack version and need
35+
to upgrade it.
36+
37+
#### Otherwise, if you get an error message like this...
38+
39+
```
40+
No compiler found, expected minor version match with...
41+
Try running "stack setup" to install the correct GHC...
42+
```
43+
44+
Just do as it says and it will download and install
45+
the correct compiler version:
46+
47+
```bash
48+
stack setup
49+
```
50+
51+
## Running *GHCi*
52+
53+
If you want to play with your solution in GHCi, just run the command:
54+
55+
```bash
56+
stack ghci
57+
```
58+
59+
## Feedback, Issues, Pull Requests
60+
61+
The [exercism/haskell](https://github.com/exercism/haskell) repository on
62+
GitHub is the home for all of the Haskell exercises.
63+
64+
If you have feedback about an exercise, or want to help implementing a new
65+
one, head over there and create an issue. We'll do our best to help you!
66+
67+
## Source
68+
69+
Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)
70+
71+
## Submitting Incomplete Solutions
72+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cabal-version: 1.12
2+
3+
-- This file has been generated from package.yaml by hpack version 0.33.0.
4+
--
5+
-- see: https://github.com/sol/hpack
6+
--
7+
-- hash: a4833901b954a0a033311786494ec67c394a98266766e3173a879ad937a493d7
8+
9+
name: armstrong-numbers
10+
version: 1.1.0.3
11+
build-type: Simple
12+
13+
library
14+
exposed-modules:
15+
ArmstrongNumbers
16+
other-modules:
17+
Paths_armstrong_numbers
18+
hs-source-dirs:
19+
src
20+
ghc-options: -Wall
21+
build-depends:
22+
base
23+
default-language: Haskell2010
24+
25+
test-suite test
26+
type: exitcode-stdio-1.0
27+
main-is: Tests.hs
28+
other-modules:
29+
Paths_armstrong_numbers
30+
hs-source-dirs:
31+
test
32+
build-depends:
33+
armstrong-numbers
34+
, base
35+
, hspec
36+
default-language: Haskell2010

armstrong-numbers/package.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: armstrong-numbers
2+
version: 1.1.0.3
3+
4+
dependencies:
5+
- base
6+
7+
library:
8+
exposed-modules: ArmstrongNumbers
9+
source-dirs: src
10+
ghc-options: -Wall
11+
# dependencies:
12+
# - foo # List here the packages you
13+
# - bar # want to use in your solution.
14+
15+
tests:
16+
test:
17+
main: Tests.hs
18+
source-dirs: test
19+
dependencies:
20+
- armstrong-numbers
21+
- hspec
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module ArmstrongNumbers (armstrong) where
2+
3+
digits :: Integral a => a -> [a]
4+
digits n
5+
| n < 10 = [n]
6+
| otherwise = (rem n 10) : digits (div n 10)
7+
8+
armstrong :: Integral a => a -> Bool
9+
armstrong n
10+
| n < 10 = True
11+
| otherwise = (sum $ map (^l) digs) == n
12+
where digs = digits n; l = length digs

armstrong-numbers/stack.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
resolver: lts-15.8

armstrong-numbers/stack.yaml.lock

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file was autogenerated by Stack.
2+
# You should not edit this file by hand.
3+
# For more information, please see the documentation at:
4+
# https://docs.haskellstack.org/en/stable/lock_files
5+
6+
packages: []
7+
snapshots:
8+
- completed:
9+
size: 492015
10+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/15/8.yaml
11+
sha256: 926bc3d70249dd0ba05277ff00943c0addb35b627cb641752669e7cf771310d0
12+
original: lts-15.8

armstrong-numbers/test/Tests.hs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{-# LANGUAGE RecordWildCards #-}
2+
3+
import Data.Foldable (for_)
4+
import Test.Hspec (Spec, describe, it, shouldBe)
5+
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
6+
7+
import ArmstrongNumbers (armstrong)
8+
9+
main :: IO ()
10+
main = hspecWith defaultConfig {configFastFail = True} specs
11+
12+
specs :: Spec
13+
specs = describe "armstrong" $ for_ cases test
14+
where
15+
16+
test Case{..} = it description assertion
17+
where
18+
assertion = armstrong input `shouldBe` expected
19+
20+
data Case = Case { description :: String
21+
, input :: Int
22+
, expected :: Bool
23+
}
24+
25+
cases :: [Case]
26+
cases = [ Case { description = "Zero is an Armstrong numbers"
27+
, input = 0
28+
, expected = True
29+
}
30+
, Case { description = "Single digit numbers are Armstrong numbers"
31+
, input = 5
32+
, expected = True
33+
}
34+
, Case { description = "There are no 2 digit Armstrong numbers"
35+
, input = 10
36+
, expected = False
37+
}
38+
, Case { description = "Three digit number that is an Armstrong number"
39+
, input = 153
40+
, expected = True
41+
}
42+
, Case { description = "Three digit number that is not an Armstrong number"
43+
, input = 100
44+
, expected = False
45+
}
46+
, Case { description = "Four digit number that is an Armstrong number"
47+
, input = 9474
48+
, expected = True
49+
}
50+
, Case { description = "Four digit number that is not an Armstrong number"
51+
, input = 9475
52+
, expected = False
53+
}
54+
, Case { description = "Seven digit number that is an Armstrong number"
55+
, input = 9926315
56+
, expected = True
57+
}
58+
, Case { description = "Seven digit number that is not an Armstrong number"
59+
, input = 9926314
60+
, expected = False
61+
}
62+
]

0 commit comments

Comments
 (0)