📜  将A转换为B的最小素数运算

📅  最后修改于: 2021-04-29 18:38:04             🧑  作者: Mango

给定两个整数AB ,任务是通过最少的以下操作数将A转换为B

  1. A乘以任何质数
  2. A除以其主要除数之一

打印所需的最少操作数。

例子:

天真的方法:如果A的素因式分解= p 1 q 1 * p 2 q 2 *…* p n q n 。如果将A乘以某个质数,则该质数的q i将增加1 ,如果将A除以其质数之一,则该质数的q i将减少1 。所以对于一个素数p,如果它发生在A的因式分解和B的因式分解q B乘以Q A倍那么我们只需要找到的总和| Q A – q C |为所有素数获取最少的操作数。

高效方法:通过将AB均除以其GCD消除AB的所有共同因素。如果AB没有公因子,那么我们只需要它们的素因子的幂之和即可将A转换为B。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to return the count of
// prime factors of a number
int countFactors(int n)
{
    int factors = 0;
  
    for (int i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            n /= i;
            factors += 1;
        }
    }
  
    if (n != 1)
        factors++;
  
    return factors;
}
  
// Function to return the minimum number of
// given operations required to convert A to B
int minOperations(int A, int B)
{
    int g = __gcd(A, B); // gcd(A, B);
  
    // Eliminate the common
    // factors of A and B
    A /= g;
    B /= g;
  
    // Sum of prime factors
    return countFactors(A) + countFactors(B);
}
  
// Driver code
int main()
{
    int A = 10, B = 15;
  
    cout << minOperations(A, B);
  
    return 0;
}


Java
// Java implementation of above approach
import java .io.*;
  
class GFG
{
      
// Function to return the count of
// prime factors of a number
static int countFactors(int n)
{
    int factors = 0;
  
    for (int i = 2; i * i <= n; i++) 
    {
        while (n % i == 0) 
        {
            n /= i;
            factors += 1;
        }
    }
  
    if (n != 1)
        factors++;
  
        return factors;
}
  
static int __gcd(int a, int b)
{
    if (b == 0)
    return a;
    return __gcd(b, a % b);
}
  
// Function to return the minimum 
// number of given operations
// required to convert A to B
static int minOperations(int A, int B)
{
    int g = __gcd(A, B); // gcd(A, B);
  
    // Eliminate the common
    // factors of A and B
    A /= g;
    B /= g;
  
    // Sum of prime factors
    return countFactors(A) + countFactors(B);
}
  
// Driver code
public static void main(String[] args)
{
    int A = 10, B = 15;
  
    System.out.println(minOperations(A, B));
}
}
  
// This code is contributed
// by Code_Mech


Python3
# Python3 implementation of above approach 
  
# from math lib import sqrt 
# and gcd function
from math import sqrt, gcd
  
# Function to return the count of 
# prime factors of a number 
def countFactors(n) : 
    factors = 0; 
  
    for i in range(2, int(sqrt(n)) + 1) :
        while (n % i == 0) : 
            n //= i 
            factors += 1
  
    if (n != 1) :
        factors += 1
  
    return factors 
  
# Function to return the minimum number of 
# given operations required to convert A to B 
def minOperations(A, B) :
      
    g = gcd(A, B)
  
    # Eliminate the common 
    # factors of A and B 
    A //= g
    B //= g
  
    # Sum of prime factors 
    return countFactors(A) + countFactors(B) 
  
# Driver code 
if __name__ == "__main__" : 
  
    A, B = 10, 15
  
    print(minOperations(A, B))
  
# This code is contributed by Ryuga


C#
// C# implementation of above approach
using System;
      
class GFG
{
      
    // Function to return the count of
    // prime factors of a number
    static int countFactors(int n)
    {
        int factors = 0;
        for (int i = 2; i * i <= n; i++) 
        {
            while (n % i == 0) 
            {
                n /= i;
                factors += 1;
            }
        }
  
        if (n != 1)
            factors++;
  
        return factors;
    }
  
    static int __gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return __gcd(b, a % b);
    }
  
    // Function to return the minimum 
    // number of given operations
    // required to convert A to B
    static int minOperations(int A, int B)
    {
        int g = __gcd(A, B); // gcd(A, B);
  
        // Eliminate the common
        // factors of A and B
        A /= g;
        B /= g;
  
        // Sum of prime factors
        return countFactors(A) + countFactors(B);
    }
  
    // Driver code
    public static void Main()
    {
        int A = 10, B = 15;
        Console.WriteLine(minOperations(A, B));
    }
}
  
// This code is contributed by 
// PrinciRaj1992


PHP
 $b)
        return __gcd($a - $b, $b);
              
    return __gcd($a, $b - $a);
}
  
// Function to return the count of
// prime factors of a number
function countFactors($n)
{
    $factors = 0;
  
    for ($i = 2; $i * $i <= $n; $i++)
    {
        while ($n % $i == 0) 
        {
            $n /= $i;
            $factors += 1;
        }
    }
  
    if ($n != 1)
        $factors++;
  
    return $factors;
}
  
// Function to return the minimum number of
// given operations required to convert A to B
function minOperations($A, $B)
{
    $g = __gcd($A, $B); // gcd(A, B);
  
    // Eliminate the common
    // factors of A and B
    $A /= $g;
    $B /= $g;
  
    // Sum of prime factors
    return countFactors($A) + 
           countFactors($B);
}
  
// Driver code
$A = 10; $B = 15;
  
echo minOperations($A, $B);
  
// This code is contributed 
// by Akanksha Rai
?>


输出:
2