# Numbers with exactly 3 divisors

Given a number N, print all numbers in range from 1 to N having exactly 3 divisors.
Examples:

```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.
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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.

## C/C++

 `// C++ program to print all three-primes smaller than ` `// or equal to n using Sieve of Eratosthenes ` `#include ` `using` `namespace` `std; ` ` `  `// Generates all primes upto n and prints their squares ` `void` `numbersWith3Divisors(``int` `n) ` `{ ` `    ``bool` `prime[n+1]; ` `    ``memset``(prime, ``true``, ``sizeof``(prime)); ` `    ``prime = prime = 0; ` ` `  `    ``for` `(``int` `p=2; p*p<=n; p++) ` `    ``{ ` `        ``// If prime[p] is not changed, then it is a prime ` `        ``if` `(prime[p] == ``true``) ` `        ``{ ` `           ``// Update all multiples of p ` `           ``for` `(``int` `i=p*2; i<=n; i += p) ` `              ``prime[i] = ``false``; ` `        ``} ` `    ``} ` ` `  `    ``// print squares of primes upto n. ` `    ``cout << ``"Numbers with 3 divisors :\n"``; ` `    ``for` `(``int` `i=0;  i*i <= n ; i++) ` `        ``if` `(prime[i]) ` `          ``cout << i*i << ``" "``; ` `} ` ` `  `// driver program  ` `int` `main() ` `{ ` `    ``// sieve(); ` `    ``int` `n = 96; ` `    ``numbersWith3Divisors(n);  ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to print all three-primes smaller than ` `// or equal to n using Sieve of Eratosthenes ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `    ``// Generates all primes upto n and prints their squares ` `    ``static` `void` `numbersWith3Divisors(``int` `n) ` `    ``{ ` `        ``boolean``[] prime = ``new` `boolean``[n+``1``]; ` `        ``Arrays.fill(prime, ``true``); ` `        ``prime[``0``] = prime[``1``] = ``false``; ` `  `  `        ``for` `(``int` `p=``2``; p*p<=n; p++) ` `        ``{ ` `            ``// If prime[p] is not changed, then it is a prime ` `            ``if` `(prime[p] == ``true``) ` `            ``{ ` `                ``// Update all multiples of p ` `                ``for` `(``int` `i=p*``2``; i<=n; i += p) ` `                    ``prime[i] = ``false``; ` `            ``} ` `        ``} ` `  `  `        ``// print squares of primes upto n ` `        ``System.out.println(``"Numbers with 3 divisors : "``); ` `        ``for` `(``int` `i=``0``;  i*i <= n ; i++) ` `            ``if` `(prime[i]) ` `                ``System.out.print(i*i + ``" "``); ` `    ``} ` `     `  `    ``// driver program ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `n = ``96``; ` `        ``numbersWith3Divisors(n);  ` `    ``} ` `} ` ` `  `// Contributed by Pramod Kumar `

## Python3

 `# Python3 program to print all three-primes smaller than  ` `# or equal to n using Sieve of Eratosthenes  ` ` `  `# Generates all primes upto n and prints their squares  ` `def` `numbersWith3Divisors(n):  ` `  `  `    ``prime``=``[``True``]``*``(n``+``1``);  ` `    ``prime[``0``] ``=` `prime[``1``] ``=` `False``; ` `    ``p``=``2``; ` `    ``while` `(p``*``p<``=``n): ` `        ``# If prime[p] is not changed, then it is a prime  ` `        ``if` `(prime[p] ``=``=` `True``): ` `            ``# Update all multiples of p ` `            ``for` `i ``in` `range``(p``*``2``,n``+``1``,p): ` `                ``prime[i] ``=` `False``;  ` `        ``p``+``=``1``;  ` ` `  `    ``# print squares of primes upto n.  ` `    ``print``(``"Numbers with 3 divisors :"``); ` `    ``i``=``0``; ` `    ``while` `(i``*``i <``=` `n):  ` `        ``if` `(prime[i]): ` `            ``print``(i``*``i,end``=``" "``); ` `        ``i``+``=``1``; ` ` `  `# driver program  ` ` `  `n ``=` `96``;  ` `numbersWith3Divisors(n);  ` ` `  `# this code is contributed by mits `

## C#

 `// C# program to print all three-primes smaller than ` `// or equal to n using Sieve of Eratosthenes ` ` `  `class` `GFG  ` `{ ` `    ``// Generates all primes upto n and prints their squares ` `    ``static` `void` `numbersWith3Divisors(``int` `n) ` `    ``{ ` `        ``bool``[] prime = ``new` `bool``[n+1]; ` `        ``prime = prime = ``true``; ` ` `  `        ``for` `(``int` `p=2; p*p<=n; p++) ` `        ``{ ` `            ``// If prime[p] is not changed, then it is a prime ` `            ``if` `(prime[p] == ``false``) ` `            ``{ ` `                ``// Update all multiples of p ` `                ``for` `(``int` `i=p*2; i<=n; i += p) ` `                    ``prime[i] = ``true``; ` `            ``} ` `        ``} ` ` `  `        ``// print squares of primes upto n ` `        ``System.Console.WriteLine(``"Numbers with 3 divisors : "``); ` `        ``for` `(``int` `i=0; i*i <= n ; i++) ` `            ``if` `(!prime[i]) ` `                ``System.Console.Write(i*i + ``" "``); ` `    ``} ` `     `  `    ``// driver program ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `n = 96; ` `        ``numbersWith3Divisors(n);  ` `    ``} ` `} ` ` `  `// This code is Contributed by mits `

## PHP

 ` `

Output:

```Numbers with 3 divisors :
4 9 25 49
```

My Personal Notes arrow_drop_up

Improved By : Mithun Kumar

Article Tags :

6

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.