TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

CrazySequence

Problem statement, definition, constraints, and public examples.

Problem Statement

Consider the following infinite sequence of numbers: 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, ... (number k appears exactly k times in the sequence). You are given a String n representing an integer (without leading zeros). Return the n-th number in that sequence (index will be 1-based).

Definition

Class:
CrazySequence
Method:
nthNumber
Parameters:
String
Returns:
int
Method signature:
int nthNumber(String n)
(be sure your method is public)

Constraints

  • n will represent an integer between 1 and 1018, inclusive.
  • n will have no leading zeros.

Examples

  1. "1"
    Returns: 1
  2. "4"
    Returns: 3
  3. "169"
    Returns: 18
  4. "25342536783"
    Returns: 225133
Back to all problems