Open in App
Not now

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

• Last Updated : 19 Oct, 2022

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

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:

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))
Auxiliary space: O(1)

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.54789454,"math_prob":0.97454554,"size":5822,"snap":"2022-40-2023-06","text_gpt3_token_len":1827,"char_repetition_ratio":0.1742867,"word_repetition_ratio":0.5140962,"special_character_ratio":0.34524217,"punctuation_ratio":0.10540301,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9998845,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-01T02:00:41Z\",\"WARC-Record-ID\":\"<urn:uuid:c928f6dc-c6a2-4fbd-8f7c-63c4eb438381>\",\"Content-Length\":\"197772\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:721c5b28-ef44-4001-bd85-8de60736656e>\",\"WARC-Concurrent-To\":\"<urn:uuid:a9292005-9708-499c-afbe-051e9853a559>\",\"WARC-IP-Address\":\"104.97.85.57\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/find-the-largest-composite-number-that-divides-n-but-is-strictly-lesser-than-n/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:73HL2X3Q2KGWHOGCL2FVZUTGKHLILIXA\",\"WARC-Block-Digest\":\"sha1:FACBA6WREFSBF4JSLRCDB2JORX6BN7ZG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499899.9_warc_CC-MAIN-20230201013650-20230201043650-00369.warc.gz\"}"}