Given a number N, print all numbers in the range from 1 to N having exactly 3 divisors.
Examples:
Input: N = 16
Output: 4 9
Explanation: 4 and 9 have exactly three divisors.
Input: N = 49
Output: 4 9 25 49
Explanation: 4, 9, 25 and 49 have exactly three divisors.
Mathematical approach to find Numbers with exactly 3 divisors:
To solve the problem follow the below idea:
Idea: After having a close look at the examples mentioned above, you have noticed that all the required numbers are perfect squares and that too of only prime numbers.
Proof: Suppose the number is N, and it is a perfect square with square root X such that X is prime.
Now if we find the factors of N, it will always have following combinations:
Therefore the required numbers will have only three numbers as their divisors:
- 1,
- that number itself, and
- just a single divisor in between 1 and the number.
Algorithm: We can generate all primes within a set using any sieve method efficiently and then we should take all primes i, such that i*i <=N.
Follow the below steps to solve the problem:
- Generate the prime numbers from 1 to N using any sieve method efficiently
- Print all the prime numbers(X) between 1 to N, such as X2 is less than or equal to N
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void numbersWith3Divisors( int N)
{
bool prime[N + 1];
memset (prime, true , sizeof (prime));
prime[0] = prime[1] = 0;
for ( int p = 2; p * p <= N; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i <= N; i += p)
prime[i] = false ;
}
}
cout << "Numbers with 3 divisors :\n" ;
for ( int i = 0; i * i <= N; i++)
if (prime[i])
cout << i * i << " " ;
}
int main()
{
int N = 96;
numbersWith3Divisors(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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] == true ) {
for ( int i = p * 2 ; i <= N; i += p)
prime[i] = false ;
}
}
System.out.println( "Numbers with 3 divisors : " );
for ( int i = 0 ; i * i <= N; i++)
if (prime[i])
System.out.print(i * i + " " );
}
public static void main(String[] args)
{
int N = 96 ;
numbersWith3Divisors(N);
}
}
|
Python3
def numbersWith3Divisors(N):
prime = [ True ] * (N + 1 )
prime[ 0 ] = prime[ 1 ] = False
p = 2
while (p * p < = N):
if (prime[p] = = True ):
for i in range (p * 2 , N + 1 , p):
prime[i] = False
p + = 1
print ( "Numbers with 3 divisors :" )
i = 0
while (i * i < = N):
if (prime[i]):
print (i * i, end = " " )
i + = 1
if __name__ = = "__main__" :
N = 96
numbersWith3Divisors(N)
|
C#
class GFG {
static void numbersWith3Divisors( int N)
{
bool [] prime = new bool [N + 1];
prime[0] = prime[1] = true ;
for ( int p = 2; p * p <= N; p++) {
if (prime[p] == false ) {
for ( int i = p * 2; i <= N; i += p)
prime[i] = true ;
}
}
System.Console.WriteLine(
"Numbers with 3 divisors : " );
for ( int i = 0; i * i <= N; i++)
if (!prime[i])
System.Console.Write(i * i + " " );
}
public static void Main()
{
int N = 96;
numbersWith3Divisors(N);
}
}
|
PHP
<?php
function numbersWith3Divisors( $N )
{
$prime = array_fill (0, $N + 1, true);
$prime [0] = $prime [1] = false;
for ( $p = 2; $p * $p <= $N ; $p ++)
{
if ( $prime [ $p ] == true)
{
for ( $i = $p * 2; $i <= $N ; $i += $p )
$prime [ $i ] = false;
}
}
echo "Numbers with 3 divisors :\n" ;
for ( $i = 0; $i * $i <= $N ; $i ++)
if ( $prime [ $i ])
echo $i * $i . " " ;
}
$N = 96;
numbersWith3Divisors( $N );
?>
|
Javascript
function numbersWith3Divisors(n)
{
let prime = new Array(n+1);
prime.fill( true );
prime[0] = prime[1] = 0;
for (let p = 2; p*p <= n; p++)
{
if (prime[p] == true )
{
for (let i = p*2; i <= n; i += p)
prime[i] = false ;
}
}
document.write( "Numbers with 3 divisors :" + "</br>" );
for (let i = 0; i*i <= n ; i++)
if (prime[i])
document.write(i*i + " " );
}
let n = 96;
numbersWith3Divisors(n);
|
Output
Numbers with 3 divisors :
4 9 25 49
Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(N)
Numbers with exactly 3 divisors using constant space:
Run a loop from 2 to sqrt(N) and check if the current element is prime or not, if it is so then print that number, but this method will increase the time complexity of the solution
Follow the below steps to solve the problem:
- 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 reviewed 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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void numbersWith3Divisors( int );
bool isPrime( int );
void numbersWith3Divisors( int N)
{
cout << "Numbers with 3 divisors : " << endl;
for ( int i = 2; i * i <= N; i++) {
if (isPrime(i)) {
if (i * i <= N) {
cout << i * i << " " ;
}
}
}
}
bool isPrime( int N)
{
for ( int i = 2; i * i <= N; i++) {
if (N % i == 0)
return false ;
}
return true ;
}
int main()
{
int N = 122;
numbersWith3Divisors(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void numbersWith3Divisors( int N)
{
System.out.println( "Numbers with 3 divisors : " );
for ( int i = 2 ; i * i <= N; i++) {
if (isPrime(i)) {
if (i * i <= N) {
System.out.print(i * i + " " );
}
}
}
}
public static boolean isPrime( int N)
{
for ( int i = 2 ; i * i <= N; i++) {
if (N % i == 0 )
return false ;
}
return true ;
}
public static void main(String[] args)
{
int N = 122 ;
numbersWith3Divisors(N);
}
}
|
Python3
def numbersWith3Divisors(N):
print ( "Numbers with 3 divisors : " )
i = 2
while i * i < = N:
if (isPrime(i)):
if (i * i < = N):
print (i * i, end = " " )
i + = 1
def isPrime(N):
i = 2
while i * i < = N:
if N % i = = 0 :
return False
i + = 1
return True
if __name__ = = "__main__" :
N = 122
numbersWith3Divisors(N)
|
C#
using System;
class GFG {
static void numbersWith3Divisors( int N)
{
Console.WriteLine( "Numbers with 3 divisors : " );
for ( int i = 2; i * i <= N; i++) {
if (isPrime(i)) {
if (i * i <= N) {
Console.Write(i * i + " " );
}
}
}
}
public static bool isPrime( int N)
{
for ( int i = 2; i * i <= N; i++) {
if (N % i == 0)
return false ;
}
return true ;
}
static void Main()
{
int N = 122;
numbersWith3Divisors(N);
}
}
|
Javascript
function numbersWith3Divisors(n)
{
document.write( "Numbers with 3 divisors : " );
for (let i = 2; i * i <= n; i++)
{
if (isPrime(i))
{
if (i * i <= n)
{
document.write(i * i + " " );
}
}
}
}
function isPrime(n)
{
if (n == 0 || n == 1)
return false ;
for (let i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
let n = 122;
numbersWith3Divisors(n);
|
Output
Numbers with 3 divisors :
4 9 25 49 121
Time Complexity: O(sqrt N2)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Sep, 2023
Like Article
Save Article