@Filiprei I wouldn't give up this is actually a very handy problem to solve. As it makes you think about the efficiency of your code.
Here is a tip, try generating all the prime numbers that could be factors of numbers up to the maximum which is 1 billion. Then do a bounded Sieve Of Eratosthenes for the range which is requested(Which can only be n-m <= 100,000).