|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
A simple program to check if the given number is a magic number or not. A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number. More...
#include <cassert>#include <iostream>Namespaces | |
| namespace | math |
| for std::rand | |
Functions | |
| bool | math::magic_number (const uint64_t &n) |
| static void | tests () |
| Test function. More... | |
| int | main () |
| Main function. More... | |
A simple program to check if the given number is a magic number or not. A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number.
This is a shortcut method to verify Magic Number. On dividing the input by 9, if the remainder is 1 then the number is a magic number else not. The divisibility rule of 9 says that a number is divisible by 9 if the sum of its digits are also divisible by 9. Therefore, if a number is divisible by 9, then, recursively, all the digit sums are also divisible by 9. The final digit sum is always 9. An increase of 1 in the original number will increase the ultimate value by 1, making it 10 and the ultimate sum will be 1, thus verifying that it is a magic number.
| int main | ( | void | ) |
|
static |
Test function.