How to calculate smallest number with certain number of divisors?

前端 未结 9 1376
野性不改
野性不改 2020-12-29 07:51

From Project Euler problem 500

The number of divisors of 120 is 16. In fact 120 is the smallest number having 16 divisors.

Find the smalle

相关标签:
9条回答
  • 2020-12-29 08:31

    The highest divisor any number has, other than itself, is the half of the number. For example, 120 has a max divisor of 60 other than itself. So, you can easily reduce the range from (n+1) to (n/2).

    More over, for a number to have m divisors, the number must be atleast ((m-1) * 2) following the above logic (-1 because the m th number is itself). For example, a number with 4 divisors has to be atleast 6. So, your search for n has a smaller range now.

    These two will reduce the runtime a little.

    0 讨论(0)
  • 2020-12-29 08:33

    Not full answer some hints instead:

    1. the max integer divisor of n is n/2

      so no need to check all divisors up to n

    2. can use prime decomposition

      max prime divisor is sqrt(n) so no need to test up to n instead test up to sqrt(n) or up to number than has half of the n bits

      m=(2^(ceil(ceil(log2(n))/2)+1))-1
      

      that should speed up things a bit but you need to add the computation of non prime divisors

    3. this looks like some kind of series (prime decomposition)

      divisors  | prime_divisors | non_prime_divisors              | LCM(all divisors)
      1         | 1               |                                 | 1
      2         | 1,2             |                                 | 2
      3         | 1,2             | 4                               | 4
      4         | 1,2,3           | 6                               | 6
      5         | 1,2             | 4,8,16                          | 16
      6         | 1,2,3           | 4,6,12                          | 12
      7         | 1,2             | 4,8,16,32,64                    | 64
      8         | 1,2,3           | 4,6,8,12,24                     | 24
      ...
      16        | 1,2,3,5        |4,6,8,10,12,15,20,24,30,40,60,120 | 120
      

      so try to find the equation that generates this order and then simply compute the n-th iteration in modulo arithmetics (simple PI operation ... modmul). I can see the even and odd elements have separate equation ...

    [edit1] decomposition up to 16 divisors

      1:    1
      2:    1,   2
      3:    1,   2,   4
      4:    1,   2,   3,   6
      5:    1,   2,   4,   8,  16
      6:    1,   2,   3,   4,   6,  12
      7:    1,   2,   4,   8,  16,  32,  64
      8:    1,   2,   3,   4,   6,   8,  12,  24
      9:    1,   2,   3,   4,   6,   9,  12,  18,  36
     10:    1,   2,   3,   4,   6,   8,  12,  16,  24,  48
     11:    1,   2,   4,   8,  16,  32,  64, 128, 256, 512,1024
     12:    1,   2,   3,   4,   5,   6,  10,  12,  15,  20,  30,  60
     13:    1,   2,   4,   8,  16,  32,  64, 128, 256, 512,1024,2048,4096
     14:    1,   2,   3,   4,   6,   8,  12,  16,  24,  32,  48,  64,  96, 192
     15:    1,   2,   3,   4,   6,   8,   9,  12,  16,  18,  24,  36,  48,  72, 144
     16:    1,   2,   3,   4,   5,   6,   8,  10,  12,  15,  20,  24,  30,  40,  60, 120
    
    0 讨论(0)
  • 2020-12-29 08:37

    Fully functional code.

    def find_smallest_number(num):
        number2=0
    
        if(num%8==0):
            number2=min(2**((num/4)-1)*3**1*5**1 , 2**((num//2)-1)*3**1)
        elif(num%9==0):
            number2=2**((num/9)-1)*3**2*5**2   
        elif(num%2==0 and num%3==0):
            number2=2**((num/6)-1)*3**2*5**1   
        elif((num%4==0)):
            number2=2**((num/4)-1)*3**1*5**1
        elif(num%2==0):
            number2=2**((num/2)-1)*3**1
        else:
            number2=2**(num-1)         
    
        return number2   
    
    
    num=32
    print("The number of divisors :",num)
    result=find_smallest_number(num)
    print("The smallest number having",num," divisors:",result)
    
    0 讨论(0)
提交回复
热议问题