Given a number N, print all numbers in 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 on 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 include 1 and that number itself resulting into just a single divisor other than 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, 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, suct that i*i <=N.
Numbers with 3 divisors : 4 9 25 49
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 firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if sum of divisors of two numbers are same
- Common Divisors of Two Numbers
- Common divisors of N numbers
- Sum of common divisors of two numbers A and B
- Count of numbers below N whose sum of prime divisors is K
- Number of divisors of product of N numbers
- Find numbers with K odd divisors in a given range
- C++ Program for Common Divisors of Two Numbers
- Sum of all perfect square divisors of numbers from 1 to N
- Find all divisors of first N natural numbers
- Sum of all prime divisors of all the numbers in range L-R
- Find numbers with n-divisors in a given range
- Divide the two given numbers by their common divisors
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Java Program for Common Divisors of Two Numbers
- Sum of all proper divisors of natural numbers in an array
- Find two numbers whose divisors are given in a random order
- Count the numbers < N which have equal number of divisors as K
- Find the number of divisors of all numbers in the range [1, n]
Improved By : Mithun Kumar