Find the kth eleven-non-free number

前端 未结 8 2127
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 10:10

Let\'s define eleven-non-free numbers:

If we consider a number as a string, then if any substring inside is a (non-zero) power of 11, then this

相关标签:
8条回答
  • 2021-02-04 10:41

    10^18 is really, really big. Brute force is not your friend.

    However, let us note some conveniences:

    • If a string s represents a eleven-non-free number, then trivially so does d s (where d is a string of digits).
    • If you wanted to know how many k-digit numbers are eleven-non-free, you could base that off of how many k-1-digit numbers are eleven-non-free.
    • (e.g. how many 1xx numbers are eleven-non-free? Well, how many xx numbers are eleven-non-free? Obviously at least that many 1xx numbers are eleven-non-free.. The only additional cases are when a power of eleven starts with the digit at the start -- with the 1 that just got tacked on the front.)
    • This suggests a relatively straightforward dynamic programming approach to figure out how many eleven-non-free numbers are in a string of known length with a fixed prefix. (e.g. how many eleven-non-free numbers are in 934xxxxx)

    Finally, throw some binary-search style logic on top, and you should be able to find exactly which number is the N-th eleven-non-free number.

    0 讨论(0)
  • 2021-02-04 10:42

    given a k, find the kth eleven-non-free number.... is a big question.

    Since this is time taking question, I am writing pseudo code.

    1. First step Keep the powers of eleven handy

    var eleven_powers = []; for (var i=0; i<1000; i++) eleven_powers.push(Math.pow(11,i));

    1. Second Step Keep all possible non-eleven-free numbers to an extent you can. This will have to deal with big integers.

    for (var i=11; i<Integer.MAX_VALUE; i++){ if(string(i).substring(for each of eleven_powers) == true) store it sequentially results_array }

    1. Third Step Take k, return results_array[k]
    0 讨论(0)
提交回复
热议问题