Finding solutions for a given pair (number of factors, number of prime factors)

后端 未结 3 458
滥情空心
滥情空心 2020-12-22 13:50

I have been given x and k, where x is the number of factors of a number A, and k is the number of prime fact

3条回答
  •  自闭症患者
    2020-12-22 14:35

    First your implementation is too inefficient

    • Don't call a function inside the for loop like this

      for (long long int numbers = 1; numbers < pow(10, 9) && !stop; numbers++)
      

      pow will be called unnecessarily on each iteration. Save every constant to a variable before the loop. But in this case just use numbers < 1e9

    • To get all factors of n you just need to loop until sqrt(n). You're doing for (long long int a = 1; a <= numbers && !stop; a++) { if (numbers % a == 0) { so for example instead of only 104 loops for 108 you'll need 108 loops. If numbers % a == 0 then both a and numbers/a are factors of numbers

    But the real issue here is that your algorithm is flawed. You can get all factors of A if you know the prime factors of A and their exponents. If A = p1m p2n ... pkp then A will have the following factors:

    • p1i for i = 1..m
    • p2i for i = 1..n
    • ...
    • pki for i = 1..p
    • Factors from 2 prime factors: p1ip2j, p1ip3j,... p1ipkj, p2ip3j, p2ip4j,... p2ipkj,... pk-1ipkj
    • Factors from 3 prime factors...
    • Factors from k prime factors

    This is purely a combination counting issue that can be done easily without even knowing A. Notice that the number of factors from k prime factors has some relation to the number of factors from k-1 prime factors

提交回复
热议问题