So, I’m trying to draw an HTML table as close to being a square as possible from a value passed in. For example, if the number passed in is 16, then the output would be 4×4. Since not all numbers have a whole square root, the output for a number like 12 would be 4×3 (not 1×12 or 2×6). The output for a prime number like 7 would be 7×1. Any suggestions? Thanks!
Share
Here’s a general procedure you can follow:
Take the floor of the square root of the number and see if your number is divisible by that without a remainder. Keep subtracting 1 and repeating the process until you get a number that is divisible without a remainder. Then take the result of your starting number divided by that number as your second dimension.
UPDATE:
Here’s an example JavaScript solution based on the above solution.