How does this integer encoding work?

北慕城南 提交于 2019-12-24 09:03:54

问题


In this code golf question, there is a python answer that encodes the lengths of all integers from 1 to 99 in english to a big number:

7886778663788677866389978897746775667552677566755267756675527886778663788677866355644553301220112001

To get the length of n, you just have to calculate 3 + (the_big_number / (10**n)) % 10. How does this work?


回答1:


(the_big_number / (10^n)) % 10 pulls out the nth least significant digit of the big number, so the lengths are just stored starting with the length of "zero" (1+3=4) at the far right, and following over to the length of "ninety-nine" (7+3=10) at the far left.

The shortest English numbers are three letters ("one", "two", "six", "ten"), so each length is stored with an offset of three. The longest prior to 100 are 9 + 3 = 12 letters (e.g. "seventy-eight"), so each number can be stored as a single digit.




回答2:


Starting from the right:

  • the first digit is how many letters are in "zero" minus 3
  • the second digit is how many letters are in "one", minus 3
  • the third digit...
  • ...the 100th digit is how many letters are in "ninety nine" minus three.

Note that that the longest number "seventy seven" has only 12 letters, which conveniently fits in a single digit after subtracting 3.



来源:https://stackoverflow.com/questions/3249592/how-does-this-integer-encoding-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!