Given a positive integer n, find if the largest prime factor of n has an exponent greater than 1 in its prime factorization.
Return true if its exponent is greater than 1; otherwise, return false.
Examples:
Input: n = 36
Output: true
Explanation: The prime factorization of 36 is 2² × 3². The largest prime factor is 3, and its exponent is 2.
Input: n = 13
Output: false
Explanation: The prime factorization of 13 is 13¹. The largest prime factor has exponent 1.
Table of Content
[Naive Approach] Try Every Divisor and Store Prime Factors - O(n) Time and O(1) Space
The idea is to check every number from 2 to n as a possible divisor. For each divisor, repeatedly divide n to count its exponent. Keep updating the exponent of the largest prime factor found and finally return whether it is greater than 1.
#include <iostream>
using namespace std;
bool largePrime(int n)
{
int largestPrime = -1;
int exponent = 0;
// Check every possible divisor
for (int i = 2; i <= n; i++)
{
if (n % i == 0)
{
int cnt = 0;
while (n % i == 0)
{
n /= i;
cnt++;
}
largestPrime = i;
exponent = cnt;
}
}
return exponent > 1;
}
int main()
{
int n = 36;
if (largePrime(n))
cout << "true";
else
cout << "false";
return 0;
}
import java.util.Scanner;
public class GFG {
public static boolean largePrime(int n)
{
int largestPrime = -1;
int exponent = 0;
// Check every possible divisor
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
largestPrime = i;
exponent = cnt;
}
}
return exponent > 1;
}
public static void main(String[] args)
{
int n = 36;
if (largePrime(n))
System.out.println("true");
else
System.out.println("false");
}
}
def largePrime(n):
largestPrime = -1
exponent = 0
# Check every possible divisor
for i in range(2, n + 1):
if n % i == 0:
cnt = 0
while n % i == 0:
n //= i
cnt += 1
largestPrime = i
exponent = cnt
return exponent > 1
if __name__ == '__main__':
n = 36
if largePrime(n):
print('true')
else:
print('false')
using System;
public class GFG {
public static bool largePrime(int n)
{
int largestPrime = -1;
int exponent = 0;
// Check every possible divisor
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
largestPrime = i;
exponent = cnt;
}
}
return exponent > 1;
}
public static void Main()
{
int n = 36;
if (largePrime(n))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function largePrime(n)
{
let largestPrime = -1;
let exponent = 0;
// Check every possible divisor
for (let i = 2; i <= n; i++) {
if (n % i === 0) {
let cnt = 0;
while (n % i === 0) {
n = Math.floor(n / i);
cnt++;
}
largestPrime = i;
exponent = cnt;
}
}
return exponent > 1;
}
// Driver Code
let n = 36;
if (largePrime(n))
console.log("true");
else
console.log("false");
Output
true
[Expected Approach] Prime Factorization using Trial Division - O(√n) Time and O(1) Space
The idea is to factorize n efficiently by removing factor 2 first and then checking only odd divisors up to √n. Track the exponent of the largest prime factor encountered and return whether its exponent is greater than 1.
Let us understand with an example:
Input: n = 36
- Initially, remove all occurrences of the prime factor 2. For 36, dividing by 2 twice gives n = 9, so the exponent of 2 is 2.
- Next, check odd prime factors starting from 3. Dividing 9 by 3 twice gives n = 1, so the exponent of 3 is also 2.
- Since factors are processed in increasing order, the last recorded exponent belongs to the largest prime factor, which is 3.
- No prime factor remains because n = 1. The exponent of the largest prime factor is 2.
- As 2 > 1, the function returns true.
#include <iostream>
using namespace std;
bool largePrime(int n)
{
int res = -1;
int cnt = 0;
// Count the exponent of factor 2
while (n % 2 == 0)
{
cnt++;
n /= 2;
}
if (cnt > 0)
{
res = cnt;
}
// Process all odd prime factors
for (int i = 3; i * i <= n; i += 2)
{
cnt = 0;
while (n % i == 0)
{
cnt++;
n /= i;
}
if (cnt > 0)
{
res = cnt;
}
}
// A remaining prime factor has exponent 1
if (n > 1)
{
res = 1;
}
return res > 1;
}
int main()
{
int n = 36;
if (largePrime(n))
cout << "true";
else
cout << "false";
return 0;
}
import java.util.Scanner;
public class GFG {
public static boolean largePrime(int n)
{
int res = -1;
int cnt = 0;
// Count the exponent of factor 2.
while (n % 2 == 0) {
cnt++;
n /= 2;
}
if (cnt > 0) {
res = cnt;
}
// Process all odd prime factors.
for (int i = 3; i * i <= n; i += 2) {
cnt = 0;
while (n % i == 0) {
cnt++;
n /= i;
}
if (cnt > 0) {
res = cnt;
}
}
// A remaining prime factor has exponent 1.
if (n > 1) {
res = 1;
}
return res > 1;
}
public static void main(String[] args)
{
int n = 36;
if (largePrime(n))
System.out.println("true");
else
System.out.println("false");
}
}
def largePrime(n):
res = -1
cnt = 0
# Count the exponent of factor 2
while n % 2 == 0:
cnt += 1
n //= 2
if cnt > 0:
res = cnt
# Process all odd prime factors
i = 3
while i * i <= n:
cnt = 0
while n % i == 0:
cnt += 1
n //= i
if cnt > 0:
res = cnt
i += 2
# A remaining prime factor has exponent 1
if n > 1:
res = 1
return res > 1
if __name__ == '__main__':
n = 36
if largePrime(n):
print('true')
else:
print('false')
using System;
class GFG {
static bool largePrime(int n)
{
int res = -1;
int cnt = 0;
// Count the exponent of factor 2
while (n % 2 == 0) {
cnt++;
n /= 2;
}
if (cnt > 0) {
res = cnt;
}
// Process all odd prime factors
for (int i = 3; i * i <= n; i += 2) {
cnt = 0;
while (n % i == 0) {
cnt++;
n /= i;
}
if (cnt > 0) {
res = cnt;
}
}
// A remaining prime factor has exponent 1
if (n > 1) {
res = 1;
}
return res > 1;
}
static void Main()
{
int n = 36;
if (largePrime(n))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function largePrime(n)
{
let res = -1;
let cnt = 0;
// Count the exponent of factor 2
while (n % 2 === 0) {
cnt++;
n = Math.floor(n / 2);
}
if (cnt > 0) {
res = cnt;
}
// Process all odd prime factors
for (let i = 3; i * i <= n; i += 2) {
cnt = 0;
while (n % i === 0) {
cnt++;
n = Math.floor(n / i);
}
if (cnt > 0) {
res = cnt;
}
}
// A remaining prime factor has exponent 1
if (n > 1) {
res = 1;
}
return res > 1;
}
// Driver Code
let n = 36;
if (largePrime(n))
console.log("true");
else
console.log("false");
Output
true