Given a positive integer n, you must write it as a sum of at least 2 positive integers so that the product of those integers is as large as possible. How many pieces should you make, and how big? With n = 10, cutting into 3 + 3 + 2 + 2 gives 3 × 3 × 2 × 2 = 36, which is the maximum. Cutting into 5 + 5 gives 25, and into 2 + 2 + 2 + 2 + 2, 32.
The strategy: cut threes until 0, 1 or 2 is left. With n = 3a + b, a is the number of threes and b the remainder. If b = 0, the product is 3ᵃ. If b = 2, that 2 stays: 3ᵃ × 2. If b = 1, since 2 × 2 > 3 × 1, the last 3 and the 1 are swapped for 2 + 2: 3ᵃ⁻¹ × 4. For n ≤ 3 a 1 must be cut off: the product is 1 × (n − 1).
Cutting a piece of 2 off n gives 2 × (n − 2). That is greater than or equal to n when 2n − 4 ≥ n, that is when n ≥ 4. So any piece of 4 or more is worth cutting again, and in the end only pieces of 1, 2 and 3 remain. 1 is the worst: 1 × (n − 1) is always less than n, so cutting off a 1 shrinks the product.
Pieces of 2 and 3 are left: which is better? With n = 6, 3 × 3 = 9 beats 2 × 2 × 2 = 8. So 3 twos can always be swapped for 2 threes and the product rises. That is why the best split has at most 2 pieces of 2, and the rest are threes.
The proof is by contradiction, for n ≥ 4, in 3 parts. Every piece is ≤ 3: if there were an x ≥ 4, cutting it gives 2 × (x − 2) ≥ x. There are no pieces of 1: if there were, adding it to another piece gives more. And there are at most 2 twos: if there were 3, swapping them for 2 threes raises 8 to 9. Each case contradicts the solution being the best.
There is no need for a loop that cuts one piece at a time: integer division gives a, and the remainder gives b. The time depends on how the language computes powers. In Python, the ** operator and pow() take O(log a), and math.pow() calls C's pow(), which works with floating point in O(1). Since only a and b are used, memory is O(1).