TopcoderArchive
Archive
Algorithm problem

PowerDigits

Problem statement, definition, constraints, and public examples.

Problem Statement

Given a positive integer a, consider the infinite sequence of integers {a^0, a^1, a^2, ...}. What proportion of them begin with the digits digits?

Definition

Class:
PowerDigits
Method:
proportion
Parameters:
int, int
Returns:
double
Method signature:
double proportion(int a, int digits)
(be sure your method is public)

Notes

  • The returned value must be accurate to within a relative or absolute value of 1E-9.

Constraints

  • a will be between 2 and 1000, inclusive.
  • digits will be between 1 and 999, inclusive.

Examples

  1. 2
    1
    Returns: 0.30102999566398114
    The first few powers of 2 that begin with "1" are 1, 16, 128, 1024.
  2. 2
    163
    Returns: 0.0026562436437400742
    Around 1 in 400 powers of 2 begin with "163". The first one is 16384.
  3. 345
    678
    Returns: 6.400804134383014E-4
  4. 10
    42
    Returns: 0.0
  5. 3
    7
    Returns: 0.057991946977686705
Back to all problems