Find the largest good number in the divisors of given number N
Given a number N. The task is to find the largest good number among the divisors of a given number N. A number X is defined as the good number if there is no such positive integer a > 1, such that a^2 is a divisor of X.
Examples:
Input: N = 10
Output: 10
In 1, 2, 5, 10.
10 is the largest good number
Input: N = 12
Output: 6
In 1, 2, 3, 4, 6, 12.
6 is the largest good number
Approach: Find all prime divisors of N. Assume they are p1, p2, …, pk (in O(sqrt(n)) time complexity). If the answer is a, then we know that for each 1 <= I <= k, obviously, a is not divisible by pi^2 (and all greater powers of pi). So a <= p1 × p2 ×… × pk. And we know that p1 × p2 × … × pk is itself a good number. So, the answer is p1 × p2 ×…× pk.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > PrimeFactors( int n)
{
vector< int > v;
int x = n;
for ( int i = 2; i * i <= n; i++) {
if (x % i == 0) {
v.push_back(i);
while (x % i == 0)
x /= i;
}
}
if (x > 1)
v.push_back(x);
return v;
}
int GoodNumber( int n)
{
vector< int > v = PrimeFactors(n);
int ans = 1;
for ( int i = 0; i < v.size(); i++)
ans *= v[i];
return ans;
}
int main()
{
int n = 12;
cout << GoodNumber(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static Vector<Integer> PrimeFactors( int n)
{
Vector<Integer> v = new Vector<Integer>();
int x = n;
for ( int i = 2 ; i * i <= n; i++) {
if (x % i == 0 ) {
v.add(i);
while (x % i == 0 )
x /= i;
}
}
if (x > 1 )
v.add(x);
return v;
}
static int GoodNumber( int n)
{
Vector<Integer> v = new Vector<Integer>(PrimeFactors(n));
int ans = 1 ;
for ( int i = 0 ; i < v.size(); i++)
ans *= v.get(i);
return ans;
}
public static void main(String[] args)
{
int n = 12 ;
System.out.println(GoodNumber(n));
}
}
|
Python 3
def PrimeFactors(n):
v = []
x = n
i = 2
while (i * i < = n):
if (x % i = = 0 ) :
v.append(i)
while (x % i = = 0 ):
x / / = i
i + = 1
if (x > 1 ):
v.append(x)
return v
def GoodNumber(n):
v = PrimeFactors(n)
ans = 1
for i in range ( len (v)):
ans * = v[i]
return ans
if __name__ = = "__main__" :
n = 12
print (GoodNumber(n))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static List< int > PrimeFactors( int n)
{
List< int > v = new List< int >();
int x = n;
for ( int i = 2; i * i <= n; i++) {
if (x % i == 0) {
v.Add(i);
while (x % i == 0)
x /= i;
}
}
if (x > 1)
v.Add(x);
return v;
}
static int GoodNumber( int n)
{
List< int > v = new List< int >(PrimeFactors(n));
int ans = 1;
for ( int i = 0; i < v.Count; i++)
ans *= v[i];
return ans;
}
public static void Main(String[] args)
{
int n = 12;
Console.WriteLine(GoodNumber(n));
}
}
|
PHP
<?php
function PrimeFactors( $n )
{
$v = array ();
$x = $n ;
for ( $i = 2; $i * $i <= $n ; $i ++)
{
if ( $x % $i == 0)
{
array_push ( $v , $i );
while ( $x % $i == 0)
$x /= $i ;
}
}
if ( $x > 1)
array_push ( $v , $x );
return $v ;
}
function GoodNumber( $n )
{
$v = PrimeFactors( $n );
$ans = 1;
for ( $i = 0; $i < count ( $v ); $i ++)
$ans *= $v [ $i ];
return $ans ;
}
$n = 12;
echo GoodNumber( $n );
?>
|
Javascript
<script>
function PrimeFactors(n)
{
let v = [];
let x = n;
for (let i = 2; i * i <= n; i++) {
if (x % i == 0) {
v.push(i);
while (x % i == 0)
x = Math.floor(x/i);
}
}
if (x > 1)
v.push(x);
return v;
}
function GoodNumber(n)
{
let v = PrimeFactors(n);
let ans = 1;
for (let i = 0; i < v.length; i++)
ans *= v[i];
return ans;
}
let n = 12;
document.write(GoodNumber(n));
</script>
|
Time Complexity : O(sqrt(n) + k) ,where n is given number and k is number of distinct prime factors.
Space Complexity : O(k) ,to store distinct prime factors.
Last Updated :
23 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...