Find the largest composite number that divides N but is strictly lesser than N
Given a composite number N, the task is to find the largest composite number that divides N and is strictly lesser than N. If there is no such number exist print -1.
Examples:
Input: N = 16
Output: 8
Explanation:
All numbers that divide 16 are { 1, 2, 4, 8, 16 }
out of which 8 is largest composite number(lesser than 16) that divides 16.
Input: N = 100
Output: -1
Approach:
Since N is a composite number, therefore N can be the product of two numbers such that one is prime number and another is a composite number and if we can’t find such a pair for N then the largest composite number which is less than N that divides N doesn’t exist.
To find the largest composite number find the smallest prime number(say a) that divides N. Then the largest composite number that divides N and less than N can be given by (N/a).
The following are the steps:
- Find the smallest prime number of N (say a).
- Check if (N/a) is prime or not. If yes then we can’t find the largest composite number.
- Else (N/a) is the largest composite number that divides N and less than N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
int getSmallestPrimefactor( int n)
{
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0)
return i;
}
}
int main()
{
int N = 100;
int a;
a = getSmallestPrimefactor(N);
if (isPrime(N / a)) {
cout << "-1" ;
}
else {
cout << N / a;
}
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i < n; i++) {
if (n % i == 0 )
return false ;
}
return true ;
}
static int getSmallestPrimefactor( int n)
{
for ( int i = 2 ; i <= Math.sqrt(n); i++) {
if (n % i == 0 )
return i;
}
return - 1 ;
}
public static void main(String[] args)
{
int N = 100 ;
int a;
a = getSmallestPrimefactor(N);
if (isPrime(N / a)) {
System.out.print( "-1" );
}
else {
System.out.print(N / a);
}
}
}
|
Python3
import math
def isPrime(n):
if (n < = 1 ):
return False
for i in range ( 2 , n):
if (n % i = = 0 ):
return False
return True
def getSmallestPrimefactor(n):
for i in range ( 2 , ( int )(math.sqrt(n) + 1 )):
if (n % i = = 0 ):
return i
return - 1
N = 100
a = getSmallestPrimefactor(N)
if ((isPrime(( int )(N / a)))):
print ( - 1 )
else :
print (( int )(N / a))
|
C#
using System;
class GFG {
static bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i < n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
static int getSmallestPrimefactor( int n)
{
for ( int i = 2; i <= Math.Sqrt(n); i++) {
if (n % i == 0)
return i;
}
return -1;
}
public static void Main()
{
int N = 100;
int a;
a = getSmallestPrimefactor(N);
if (isPrime(N / a)) {
Console.Write( "-1" );
}
else {
Console.Write(N / a);
}
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
var i;
for (i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
function getSmallestPrimefactor(n)
{
var i;
for (i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return i;
}
}
var N = 100;
var a;
a = getSmallestPrimefactor(N);
if (isPrime(N / a))
document.write( "-1" );
else
document.write(N/a);
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary space: O(1)
Last Updated :
19 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...