Find the Punishment Number of an Integer

Given a positive integer n, return the punishment number of n.

The punishment number of n is defined as the sum of the squares of all integers i such that:

  • 1 <= i <= n
  • The decimal representation of i * i can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals i.
Example 1
Inputn = 10
Output182
There are exactly 3 integers in the range [1, 10] that satisfy the conditions, so the punishment number is 1 + 81 + 100 = 182.
Example 2
Inputn = 37
Output1478
There are exactly 4 integers in the range [1, 37] that satisfy the conditions, so the punishment number is 1 + 81 + 100 + 1296 = 1478.

Constraints

  • 1 <= n <= 1000

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate