2 Answers Having a number x = 2 k 1 +2 k 2 +...+2 k m where k 1 &lt;k 2 &lt;...&lt;k m it could be claimed that position of number x in lexicographically ordered sequence of all numbers with the same hamming weight is lex_order(x) = C(k 1 ,1)+C(k 2 ,2)+...+C(k m ,m) where C(n,m) = n!/m!/(n-m)! or 0 if m&gt;n Would love to use this but, when m is 20 - 30, unfeasible to sort of calculate. -- ks6g10 Nov 25 at 22:32 @ks6g10 - All C(n,m) values for 1&lt;n,m&lt;32 should be calculated using recurrence relation C(n+1,m+1)=C(n,m)+C(n,m+1) and stored in an array prior to running the main function. It will cost you just about 1000 additions. Factorial calculation is not actually required. -- Egor Skriptunoff Nov 25 at 23:17 @EgorSkriptunoff Then it comes to that I can not really use arrays. -- ks6g10 Nov 25 at 23:44 @ks6g10 - What constraints are there on your program? Are input number values limited by some constant? What amount of ROM size, RAM size and CPU time do you have at your disposal? My solution can be enhanced to require only O(log(x)) memory and O(log^2(x)) time for calculating lex_order(x). -- Egor Skriptunoff Nov 26 at 3:33