From 8fd06efe22ec3e870ac1fa375bd4600cb30baad4 Mon Sep 17 00:00:00 2001 From: JatinR05 <71865805+JatinR05@users.noreply.github.com> Date: Wed, 26 Oct 2022 20:13:01 +0530 Subject: [PATCH] Create minimums_squares_to_represent_a_number.py (#7595) * Create minimums_squares_to_represent_a_number.py added a dynamic programming approach of finding the minimum number of square to represent a number. eg : 25 = 5*5 37 = 6*6 + 1*1 21 = 4*4 + 2*2 + 1*1 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update and rename minimums_squares_to_represent_a_number.py to minimum_squares_to_represent_a_number.py updated the code * Update minimum_squares_to_represent_a_number.py I have added the appropriate checks for 0 and 12.34. It would be great if you could suggest a name for the dp array * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update minimum_squares_to_represent_a_number.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update minimum_squares_to_represent_a_number.py updated * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update minimum_squares_to_represent_a_number.py updated * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .../minimum_squares_to_represent_a_number.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 dynamic_programming/minimum_squares_to_represent_a_number.py diff --git a/dynamic_programming/minimum_squares_to_represent_a_number.py b/dynamic_programming/minimum_squares_to_represent_a_number.py new file mode 100644 index 000000000..bf5849f5b --- /dev/null +++ b/dynamic_programming/minimum_squares_to_represent_a_number.py @@ -0,0 +1,48 @@ +import math +import sys + + +def minimum_squares_to_represent_a_number(number: int) -> int: + """ + Count the number of minimum squares to represent a number + >>> minimum_squares_to_represent_a_number(25) + 1 + >>> minimum_squares_to_represent_a_number(37) + 2 + >>> minimum_squares_to_represent_a_number(21) + 3 + >>> minimum_squares_to_represent_a_number(58) + 2 + >>> minimum_squares_to_represent_a_number(-1) + Traceback (most recent call last): + ... + ValueError: the value of input must not be a negative number + >>> minimum_squares_to_represent_a_number(0) + 1 + >>> minimum_squares_to_represent_a_number(12.34) + Traceback (most recent call last): + ... + ValueError: the value of input must be a natural number + """ + if number != int(number): + raise ValueError("the value of input must be a natural number") + if number < 0: + raise ValueError("the value of input must not be a negative number") + if number == 0: + return 1 + answers = [-1] * (number + 1) + answers[0] = 0 + for i in range(1, number + 1): + answer = sys.maxsize + root = int(math.sqrt(i)) + for j in range(1, root + 1): + current_answer = 1 + answers[i - (j**2)] + answer = min(answer, current_answer) + answers[i] = answer + return answers[number] + + +if __name__ == "__main__": + import doctest + + doctest.testmod()