I need to sort a range of numbers. The numbers can be from 0 to 1000. The user enters a range of numbers between 0 and 1000. So if they for example maybe enter 0 -> 500.
My goal would be to take all the numbers in the range given and output all the numbers in that range that have only 4 possible divisors (including 1 and itself.
I’m wondering if I should use an algorithm such as this one, or if I should check and int array that contains all the numbers from 1 – 1000 that are divisible by 4 (so the array doesn’t have 1-1000 it has the numbers in that range that would be divisible 4 times only. Would require manual work to add all the possible divisors to that array).
I’m looking to make it efficient just I’m not sure if using such an algorithm would be very fast for this.
You’re looking for a number that’s the product of two primes edit: or the cube of a prime. Since your numbers are small, you can just make a short table of the primes between 2 and 500. Then, starting with 2, multiply each prime by each of the primes above it until you’re over your upper limit.
Edit: Is this homework? If it is, I’ve probably said too much already. If it isn’t, I can provide some code or pseudocode if my answer doesn’t make sense.
Edit: Thanks, Daniel Fischer: I missed the cubes of primes. You can check for and include these in your outer loop. (Before you multiply a prime by each of those above it, see if its cube falls in in the desired range.)
Generally:
It’s certainly fast enough for numbers on this scale. I have it in under a millisecond (to find 292 numbers between 0 and 1000. I used hardcoded primes — you only need 95 of them for this range of inputs.)