TopcoderArchive
Archive
Algorithm problem

EllysDigitMultiplication

Problem statement, definition, constraints, and public examples.

Problem Statement

Elly is bored in her math classes. She solves the problems much quicker than her classmates and then has nothing to do for several minutes until the rest of the kids also get the result.

While waiting for the others, she is entertaining herself with the following procedure. The girl gets the number she got from the problem she just solved (a non-negative integer) and if it has more than one digit, she multiplies its digits, obtaining a new number. If the new number also has more than one digit, she does the same. Elly continues with this procedure until obtaining a single-digit number.

For example, if she starts with the number 1337, she multiplies its digits and gets 1 * 3 * 3 * 7 = 63. Since the new number still has more than one digit, she does this again, obtaining 6 * 3 = 18. She has to do the procedure one more time to obtain 1 * 8 = 8, which has a single digit, thus the girl stops.

She is now getting bored of doing this by hand. The girl has asked you to write a program, that given the int number returns the digit, obtained by applying the described procedure.

Definition

Class:
EllysDigitMultiplication
Method:
getDigit
Parameters:
long
Returns:
int
Method signature:
int getDigit(long number)
(be sure your method is public)

Notes

  • Since the product of the digits of an integer number always yields smaller, non-negative integer number, it is guaranteed that applying the described procedure finite number of times yields a single digit result.

Constraints

  • number will be between 0 and 1018 inclusive.

Examples

  1. 1337
    Returns: 8
    The example from the problem statement.
  2. 42
    Returns: 8
    We'll get the number 8 after a single iteration of the algorithm.
  3. 3
    Returns: 3
    Sometimes the girl starts with a number that already has a single digit and she stops right away.
  4. 123456789
    Returns: 0
  5. 347426771113839271
    Returns: 6
Back to all problems