Perfect Squares
Prereq: Knapsack Intro
Given an integer n, determine the smallest number of perfect squares that sum to n. The same square may be used multiple times.
A perfect square is an integer p that can be written as q^2 for some integer q. For example, 9 is a perfect square since 9 = 3^2, while 8 is not.
n = 12
3
12 = 4 + 4 + 4 uses three squares. No pair of squares sums to 12, so three is the minimum.
n = 13
2
13 = 4 + 9. Taking the largest square first would give 13 = 9 + 1 + 1 + 1, four squares, so the greedy choice is not the minimum here.
1 <= n <= 10^4