Skip to content

Create minimums_squares_to_represent_a_number.py #7595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 26, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update minimum_squares_to_represent_a_number.py
updated
  • Loading branch information
JatinR05 authored Oct 26, 2022
commit dbaa6062fc5e75642732d141bbef62dd8ba725da
17 changes: 7 additions & 10 deletions dynamic_programming/minimum_squares_to_represent_a_number.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import math
import sys

import math,sys

def minimum_squares_to_represent_a_number(number: int) -> int:
"""
Expand All @@ -16,7 +14,7 @@ def minimum_squares_to_represent_a_number(number: int) -> int:
>>> minimum_squares_to_represent_a_number(-1)
Traceback (most recent call last):
...
ValueError: the value of input must be positive
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)
Expand All @@ -30,18 +28,17 @@ def minimum_squares_to_represent_a_number(number: int) -> int:
raise ValueError("the value of input must not be a negative number")
if number == 0:
return 1
answers = [-1] * (number + 1)
answers = [-1] * (number+1)
answers[0] = 0
for i in range(1, number + 1):
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)
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

Expand Down