Maximum number of unique prime factors
Last Updated :
23 Jun, 2022
Given a number N, find the maximum number of unique prime factors any number can have in range [1, N].
Examples:
Input : N = 500
Output : 4
The maximum number of prime factors
for any number in [1, 500] is 4. A
number in range that has 4 prime
factors is 210 (2 x 3 x 5 x 7)
Input : N = 3
Output : 1
Input : N = 5000
Output : 5
Method 1 (brute force):
For each integer from 1 to N, find the number of prime factor of each integer and find the number of maximum unique prime factors.
Method 2 (Better Approach):
Use sieve method to count a number of prime factors of each number less than N. And find the minimum number having maximum count.
Below is the implementation of this approach:
C++
// C++ program to find maximum number of prime
// factors for a number in range [1, N]
#include <bits/stdc++.h>
using namespace std;
// Return smallest number having maximum
// prime factors.
int maxPrimefactorNum(int N)
{
// Sieve of eratosthenes method to count
// number of unique prime factors.
int arr[N + 1];
memset(arr, 0, sizeof(arr));
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;
}
// Return maximum element in arr[]
return *max_element(arr, arr+N);
}
// Driven Program
int main()
{
int N = 40;
cout << maxPrimefactorNum(N) << endl;
return 0;
}
Java
// Java program to find maximum
// number of prime factors for
// a number in range [1, N]
class GFG
{
static int getMax(int[] Arr)
{
int max = Arr[0];
for(int i = 1; i < Arr.length; i++)
if(Arr[i] > max)
max = Arr[i];
return max;
}
// Return smallest number
// having maximum prime factors.
static int maxPrimefactorNum(int N)
{
// Sieve of eratosthenes method
// to count number of unique
// prime factors.
int[] arr = new int[N + 1];
for (int i = 2; i * i <= N; i++)
{
if (arr[i] == 0)
for (int j = 2 * i; j <= N; j += i)
arr[j]++;
arr[i] = 1;
}
// Return maximum element in arr[]
return getMax(arr);
}
// Driver Code
public static void main(String[] args)
{
int N = 40;
System.out.println(maxPrimefactorNum(N));
}
}
// This code is contributed by mits
Python3
# Python3 program to find maximum number
# of prime factors for a number in range [1, N]
# Return smallest number having maximum
# prime factors.
def maxPrimefactorNum(N):
# Sieve of eratosthenes method to count
# number of unique prime factors.
arr = [0] * (N + 1);
i = 2;
while (i * i <= N):
if (arr[i] > 0):
for j in range(2 * i, N + 1, i):
arr[j] += 1;
i += 1;
arr[i] = 1;
# Return maximum element in arr[]
return max(arr);
# Driver Code
N = 40;
print(maxPrimefactorNum(N));
# This code is contributed by mits
C#
// C# program to find maximum
// number of prime factors for
// a number in range [1, N]
using System;
class GFG
{
static int getMax(int[] Arr)
{
int max = Arr[0];
for(int i = 1; i < Arr.Length; i++)
if(Arr[i] > max)
max = Arr[i];
return max;
}
// Return smallest number
// having maximum prime factors.
static int maxPrimefactorNum(int N)
{
// Sieve of eratosthenes method
// to count number of unique
// prime factors.
int[] arr = new int[N + 1];
for (int i = 2; i * i <= N; i++)
{
if (arr[i] == 0)
for (int j = 2 * i;
j <= N; j += i)
arr[j]++;
arr[i] = 1;
}
// Return maximum
// element in arr[]
return getMax(arr);
}
// Driver Code
public static void Main()
{
int N = 40;
Console.WriteLine(maxPrimefactorNum(N));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
<?php
// PHP program to find maximum number of prime
// factors for a number in range [1, N]
// Return smallest number having maximum
// prime factors.
function maxPrimefactorNum($N)
{
// Sieve of eratosthenes method to count
// number of unique prime factors.
$arr = array_fill(0, $N + 1, 0);
for ($i = 2; $i * $i <= $N; $i++)
{
if (!$arr[$i])
for ($j = 2 * $i; $j <= $N; $j += $i)
$arr[$j]++;
$arr[$i] = 1;
}
// Return maximum element in arr[]
return max($arr);
}
// Driver Code
$N = 40;
echo maxPrimefactorNum($N);
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript program to find maximum
// number of prime factors for
// a number in range [1, N]
function getMax(Arr)
{
let max = Arr[0];
for(let i = 1; i < Arr.length; i++)
if(Arr[i] > max)
max = Arr[i];
return max;
}
// Return smallest number
// having maximum prime factors.
function maxPrimefactorNum(N)
{
// Sieve of eratosthenes method
// to count number of unique
// prime factors.
let arr = new Array(N+1).fill(0);
for (let i = 2; i * i <= N; i++)
{
if (arr[i] == 0)
for (let j = 2 * i; j <= N; j += i)
arr[j]++;
arr[i] = 1;
}
// Return maximum element in arr[]
return getMax(arr);
}
// driver program
let N = 40;
document.write(maxPrimefactorNum(N));
</script>
Output:
3
Time Complexity: O(n log(log(n)))
Auxiliary Space: O(n)
Method 3 (efficient approach):
Generate all prime numbers before N using Sieve. Now, multiply consecutive prime numbers (starting from first prime number) one after another until the product is less than N. The idea is based on simple fact that the first set of prime numbers can cause maximum unique prime factors.
Below is the implementation of this approach:
C++
// C++ program to find maximum number of prime
// factors in first N natural numbers
#include <bits/stdc++.h>
using namespace std;
// Return maximum number of prime factors for
// any number in [1, N]
int maxPrimefactorNum(int N)
{
if (N < 2)
return 0;
// Based on Sieve of Eratosthenes
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/
bool arr[N+1];
memset(arr, true, sizeof(arr));
int prod = 1, res = 0;
for (int p=2; p*p<=N; p++)
{
// If p is prime
if (arr[p] == true)
{
for (int i=p*2; i<=N; i += p)
arr[i] = false;
// We simply multiply first set
// of prime numbers while the
// product is smaller than N.
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
// Driven Program
int main()
{
int N = 500;
cout << maxPrimefactorNum(N) << endl;
return 0;
}
Java
// Java program to find maximum
// number of prime factors in
// first N natural numbers
class GFG
{
// Return maximum number
// of prime factors for
// any number in [1, N]
static int maxPrimefactorNum(int N)
{
if (N < 2)
return 0;
// Based on Sieve of Eratosthenes
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/
boolean[] arr = new boolean[N + 1];
int prod = 1, res = 0;
for (int p = 2; p * p <= N; p++)
{
// If p is prime
if (arr[p] == false)
{
for (int i = p * 2;
i <= N; i += p)
arr[i] = true;
// We simply multiply first set
// of prime numbers while the
// product is smaller than N.
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
// Driver Code
public static void main(String[] args)
{
int N = 500;
System.out.println(maxPrimefactorNum(N));
}
}
// This code is contributed by mits
Python3
# Python3 program to find maximum number
# of prime factors in first N natural numbers
# Return maximum number of prime factors
# for any number in [1, N]
def maxPrimefactorNum(N):
if (N < 2):
return 0;
arr = [True] * (N + 1);
prod = 1;
res = 0;
p = 2;
while (p * p <= N):
# If p is prime
if (arr[p] == True):
for i in range(p * 2, N + 1, p):
arr[i] = False;
# We simply multiply first set
# of prime numbers while the
# product is smaller than N.
prod *= p;
if (prod > N):
return res;
res += 1;
p += 1;
return res;
# Driver Code
N = 500;
print(maxPrimefactorNum(N));
# This code is contributed by mits
C#
// C# program to find maximum number of
// prime factors in first N natural numbers
using System;
class GFG
{
// Return maximum number of prime
// factors for any number in [1, N]
static int maxPrimefactorNum(int N)
{
if (N < 2)
return 0;
// Based on Sieve of Eratosthenes
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/
bool[] arr = new bool[N + 1];
int prod = 1, res = 0;
for (int p = 2; p * p <= N; p++)
{
// If p is prime
if (arr[p] == false)
{
for (int i = p * 2;
i <= N; i += p)
arr[i] = true;
// We simply multiply first set
// of prime numbers while the
// product is smaller than N.
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
// Driver Code
public static void Main()
{
int N = 500;
Console.WriteLine(maxPrimefactorNum(N));
}
}
// This code is contributed
// by 29AjayKumar
PHP
<?php
// PHP program to find maximum
// number of prime factors in
// first N natural numbers
// Return maximum number of
// prime factors for any
// number in [1, N]
function maxPrimefactorNum($N)
{
if ($N < 2)
return 0;
$arr = array_fill(0, ($N + 1), true);
$prod = 1;
$res = 0;
for ($p = 2;
$p * $p <= $N; $p++)
{
// If p is prime
if ($arr[$p] == true)
{
for ($i = $p * 2;
$i <= $N; $i += $p)
$arr[$i] = false;
// We simply multiply first set
// of prime numbers while the
// product is smaller than N.
$prod *= $p;
if ($prod > $N)
return $res;
$res++;
}
}
return $res;
}
// Driver Code
$N = 500;
echo maxPrimefactorNum($N) . "\n";
// This code is contributed by mits
?>
JavaScript
<script>
// javascript program to find maximum
// number of prime factors in
// first N natural numbers
// Return maximum number
// of prime factors for
// any number in [1, N]
function maxPrimefactorNum(N)
{
if (N < 2)
return 0;
// Based on Sieve of Eratosthenes
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/
arr = Array.from({length: N + 1}, (_, i) => false);
var prod = 1, res = 0;
for (var p = 2; p * p <= N; p++)
{
// If p is prime
if (arr[p] == false)
{
for (var i = p * 2;
i <= N; i += p)
arr[i] = true;
// We simply multiply first set
// of prime numbers while the
// product is smaller than N.
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
// Driver Code
var N = 500;
document.write(maxPrimefactorNum(N));
// This code is contributed by 29AjayKumar
</script>
Output:
4
Time Complexity: O(n log(log n))
Auxiliary Space: O(n)
Similar Reads
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 the same maximum number of prime factors, find the smallest of all.Examples: Input : N = 10Output : 6Number of prime factor of:1 : 02 : 13 : 1
15+ min read
Find sum of a number and its maximum prime factor Given an integer N, the task is to find the sum of N and it's maximum prime factor.Examples: Input: 19 Output: 38 Maximum prime factor of 19 is 19. Hence, 19 + 19 = 38Input: 8 Output: 10 8 + 2 = 10 Approach: Find the largest prime factor of the number and store it in maxPrimeFact then print the valu
6 min read
Product of unique prime factors of a number Given a number n, we need to find the product of all of its unique prime factors. Prime factors: It is basically a factor of the number that is a prime number itself. Examples : Input: num = 10 Output: Product is 10 Explanation: Here, the input number is 10 having only 2 prime factors and they are 5
11 min read
Prime factors of a big number Given a number N, print all the prime factors and their powers. Here N <= 10^18Examples : Input : 250 Output : 2 1 5 3Explanation: The prime factors of 250 are 2and 5. 2 appears once in the prime factorization of and 5 is thrice in it. Input : 1000000000000000000Output : 2 18 5 18Explanation: The
7 min read
N-th prime factor of a given number Given Q queries which consist of two integers, one is number(1 <= number <= 106) and the other is N., the task is to find the N-th prime factor of the given number. Examples: Input: Number of Queries, Q = 4 number = 6, N = 1 number = 210, N = 3 number = 210, N = 2 number = 60, N = 2Output: 2 5
15 min read
Print all prime factors of a given number Given a number n, the task is to find all prime factors of n.Examples:Input: n = 24Output: 2 2 2 3Explanation: The prime factorization of 24 is 23Ã3.Input: n = 13195Output: 5 7 13 29Explanation: The prime factorization of 13195 is 5Ã7Ã13Ã29.Approach:Every composite number has at least one prime fact
6 min read
k-th prime factor of a given number Given two numbers n and k, print k-th prime factor among all prime factors of n. For example, if the input number is 15 and k is 2, then output should be "5". And if the k is 3, then output should be "-1" (there are less than k prime factors). Examples: Input : n = 225, k = 2 Output : 3 Prime factor
15+ min read
Number less than equals to N with maximum product of prime factors Given a number N, the task is to find the number which is less than or equals to N whose product of prime factors is maximum.Note: If there is more than one number whose maximum product is equal, then print the smallest number of them.Examples: Input: N = 12 Output: 11 Explanation: Product of prime
7 min read
Common prime factors of two numbers Given two integer A and B , the task is to find the common prime divisors of these numbers.Examples: Input: A = 6, B = 12 Output: 2 3 2 and 3 are the only common prime divisors of 6 and 12Input: A = 4, B = 8 Output: 2 Naive Approach: Iterate from 1 to min(A, B) and check whether i is prime and a fac
7 min read
Maximum no. of contiguous Prime Numbers in an array Given an array arr[] of N elements. The task is to find the maximum number of the contiguous prime numbers in the given array. Examples: Input: arr[] = {3, 5, 2, 66, 7, 11, 8} Output: 3 Maximum contiguous prime number sequence is {2, 3, 5} Input: arr[] = {1, 0, 2, 11, 32, 8, 9} Output: 2 Maximum con
11 min read