How to find the smallest number with just 0 and 1 which is divided by a given number?

后端 未结 11 1348
情话喂你
情话喂你 2020-11-29 22:44

Every positive integer divide some number whose representation (base 10) contains only zeroes and ones.

One can prove that:

Consider the numbers 1, 11, 111,

11条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 23:23

    Here's a brute force version in Raku:

    say (1..Inf).map( *.base(2) ).first( * %% $n );
    

    The code generates a lazy (potentially infinite) sequence of candidate numbers and then searches for the first element that's divisible by n.

    Being brute force it's not exceptionally fast, but the code is striking in its simplicity and expressiveness, as it is typical for Raku.

提交回复
热议问题