# Dynamic Programming – Minimum Numbers are Required Whose Square Sum is Equal To a Given Number

Objective: Given a number, Write an algorithm to find out the minimum numbers required whose square is equal to the number.

This question has been asked in the Google Interview for the Software Developer position. This is a very good problem that shows the advantage of dynamic programming over recursion.

Example:

```Given Number: 12

Numbers whose sum of squares are equal to 12.

12+12+12+12+12+12+12+12+12+12+12+12= 12
22+22+22= 12
32+12+12+12= 12
Answer: 3 numbers (2,2,2)
```

Approach
Given Number: N

Find the square root of a given number ‘N’ and take the integer part of it, say it is ‘x’

Now numbers from 1 to x are the options which can be used, whose square sum is equal to N.

Example:

Given Number: 12, Integer part of square root of 12 is : 3. So 1,2,3 are the numbers whose square sum can be made to 12.

Now of you notice, this problem has been reduced to “Minimum Coin Change Problem” with some modification. In “Minimum Coin Change Problem“, the minimum numbers of coins are required to make change of a given amount, here minimum numbers required whose square sum is equal to given number.

Recursive Solution:
https://gist.github.com/thmain/c2785d6995fc79dea3ed0efa63d24df3

But again we are solving many sub problems repeatedly and again Dynamic Programming(is to rescue.

Code:

```3
```

### 1 thought on “Dynamic Programming – Minimum Numbers are Required Whose Square Sum is Equal To a Given Number”

1. Any number can be represented as sum of at most four perfect squares.

For 1… just check if number is a perfect square.
For 2… for every perfect square smaller then n, see if n – perfect_sq is also a perfect square.
For 3… for sum of every pair of perfect squares smaller then n, check if n – (a^2 + b^2) is a perfect square….
For 4,… put all sum of every pair of perfect squares in a map mapping sum to pair of perfect squares. Also, keep checking if such sum

was already encountered in the map thus maintained.

Above algorithm should complete in O(n) rather then O(n*sqrt(n)) … where n is value of the number and not size of the number.

Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://algorithms.tutorialhorizon.com/minimum-numbers-are-required-which-square-sum-is-equal-to-a-given-number/", "fetch_time": 1670526206000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2022-49/segments/1669446711360.27/warc/CC-MAIN-20221208183130-20221208213130-00396.warc.gz", "warc_record_offset": 114432380, "warc_record_length": 17374, "token_count": 565, "char_count": 2263, "score": 4.21875, "int_score": 4, "crawl": "CC-MAIN-2022-49", "snapshot_type": "latest", "language": "en", "language_score": 0.8912127017974854, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00040-of-00064.parquet"}