Maximum number of times N can be divided by distinct powers of prime integers
Given an integer N, the task is to calculate the maximum number of times N can be divided by an integer K, where K is a power of a prime integer and the value of K is always distinct.
Example:
Input: N = 24
Output: 3
Explanation: In the first operation, K = 2 (=21). Hence, the value of N becomes N = 24/2 = 12. In the second operation, K = 3. Hence, N = 12/3 = 4. In the 3rd operation, K = 4 (=22). Hence, N = 4/4 = 1, which can not be further divided. Therefore, {2, 3, 4} is the largest set 3 integers having distinct powers of prime integers than can divide N.
Input: N = 100
Output: 2
Approach: The given problem can be solved using a greedy approach. The idea is to divide the number by all of its prime factors in increasing order of their value as well as power. Iterate using a variable i in the range [2, ?N] and if i divides N, then divide N with increasing powers of i (i.e, 1, 2, 3…) until it is divisible with it. Maintain the count of the number of divisions in a variable which is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxDivisions( int N)
{
int cnt = 0;
int range = sqrt (N);
for ( int i = 2; i <= range; i++) {
if (N % i == 0) {
int j = i;
while (N % j == 0 && N) {
N = N / j;
j *= i;
cnt++;
}
while (N % i == 0) {
N /= i;
}
}
}
return cnt;
}
int main()
{
int N = 100;
cout << maxDivisions(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int maxDivisions( int N)
{
int cnt = 0 ;
double range = Math.sqrt(N);
for ( int i = 2 ; i <= range; i++) {
if (N % i == 0 ) {
int j = i;
while (N % j == 0 && N != 0 ) {
N = N / j;
j *= i;
cnt++;
}
while (N % i == 0 ) {
N /= i;
}
}
}
return cnt;
}
public static void main(String[] args)
{
int N = 100 ;
System.out.print(maxDivisions(N));
}
}
|
Python3
from math import ceil, sqrt
def maxDivisions(N):
cnt = 0 ;
_range = ceil(sqrt(N));
for i in range ( 2 , _range + 1 ):
if (N % i = = 0 ):
j = i;
while (N % j = = 0 and N):
N = N / j;
j * = i;
cnt + = 1
while (N % i = = 0 ):
N / = i;
return cnt;
N = 100 ;
print (maxDivisions(N));
|
C#
using System;
class GFG
{
public static int maxDivisions( int N)
{
int cnt = 0;
double range = Math.Sqrt(N);
for ( int i = 2; i <= range; i++) {
if (N % i == 0) {
int j = i;
while (N % j == 0 && N != 0) {
N = N / j;
j *= i;
cnt++;
}
while (N % i == 0) {
N /= i;
}
}
}
return cnt;
}
public static void Main()
{
int N = 100;
Console.Write(maxDivisions(N));
}
}
|
Javascript
<script>
function maxDivisions(N)
{
let cnt = 0;
let range = Math.sqrt(N);
for (let i = 2; i <= range; i++) {
if (N % i == 0) {
let j = i;
while (N % j == 0 && N) {
N = N / j;
j *= i;
cnt++;
}
while (N % i == 0) {
N /= i;
}
}
}
return cnt;
}
let N = 100;
document.write(maxDivisions(N));
</script>
|
Time Complexity: O(?N)
Auxiliary Space: O(1)
Last Updated :
17 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...