(January 10, 2018 at 9:12 am)FlatAssembler Wrote: I simply don't understand what's that algorithm supposed to achieve on the high-level. I suppose it counts the number of ones in the binary representation of the numbers and searches for some kind of maximum. But such low numbers (from 14 to 17) can't have 15 ones in their binary representation, they are only 4-5 digits long in the binary system.
Yes, it counts the number of 1s in the binary description. In the for loop, t starts out with the value of i. then, setting z=t mod 2 gets the last binary digit, which is added to the running total, s. Then t=t div 2 divides t by 2, discarding the remainder. This happens as long as t>0. At the end, s holds the number of 1s (actually, the total of the binary digits, which is the same thing).
Now, m keeps track of the largest number of 1s so far, and p keeps track of the corresponding number. If s is larger than m, we replace m by s and p by i.
At the end, p=15, which in binary is 1111 (and has four 1s, more than any other number in the for loop). So, m=4, but we only print p.