Given a number N, print all numbers in the range from 1 to N having exactly 3 divisors.
Input : N = 16 Output : 4 9 4 and 9 have exactly three divisors. Divisor Input : N = 49 Output : 4 9 25 49 4, 9, 25 and 49 have exactly three divisors.
After having a close look at the examples mentioned above, you have noticed that all the required numbers are perfect squares and that too are only of primes numbers. The logic behind this is, such numbers can have only three numbers as their divisor and also that includes 1 and that number itself resulting into just a single divisor other than a number, so we can easily conclude that required are those numbers which are squares of prime numbers so that they can have only three divisors (1, the number itself and sqrt(number)). So all primes i, such that i*i is less than equal to N are three-prime numbers.
Note: We can generate all primes within a set using any sieve method efficiently and then we should all primes i, such that i*i <=N.
Below is the implementation of the above approach:
Numbers with 3 divisors : 4 9 25 49
To print all numbers in the range from 1 to N having exactly 3 divisors, the main calculation is to find those which are squares of prime numbers and less than or equal to the number. We can do this as follows:
- Start a loop for integer i from 2 to n.
- Check if i is prime or not, which can be done easily using the isPrime(n) method.
- If i is prime, check if its square is less than or equal to the given number. This will be checked only for squares of prime numbers, therefore reducing the number of checks.
- If the above condition is satisfied, the number will be printed and the loop will continue till i<=n.
In this way, no extra space will be required to store anything.
Here is an implementation of the above logic without using extra space, in Java;
Numbers with 3 divisors : 4 9 25 49 121
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Find sum of divisors of all the divisors of a natural number
- Divisors of n-square that are not divisors of n
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Check if count of even divisors of N is equal to count of odd divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count number of integers less than or equal to N which has exactly 9 divisors
- Check if a number can be expressed as a product of exactly K prime divisors
- Numbers less than N which are product of exactly two distinct prime numbers
- Common Divisors of Two Numbers
- Find numbers with K odd divisors in a given range
- Find numbers with n-divisors in a given range
- Divide the two given numbers by their common divisors
- Sum of all proper divisors of natural numbers in an array
- C++ Program for Common Divisors of Two Numbers
- Java Program for Common Divisors of Two Numbers
- Check if sum of divisors of two numbers are same
- Sum of all prime divisors of all the numbers in range L-R
- Number of divisors of product of N numbers
- Numbers in range [L, R] such that the count of their divisors is both even and prime