divide an arbitrary rectangle into set of squares

Hello forum,

I have a arbitrary rectangle of size I1 X I2 and I have to formulate an recursive/iterative algorithm to partition the rectangle into set of squares. The article that I am following have formulated some mathematical formula and I am having some trouble to formulate an algorithm out of it. Lets describe the mathematics behind it.

It is assumed in the paper that I1 < I2 and

Code :

long long int L1 = std::floor(I2/I1) x I1;

The rectangular region is first partitioned into
1. One I1 x L1 sub-region , and
2. One I1 x (I2 - L1) sub-region.

Then the I1 x L1 sub-region is partitioned into (L1/I1) smaller square sub-regions, each with size I1 x I1.

Repeating the above rule to partition the remaining (I2 - L1) x (I1 - L2) sub-region until the remaining sub-region is of size Z1 x Z2 such that Z1 > Z2 and one is the factor of the other. At that time, the final remaining sub-image can be partitioned into Z1/Z2 or Z2/Z1 square regions.