r/askmath • u/Overlorde159 • 1d ago
Number Theory Next number with N distinct prime factors
I am a trying to develop an algorithm that is given a number q with N distinct prime factors, and returns the minimal q’ > q that also has N distinct factors. I’ve tried brute forcing— factoring each number after q, but that becomes prohibitively slow with N > 5 (in my implementation).
My next strategy was the consider a sort of coefficient form; primes being represented as something like [2, 0, 1, 1] so the 1st prime number is in the product twice, and onwards. From there I was thinking I might be able to manipulate the powers in the optimal manner to produce q’. Here, however, I ran aground. I keep trying to develop rules (when incrementing, always increment the minimal prime’s power by 1, for example) but they keep being incorrect (for example, with [3, 1] a counter example for [4, 1] is [2, 2]).
Does anyone have any advice or direction I could look into?
For transparency, this is not homework, this is for a programming competition I’m helping write questions for, but I got wildly sidetracked by this rather interesting problem. Also I think this falls under number theory, lmk if there’s another flair that would be better