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++
// C++ program to find the largest // composite number that divides // N which is less than N #include <bits/stdc++.h> using namespace std; // Function to check whether // a number is prime or not bool isPrime( int n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for ( int i = 2; i < n; i++) if (n % i == 0) return false ; return true ; } // Function that find the largest // composite number which divides // N and less than N int getSmallestPrimefactor( int n) { // Find the prime number for ( int i = 2; i <= sqrt (n); i++) { if (n % i == 0) return i; } } // Driver's Code int main() { int N = 100; int a; // Get the smallest prime // factor a = getSmallestPrimefactor(N); // Check if (N/a) is prime // or not // If Yes print "-1" if (isPrime(N / a)) { cout << "-1" ; } // Else print largest composite // number (N/a) else { cout << N / a; } return 0; } |
Java
// Java program to find the largest // composite number that divides // N which is less than N import java.util.*; class GFG { // Function to check whether // a number is prime or not static boolean isPrime( int n) { // Corner case if (n <= 1 ) return false ; // Check from 2 to n-1 for ( int i = 2 ; i < n; i++) { if (n % i == 0 ) return false ; } return true ; } // Function that find the largest // composite number which divides // N and less than N static int getSmallestPrimefactor( int n) { // Find the prime number for ( int i = 2 ; i <= Math.sqrt(n); i++) { if (n % i == 0 ) return i; } return - 1 ; } // Driver Code public static void main(String[] args) { int N = 100 ; int a; // Get the smallest prime // factor a = getSmallestPrimefactor(N); // Check if (N/a) is prime or // not. If Yes print "-1" if (isPrime(N / a)) { System.out.print( "-1" ); } // Else print largest composite // number (N/a) else { System.out.print(N / a); } } } // This code is contributed by amal kumar choubey |
Python3
# Python3 program to find the largest # composite number that divides # N which is less than N import math # Function to check whether # a number is prime or not def isPrime(n): # Corner case if (n < = 1 ): return False # Check from 2 to n-1 for i in range ( 2 , n): if (n % i = = 0 ): return False return True # Function that find the largest # composite number which divides # N and less than N def getSmallestPrimefactor(n): # Find the prime number for i in range ( 2 , ( int )(math.sqrt(n) + 1 )): if (n % i = = 0 ): return i return - 1 # Driver Code N = 100 # Get the smallest prime # factor a = getSmallestPrimefactor(N) # Check if (N/a) is prime # or not. If Yes print "-1" if ((isPrime(( int )(N / a)))): print ( - 1 ) # Else print largest composite # number (N/a) else : print (( int )(N / a)) # This code is contributed by grand_master |
C#
// C# program to find the largest // composite number that divides // N which is less than N using System; class GFG { // Function to check whether // a number is prime or not static bool isPrime( int n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for ( int i = 2; i < n; i++) { if (n % i == 0) return false ; } return true ; } // Function that find the largest // composite number which divides // N and less than N static int getSmallestPrimefactor( int n) { // Find the prime number for ( int i = 2; i <= Math.Sqrt(n); i++) { if (n % i == 0) return i; } return -1; } // Driver Code public static void Main() { int N = 100; int a; // Get the smallest prime // factor a = getSmallestPrimefactor(N); // Check if (N/a) is prime or // not. If Yes print "-1" if (isPrime(N / a)) { Console.Write( "-1" ); } // Else print largest composite // number (N/a) else { Console.Write(N / a); } } } // This code is contributed by Code_Mech |
Javascript
<script> // Javascript program to find the largest // composite number that divides // N which is less than N // Function to check whether // a number is prime or not function isPrime(n) { // Corner case if (n <= 1) return false ; var i; // Check from 2 to n-1 for (i = 2; i < n; i++) if (n % i == 0) return false ; return true ; } // Function that find the largest // composite number which divides // N and less than N function getSmallestPrimefactor(n) { var i; // Find the prime number for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return i; } } // Driver's Code var N = 100; var a; // Get the smallest prime // factor a = getSmallestPrimefactor(N); // Check if (N/a) is prime // or not // If Yes print "-1" if (isPrime(N / a)) document.write( "-1" ); // Else print largest composite // number (N/a) else document.write(N/a); </script> |
50
Time Complexity: O(sqrt(N))
Auxiliary space: O(1)
Please Login to comment...