Skip to content

armstrong-numbers: add to track #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
"unlocked_by": "two-fer",
"uuid": "2c8afeed-480e-41f3-ad58-590fa8f88029"
},
{
"core": false,
"difficulty": 2,
"slug": "armstrong-numbers",
"topics": [
"integers",
"mathematics"
],
"unlocked_by": null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be unlocked by some other exercise? Or do you think it should be always accessible? I think either is fine :)

"uuid": "8e1dd48c-e05e-4a72-bf0f-5aed8dd123f5"
},
{
"core": false,
"difficulty": 2,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import static java.lang.Math.pow;

class ArmstrongNumbers {

boolean isArmstrongNumber(int numberToCheck) {

int number = numberToCheck;
int calculation = 0;
int length = String.valueOf(number).length();

while (number > 0) {
calculation += pow((number % 10), length);
number = number / 10;
}

return numberToCheck == calculation;

}

}
1 change: 1 addition & 0 deletions exercises/armstrong-numbers/.meta/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
30 changes: 30 additions & 0 deletions exercises/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Armstrong Numbers

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.

For example:

- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 2`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`

Write some code to determine whether a number is an Armstrong number.

# Running the tests

You can run all the tests for an exercise by entering

```sh
$ gradle test
```

in your terminal.

## Source

Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.
18 changes: 18 additions & 0 deletions exercises/armstrong-numbers/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}

test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ArmstrongNumbers {

boolean isArmstrongNumber(int numberToCheck) {

throw new UnsupportedOperationException("Delete this statement and write your own implementation.");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class ArmstrongNumbersTest {

private ArmstrongNumbers armstrongNumbers;

@Before
public void setup() {
armstrongNumbers = new ArmstrongNumbers();
}

@Test
public void singleDigitsAreArmstrongNumbers() {
int input = 5;

assertTrue(armstrongNumbers.isArmstrongNumber(input));
}

@Ignore("Remove to run test")
@Test
public void noTwoDigitArmstrongNumbers() {
int input = 10;

assertFalse(armstrongNumbers.isArmstrongNumber(input));
}

@Ignore("Remove to run test")
@Test
public void threeDigitNumberIsArmstrongNumber() {
int input = 153;

assertTrue(armstrongNumbers.isArmstrongNumber(input));
}

@Ignore("Remove to run test")
@Test
public void threeDigitNumberIsNotArmstrongNumber() {
int input = 100;

assertFalse(armstrongNumbers.isArmstrongNumber(input));
}

@Ignore("Remove to run test")
@Test
public void fourDigitNumberIsArmstrongNumber() {
int input = 9474;

assertTrue(armstrongNumbers.isArmstrongNumber(input));
}

@Ignore("Remove to run test")
@Test
public void fourDigitNumberIsNotArmstrongNumber() {
int input = 9475;

assertFalse(armstrongNumbers.isArmstrongNumber(input));
}

@Ignore("Remove to run test")
@Test
public void sevenDigitNumberIsArmstrongNumber() {
int input = 9926315;

assertTrue(armstrongNumbers.isArmstrongNumber(input));
}

@Ignore("Remove to run test")
@Test
public void sevenDigitNumberIsNotArmstrongNumber() {
int input = 9926314;

assertFalse(armstrongNumbers.isArmstrongNumber(input));
}

}
1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include 'acronym'
include 'all-your-base'
include 'allergies'
include 'anagram'
include 'armstrong-numbers'
include 'atbash-cipher'
include 'bank-account'
include 'beer-song'
Expand Down