TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
magic_number.cpp
Go to the documentation of this file.
1
19#include <cassert>
20#include <cstdint>
21#include <iostream>
22
27namespace math {
33bool magic_number(const uint64_t &n) {
34 if (n <= 0) {
35 return false;
36 }
37 // result stores the modulus of @param n with 9
38 uint64_t result = n % 9;
39 // if result is 1 then the number is a magic number else not
40 if (result == 1) {
41 return true;
42 } else {
43 return false;
44 }
45}
46} // namespace math
47
52static void tests() {
53 std::cout << "Test 1:\t n=60\n";
54 assert(math::magic_number(60) == false);
55 std::cout << "passed\n";
56
57 std::cout << "Test 2:\t n=730\n";
58 assert(math::magic_number(730) == true);
59 std::cout << "passed\n";
60
61 std::cout << "Test 3:\t n=0\n";
62 assert(math::magic_number(0) == false);
63 std::cout << "passed\n";
64
65 std::cout << "Test 4:\t n=479001600\n";
66 assert(math::magic_number(479001600) == false);
67 std::cout << "passed\n";
68
69 std::cout << "Test 5:\t n=-35\n";
70 assert(math::magic_number(-35) == false);
71 std::cout << "passed\n";
72}
73
78int main() {
79 tests(); // execute the tests
80 return 0;
81}
static void tests()
Test function.
int main()
Main function.
for assert
bool magic_number(const uint64_t &n)