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

后端 未结 11 1374
情话喂你
情话喂你 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:10

    This is a fast way to get the first 792 answers. Def the most simple code:

    __author__ = 'robert'
    
    from itertools import product
    
    def get_nums(max_length):
        assert max_length < 21 #Otherwise there will be over 2 million possibilities
        for length in range(1, max_length + 1):
            for prod in product("10", repeat=length):
                if prod[0] == '1':
                    yield int("".join(prod))
    
    print list(get_nums(4))
    [1, 11, 10, 111, 110, 101, 100, 1111, 1110, 1101, 1100, 1011, 1010, 1001, 1000]
    
    nums = sorted(get_nums(20))
    print len(nums)
    
    solution = {}
    
    operations = 0
    
    for factor in range(1, 1000):
        for num in nums:
            operations += 1
            if num % factor == 0:
                solution[factor] = num
                break
        print factor, operations
        if factor not in solution:
            print "no solution for factor %s" % factor
            break
    
    print solution[787]
    
    max_v = max(solution.values())
    for factor, val in solution.items():
        if val == max_v:
            print factor, max_v
    
    
    [1, 11, 10, 111, 110, 101, 100, 1111, 1110, 1101, 1100, 1011, 1010, 1001, 1000]
    1048575
    1 1
    2 3
    3 10
    4 14
    5 16
    6 30
    7 39
    8 47
    9 558
    10 560
    11 563
    12 591
    13 600
    14 618
    15 632
    16 648
    17 677
    18 1699
    19 1724
    20 1728
    ..
    ..
    187 319781
    188 319857
    ..
    ..
    791 4899691
    792 5948266
    no solution for factor 792
    10110001111
    396 11111111111111111100
    

提交回复
热议问题