# Find the largest composite number that divides N but is strictly lesser than N

• Last Updated : 27 Apr, 2021

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:
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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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).
Following are the steps:

1. Find the smallest prime number of N (say a).
2. Check if (N/a) is prime or not. If yes then we can’t find the largest composite number.
3. 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 ``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

 ``
Output:
`50`

Time Complexity: O(sqrt(N))

My Personal Notes arrow_drop_up