TopcoderArchive
Archive
Algorithm problem

DecreasingNumber

Problem statement, definition, constraints, and public examples.

Problem Statement

There are three types of operations you can perform on an integer:
  • 1. If it's divisible by 3, divide it by 3.
  • 2. If it's divisible by 2, divide it by 2.
  • 3. Subtract 1.
Given an int n, return the minimal number of operations needed to produce the number 1.

Definition

Class:
DecreasingNumber
Method:
numberOfOperations
Parameters:
int
Returns:
int
Method signature:
int numberOfOperations(int n)
(be sure your method is public)

Constraints

  • n will be between 1 and 106, inclusive.

Examples

  1. 1
    Returns: 0
    We are already at number 1.
  2. 2
    Returns: 1
    Only divide it by 2.
  3. 5
    Returns: 3
    First, substract 1. Then, divide by 2 twice. (Or substract 1 twice, and then divide by 3).
  4. 10
    Returns: 3
    First, substract 1. Then, divide by 3 twice.
  5. 962340
    Returns: 17
Back to all problems