Title: Most equivalent factors of a number
Tags: c++;math
Question: Given a number 'n', which is a power-of-2, how can I efficiently find the 2 factors which are most equivalent to eachother? In other words, if I have a linear array and want to map it to 2D, how can I find the 2D dimensions that are the most equal (image dimensions most close to a square)?

Gotta be some kind of bitwise operation to make this fast, rather than looping over factors.

Comment: `n` is representable as `2^k` (since you say it's a power of 2). If `k` is even, then `n == 2^(k/2) * 2^(k/2)` (e.g. `16==4*4`). If `k` is odd, then the closest you can get is `n == 2^((k-1)/2) * 2^((k+1)/2)` (e.g. `8==2*4`)

Comment: Fantastic! Please post as answer and I will accept.

Comment: You are absolutely right in your suspicions. Now, you just need to think what "2 to the nth" power means, mathematically, and given the `n`, how to find a number that when multiplied by itself results in this "2^n". Free clue: this is easy when `n` is even.

Here is the accepted answer: ```n``` is representable as ```2^k``` (since you say it's a power of 2). If ```k``` is even, then ```n == 2^(k/2) * 2^(k/2)``` (e.g. ```16==4*4```). If ```k``` is odd, then the closest you can get is ```n == 2^((k-1)/2) * 2^((k+1)/2)``` (e.g. ```8==2*4```)



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:11/14/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><math>', 'Title': 'Most equivalent factors of a number', 'ViewCount': 41, 'id': 40580533, 'user_AboutMe': None, 'user_AccountId': 1036686.0, 'user_DisplayName': 'user1043761', 'user_Reputation': 696.0, 'user_Views': 60.0, 'user_id': 1043761.0}}