Minimum operations to reduce N to a prime number by subtracting with its highest divisor
Given a positive integer N. In one operation subtract N with its highest divisor other than N and 1. The task is to find minimum operations required to reduce N exactly to a prime number.
Examples:
Input: N = 38
Output: 1
Explanation: Highest divisor of 38 is 19, so subtract it (38 – 19) = 19. 19 is a prime number.
So, number of operations required = 1.
Input: N = 69
Output: 2
Approach: This problem can be solved by using simple concepts of maths. Follow the steps below to solve the given problem.
- At first, check whether N is already prime or not.
- If N is already a prime, return 0.
- Else, Initialize a variable say count = 0 to store the number of operations required.
- Initialize a variable say i=2 and run a while loop till N!=i
- Run while loop and on each iteration subtract current value of N with its largest divisor.
- Compute the steps and increment the count by 1.
- Return the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i <= sqrt (n); i++)
if (n % i == 0)
return false ;
return true ;
}
int minOperation( int N)
{
if (N == 1)
return -1;
if (isPrime(N) == true ) {
return 0;
}
else {
int count = 0;
int i = 2;
while (N != i) {
while (N % i == 0) {
int temp = N;
N -= (temp / i);
count++;
if (isPrime(N))
return count;
}
i++;
}
return count;
}
}
int main()
{
int N = 38;
cout << minOperation(N);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static boolean isPrime( int n) {
if (n <= 1 )
return false ;
for ( int i = 2 ; i <= Math.sqrt(n); i++)
if (n % i == 0 )
return false ;
return true ;
}
public static int minOperation( int N) {
if (N == 1 )
return - 1 ;
if (isPrime(N) == true ) {
return 0 ;
}
else {
int count = 0 ;
int i = 2 ;
while (N != i) {
while (N % i == 0 ) {
int temp = N;
N -= (temp / i);
count++;
if (isPrime(N))
return count;
}
i++;
}
return count;
}
}
public static void main(String args[]) {
int N = 38 ;
System.out.println(minOperation(N));
}
}
|
Python3
import math
def isPrime(n):
if (n < = 1 ):
return False
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
if (n % i = = 0 ):
return False
return True
def minOperation(N):
if (N = = 1 ):
return - 1
if (isPrime(N) = = True ):
return 0
else :
count = 0
i = 2
while (N ! = i):
while (N % i = = 0 ):
temp = N
N - = (temp / / i)
count + = 1
if (isPrime(N)):
return count
i + = 1
return count
if __name__ = = "__main__" :
N = 38
print (minOperation(N))
|
C#
using System;
public class GFG {
public static bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i <= Math.Sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
public static int minOperation( int N) {
if (N == 1)
return -1;
if (isPrime(N) == true ) {
return 0;
}
else {
int count = 0;
int i = 2;
while (N != i) {
while (N % i == 0) {
int temp = N;
N -= (temp / i);
count++;
if (isPrime(N))
return count;
}
i++;
}
return count;
}
}
public static void Main( string []args) {
int N = 38;
Console.WriteLine(minOperation(N));
}
}
|
Javascript
<script>
const isPrime = (n) => {
if (n <= 1)
return false ;
for (let i = 2; i <= parseInt(Math.sqrt(n)); i++)
if (n % i == 0)
return false ;
return true ;
}
const minOperation = (N) => {
if (N == 1)
return -1;
if (isPrime(N) == true ) {
return 0;
}
else
{
let count = 0;
let i = 2;
while (N != i)
{
while (N % i == 0)
{
let temp = N;
N -= parseInt(temp / i);
count++;
if (isPrime(N))
return count;
}
i++;
}
return count;
}
}
let N = 38;
document.write(minOperation(N));
</script>
|
Time Complexity: O(sqrtN)
Auxiliary Space: O(1)
Last Updated :
26 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...