Skip to content
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
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,18 @@
"math",
"strings"
]
},
{
"slug": "armstrong-numbers",
"uuid": "1035aa3f-030a-425b-84f1-1967f344d155",
"core": false,
"unlocked_by": null,
"difficulty": 3,
"topics": [
"algorithm",
"loops",
"math"
]
}
]
}
53 changes: 53 additions & 0 deletions exercises/armstrong-numbers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Get the exercise name from the current directory
Comment thread
smuroff marked this conversation as resolved.
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.1.3)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
51 changes: 51 additions & 0 deletions exercises/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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 = 1`
- 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.

## Getting Started

Make sure you have read the [Installing](https://exercism.io/tracks/cpp/installation) and
[Running the Tests](https://exercism.io/tracks/cpp/tests) pages for C++ on exercism.io.
This covers the basic information on setting up the development
environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the [three
rules of test-driven development](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd).
Create just enough structure by declaring namespaces, functions, classes,
etc., to satisfy any compiler errors and get the test to fail. Then write
just enough code to get the test to pass. Once you've done that,
uncomment the next test by moving the following line past the next test.

```C++
#if defined(EXERCISM_RUN_ALL_TESTS)
```

This may result in compile errors as new constructs may be invoked that
you haven't yet declared or defined. Again, fix the compile errors minimally
to get a failing test, then change the code minimally to pass the test,
refactor your implementation for readability and expressiveness and then
go on to the next test.

Try to use standard C++11 facilities in preference to writing your own
low-level algorithms or facilities by hand. [CppReference](http://en.cppreference.com/)
is a wiki reference to the C++ language and standard library. If you
are new to C++, but have programmed in C, beware of
[C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers).

## 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.
51 changes: 51 additions & 0 deletions exercises/armstrong-numbers/armstrong_numbers_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "test/catch.hpp"
#include "armstrong_numbers.h"

// Armstrong-numbers exercise test case data version 1.1.0

TEST_CASE("zero_is_an_armstrong_number")
{
REQUIRE(armstrong_numbers::is_armstrong_number(0));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
Comment thread
smuroff marked this conversation as resolved.
TEST_CASE("single_digit_numbers_are_armstrong_numbers")
{
REQUIRE(armstrong_numbers::is_armstrong_number(5));
}

TEST_CASE("there_are_no_2_digit_armstrong_numbers")
{
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(10));
}

TEST_CASE("three_digit_number_that_is_an_armstrong_number")
{
REQUIRE(armstrong_numbers::is_armstrong_number(153));
}

TEST_CASE("three_digit_number_that_is_not_an_armstrong_number")
{
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(100));
}

TEST_CASE("four_digit_number_that_is_an_armstrong_number")
{
REQUIRE(armstrong_numbers::is_armstrong_number(9474));
}

TEST_CASE("four_digit_number_that_is_not_an_armstrong_number")
{
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(9475));
}

TEST_CASE("seven_digit_number_that_is_an_armstrong_number")
{
REQUIRE(armstrong_numbers::is_armstrong_number(9926315));
}

TEST_CASE("seven_digit_number_that_is_not_an_armstrong_number")
{
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(9926314));
}
#endif // !EXERCISM_RUN_ALL_TESTS
11 changes: 11 additions & 0 deletions exercises/armstrong-numbers/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "armstrong_numbers.h"
#include <cmath>

bool armstrong_numbers::is_armstrong_number(int number) {
int length = number != 0 && number != 1 ? std::log10(std::abs(number)) + 1 : 1;
int accumulate = 0;
for (int current = number; current != 0; current /= 10) {
accumulate += std::pow(current % 10, length);
}
return accumulate == number;
}
10 changes: 10 additions & 0 deletions exercises/armstrong-numbers/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#if !defined(ARMSTRONG_NUMBERS_H_)
#define ARMSTRONG_NUMBERS_H_

namespace armstrong_numbers {

bool is_armstrong_number(int number);

} // namespace armstrong_numbers

#endif // !ARMSTRONG_NUMBERS_H_
Loading