Skip to main content
LeetCode 2698, Medium. Topics: Math, Backtracking. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 23 parametrized pytest cases, and a playground notebook:

Problem

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.

Examples

Constraints

  • 1 <= n <= 1000

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026