How to generate square-ish rectangle dimensions for any specified area

2 vues (au cours des 30 derniers jours)
Hi all, given an arbitrary positive integer N, how can I calculate two integer factors A and B subject to A*B = N with the restriction that A should be close to B. In other words, if N is 12 then I would prefer the solution A=3,B=4 over A=2,B=6. Thanks!

Réponse acceptée

Image Analyst
Image Analyst le 3 Mai 2014
Try this:
a=floor(sqrt(N)) % First integer less than sqrt.
b = ceil(N/a) % If fraction, make next largest integer.
  1 commentaire
Eric Sampson
Eric Sampson le 3 Mai 2014
Thanks IA, I knew it should be something like that but my brain is just a little fried at the end of the week. Have a great weekend! :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Lighting, Transparency, and Shading dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by