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.
- Sum of common divisors of two numbers A and B
- Check if sum of divisors of two numbers are same
- Common Divisors of Two Numbers
- Find numbers with K odd divisors in a given range
- Number of divisors of product of N numbers
- Count of numbers below N whose sum of prime divisors is K
- Sum of all prime divisors of all the numbers in range L-R
- C++ Program for Common Divisors of Two Numbers
- Divide the two given numbers by their common divisors
- Find numbers with n-divisors in a given range
- Find the number of divisors of all numbers in the range [1, n]
- Sum of all proper divisors of natural numbers in an array
- Find two numbers whose divisors are given in a random order
- Java Program for Common Divisors of Two Numbers
- Sum of numbers in a range [L, R] whose count of divisors is prime
Improved By : Mithun Kumar