Problem · Math

Consecutive Sum (Financial Engineer Intern)

Learn this problem
MediumBloomberg logoBloombergINTERNOA

Problem statement

Given a long integer, num, find the number of ways to represent it as a sum of two or more consecutive positive integers. For example:

  • If num = 15, then there are three such ways: (1 + 2 + 3 + 4 + 5) = (4 + 5 + 6) = (7 + 8) = 15.
  • If num = 2, then there are zero such ways.
  • Complete the consecutive function in the editor below. It has one parameter: a long integer named num. The function must return an integer denoting the number of ways to represent num as a sum of two or more consecutive positive integers.

    Input Format

    Locked stub code in the editor reads a long integer denoting num from stdin and passes it to the function.

    Constraints

    • 1 ≤ num ≤ 10^12

    Output Format

    Return an integer denoting the number of ways to represent num as a sum of two or more consecutive positive integers.

    Function

    consecutive(num: long) → int

    Examples

    Example 1

    num = 15return = 3
    Might or might not be an accurate explanation :)

    There are three ways to represent num = 15 as a sum of two or more consecutive positive integers:

    • (1 + 2 + 3 + 4 + 5)
    • (4 + 5 + 6)
    • (7 + 8)

    Therefore, the function returns 3.

    Constraints

    1 ≤ num ≤ 10^12

    More Bloomberg problems

    drafts saved locally
    public int consecutive(long num) {
      // write your code here
    }
    
    num15
    expected3
    checking account