PracticeMath
Decode a Square Token Count
Context
A document-ingestion service stores a compact audit number for each batch. The number is always a perfect square: it records the total token count after pairing tokens into equal rows and columns. Recover the number of tokens along one side of that arrangement.
Problem
Implement a function that receives the audit number for a document batch. The audit number is guaranteed to be a nonnegative perfect square. Return its principal nonnegative integer square root, which is the side length of the token arrangement. The result must be exact, including when the audit number is zero or very large. Do not return a rounded floating-point approximation.
Examples
Example 1
Input:
audit_number = 0Output:
0Explanation: For audit_number 0, the executed function returned 0. The trace ended with audit_number 0 and input_value 0.
Example 2
Input:
audit_number = 144Output:
12Explanation: For audit_number 144, the executed function returned 12. The final traced root_value and next_value were both 12.
Example 3
Input:
audit_number = 2025Output:
45Explanation: For audit_number 2025, the executed function returned 45. The final traced root_value and next_value were both 45.
Constraints
0 <= audit_number <= 1000000000000000000The input is guaranteed to be a perfect square.The input is supplied as an integer, not as text requiring parsing.- Types:
audit_numberis int; result is int
Function signature
def decode_token_side(audit_number: int) -> int
audit_numberint- A nonnegative perfect-square value from the document audit log.
- returns int
- The exact principal nonnegative integer whose square equals audit_number.
Adapted from MBPP problem task_518 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Negative values are outside the valid input domain.
Code
Saved