TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

SimpleSums

Problem statement, definition, constraints, and public examples.

Problem Statement

You will be given a positive integer n. You will return a int[] that contains 3 elements:
  • Element 0: The sum of all integers from 1 to n inclusive
  • Element 1: The sum of the squares of all integers from 1 to n inclusive
  • Element 2: The sum of the cubes of all integers from 1 to n inclusive
For example:
n = 3
  • Element 0 = 1 + 2 + 3 = 6
  • Element 1 = 1*1 + 2*2 + 3*3 = 14
  • Element 2 = 1*1*1 + 2*2*2 + 3*3*3 = 36
So you would return {6,14,36}

Definition

Class:
SimpleSums
Method:
compute
Parameters:
int
Returns:
int[]
Method signature:
int[] compute(int n)
(be sure your method is public)

Notes

  • The return value will always be within the bounds of an integer.

Constraints

  • n will be between 1 and 100 inclusive

Examples

  1. 3
    Returns: { 6,  14,  36 }
    The example from above
  2. 1
    Returns: { 1,  1,  1 }
    Only a single number
  3. 100
    Returns: { 5050,  338350,  25502500 }
    As big as it gets
  4. 10
    Returns: { 55,  385,  3025 }
Back to all problems