# Largest integer upto N having greatest prime factor greater than its square root

• Difficulty Level : Medium
• Last Updated : 31 Oct, 2021

Given a positive integer N, the task is to find the largest number in the range [1, N] such that the square root of the number is less than its greatest prime factor.

Input: N = 15
Output: 15
Explanation: The prime factors of 15 are {3, 5}. The square root of 15 is 3.87 (i.e, 3.87 < 5). Therefore 15 is the largest valid integer in the given range.

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.

Input: N = 25
Output: 23

Approach: The given problem can be solved by using the Sieve of Eratosthenes with a few modifications. Create an array gpf[], which stores the Greatest Prime Factor of all integers in the given range. Initially, gpf[] = {0}. Using Sieve, initialize all the indices of the array gpf[] with the greatest prime factor of the respective index similar to the algorithm discussed in this article.

Now, iterate over the range [N, 1] in a reverse manner and print the first integer whose square root of the number is less than its greatest prime factor.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `const` `int` `maxn = 100001;` `// Stores the Greatest Prime Factor``int` `gpf[maxn];` `// Modified Sieve to find the Greatest``// Prime Factor of all integers in the``// range [1, maxn]``void` `modifiedSieve()``{``    ``// Initialize the array with 0``    ``memset``(gpf, 0, ``sizeof``(gpf));``    ``gpf = 0;``    ``gpf = 1;` `    ``// Iterate through all values of i``    ``for` `(``int` `i = 2; i < maxn; i++) {` `        ``// If i is not a prime number``        ``if` `(gpf[i] > 0)``            ``continue``;` `        ``// Update the multiples of i``        ``for` `(``int` `j = i; j < maxn; j += i) {``            ``gpf[j] = max(i, gpf[j]);``        ``}``    ``}``}` `// Function to find integer in the range``// [1, N] such that its Greatest Prime``// factor is greater than its square root``int` `greatestValidInt(``int` `N)``{` `    ``modifiedSieve();` `    ``// Iterate through all values of``    ``// i in the range [N, 1]``    ``for` `(``int` `i = N; i > 0; i--) {` `        ``// If greatest prime factor of i``        ``// is greater than its square root``        ``if` `(gpf[i] > ``sqrt``(i)) {` `            ``// Return answer``            ``return` `i;``        ``}``    ``}` `    ``// If no valid integer exist``    ``return` `-1;``}` `// Driver Code``int` `main()``{``    ``int` `N = 25;``    ``cout << greatestValidInt(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``public` `class` `GFG {``    ` `    ``final` `static` `int` `maxn = ``100001``;``    ` `    ``// Stores the Greatest Prime Factor``    ``static` `int` `gpf[] = ``new` `int``[maxn];``    ` `    ``// Modified Sieve to find the Greatest``    ``// Prime Factor of all integers in the``    ``// range [1, maxn]``    ``static` `void` `modifiedSieve()``    ``{``      ` `        ``// Initialize the array with 0``        ``for` `(``int` `i = ``0``; i < maxn; i++ )``            ``gpf[i] = ``0``;``            ` `        ``gpf[``0``] = ``0``;``        ``gpf[``1``] = ``1``;``    ` `        ``// Iterate through all values of i``        ``for` `(``int` `i = ``2``; i < maxn; i++) {``    ` `            ``// If i is not a prime number``            ``if` `(gpf[i] > ``0``)``                ``continue``;``    ` `            ``// Update the multiples of i``            ``for` `(``int` `j = i; j < maxn; j += i) {``                ``gpf[j] = Math.max(i, gpf[j]);``            ``}``        ``}``    ``}``    ` `    ``// Function to find integer in the range``    ``// [1, N] such that its Greatest Prime``    ``// factor is greater than its square root``    ``static` `int` `greatestValidInt(``int` `N)``    ``{``    ` `        ``modifiedSieve();``    ` `        ``// Iterate through all values of``        ``// i in the range [N, 1]``        ``for` `(``int` `i = N; i > ``0``; i--) {``    ` `            ``// If greatest prime factor of i``            ``// is greater than its square root``            ``if` `(gpf[i] > Math.sqrt(i)) {``    ` `                ``// Return answer``                ``return` `i;``            ``}``        ``}``    ` `        ``// If no valid integer exist``        ``return` `-``1``;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `N = ``25``;``        ``System.out.println(greatestValidInt(N));``    ``}``}` `// This code is contributed by AnkThon`

## Python3

 `# python program for the above approach` `import` `math` `maxn ``=` `100001` `# Stores the Greatest Prime Factor``gpf ``=` `[``0` `for` `_ ``in` `range``(maxn)]` `# Modified Sieve to find the Greatest``# Prime Factor of all integers in the``# range [1, maxn]`  `def` `modifiedSieve():` `    ``# Initialize the array with 0``    ``gpf[``0``] ``=` `0``    ``gpf[``1``] ``=` `1` `    ``# Iterate through all values of i``    ``for` `i ``in` `range``(``2``, maxn):` `        ``# If i is not a prime number``        ``if` `(gpf[i] > ``0``):``            ``continue` `        ``# Update the multiples of i``        ``for` `j ``in` `range``(i, maxn, i):``            ``gpf[j] ``=` `max``(i, gpf[j])`  `# Function to find integer in the range``# [1, N] such that its Greatest Prime``# factor is greater than its square root``def` `greatestValidInt(N):` `    ``modifiedSieve()` `    ``# Iterate through all values of``    ``# i in the range [N, 1]``    ``for` `i ``in` `range``(N, ``0``, ``-``1``):` `        ``# If greatest prime factor of i``        ``# is greater than its square root``        ``if` `(gpf[i] > math.sqrt(i)):` `            ``# Return answer``            ``return` `i` `    ``# If no valid integer exist``    ``return` `-``1`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `25``    ``print``(greatestValidInt(N))` `# This code is contributed by rakeshsahni`

## C#

 `// C# program for the above approach``using` `System;``public` `class` `GFG {` `    ``static` `int` `maxn = 100001;` `    ``// Stores the Greatest Prime Factor``    ``static` `int``[] gpf = ``new` `int``[maxn];` `    ``// Modified Sieve to find the Greatest``    ``// Prime Factor of all integers in the``    ``// range [1, maxn]``    ``static` `void` `modifiedSieve()``    ``{` `        ``// Initialize the array with 0``        ``for` `(``int` `i = 0; i < maxn; i++)``            ``gpf[i] = 0;` `        ``gpf = 0;``        ``gpf = 1;` `        ``// Iterate through all values of i``        ``for` `(``int` `i = 2; i < maxn; i++) {` `            ``// If i is not a prime number``            ``if` `(gpf[i] > 0)``                ``continue``;` `            ``// Update the multiples of i``            ``for` `(``int` `j = i; j < maxn; j += i) {``                ``gpf[j] = Math.Max(i, gpf[j]);``            ``}``        ``}``    ``}` `    ``// Function to find integer in the range``    ``// [1, N] such that its Greatest Prime``    ``// factor is greater than its square root``    ``static` `int` `greatestValidInt(``int` `N)``    ``{` `        ``modifiedSieve();` `        ``// Iterate through all values of``        ``// i in the range [N, 1]``        ``for` `(``int` `i = N; i > 0; i--) {` `            ``// If greatest prime factor of i``            ``// is greater than its square root``            ``if` `(gpf[i] > Math.Sqrt(i)) {` `                ``// Return answer``                ``return` `i;``            ``}``        ``}` `        ``// If no valid integer exist``        ``return` `-1;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int` `N = 25;``        ``Console.WriteLine(greatestValidInt(N));``    ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``
Output:
`23`

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up