# Number with maximum number of prime factors

Given an integer N. The task is to find a number that is smaller than or equal to N and has maximum prime factors. In case there are two or more numbers with same maximum number of prime factors, find the smallest of all.

Examples:

```Input : N = 10
Output : 6
Number of prime factor of:
1 : 0
2 : 1
3 : 1
4 : 1
5 : 1
6 : 2
7 : 1
8 : 1
9 : 1
10 : 2
6 and 10 have maximum (2) prime factor
but 6 is smaller.

Input : N = 40
Output : 30
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (brute force):
For each integer from 1 to N, find the number of prime factor of each integer and find the smallest number having maximum number of prime factors.

Method 2 (Better Approach):
Use sieve method to count number of prime factor of each number less than N. And find the minimum number having maximum count.

Below is C++ implementation of this approach:

```// C++ program to find integer having maximum number
// of prime factor in first N natural numbers.
#include<bits/stdc++.h>
using namespace std;

// Return smallest number having maximum
// prime factors.
int maxPrimefactorNum(int N)
{
int arr[N + 5];
memset(arr, 0, sizeof(arr));

// Sieve of eratosthenes method to count
// number of prime factors.
for (int i = 2; i*i <= N; i++)
{
if (!arr[i])
for (int j = 2*i; j <= N; j+=i)
arr[j]++;

arr[i] = 1;
}

int maxval = 0, maxint = 1;

// Finding number having maximum number
// of prime factor.
for (int i = 1; i <= N; i++)
{
if (arr[i] > maxval)
{
maxval = arr[i];
maxint = i;
}
}

return maxint;
}

// Driven Program
int main()
{
int N = 40;
cout << maxPrimefactorNum(N) << endl;
return 0;
}
```

Output:

```30
```

Method 3 (efficient approach):
Generate all prime number before N using Sieve. Now, multiply consecutive prime numbers (starting from first prime number) one after another until the product is less than N.

Below is C++ implementation of this approach:

```// C++ program to find integer having maximum number
// of prime factor in first N natural numbers
#include<bits/stdc++.h>
using namespace std;

// Return smallest number having maximum prime factors.
int maxPrimefactorNum(int N)
{
bool arr[N + 5];
memset(arr, true, sizeof(arr));

// Sieve of eratosthenes
for (int i = 3; i*i <= N; i += 2)
{
if (!arr[i])
for (int j = i*i; j <= N; j+=i)
arr[j] = false;
}

// Storing prime numbers.
vector<int> prime;
prime.push_back(2);

for(int i = 3; i <= N; i += 2)
if(arr[i])
prime.push_back(i);

// Generating number having maximum prime factors.
int i = 0, ans = 1;
while (ans*prime[i] <= N && i < prime.size())
{
ans *= prime[i];
i++;
}

return ans;
}

// Driven Program
int main()
{
int N = 40;
cout << maxPrimefactorNum(N) << endl;
return 0;
}
```

Output:

```30
```

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1 Average Difficulty : 1/5.0
Based on 4 vote(s)