📜  当F(N)= f(a)+ f(b)时找到N的值,其中a + b是最小可能值,而a * b = N

📅  最后修改于: 2021-04-28 17:43:56             🧑  作者: Mango

给定整数N ,任务是在以下情况下找到F(N)的值:

  1. F(1)= 0
  2. F(2)= 2
  3. 如果N是奇质数,则F(N)= 0。
  4. F(N)= F(a)+ F(b),其中a和b是N的因数,而(a + b)最小。也是a * b = N

例子:

方法:可以按照以下步骤解决问题:

  • 如果N为1或2,则答案分别为0或2。
  • 在打破递归f(n)= f(a)+ f(b)时,我们得到它是一个被2整除的次数。
  • f(n)的答案是2 *(如果被2整除,则为该数字的倍数)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the value of F(N)
int getValueOfF(int n)
{
  
    // Base cases
    if (n == 1)
        return 0;
    if (n == 2)
        return 1;
  
    int cnt = 0;
  
    // Count the number of times a number
    // if divisible by 2
    while (n % 2 == 0) {
        cnt += 1;
        n /= 2;
    }
  
    // Return the summation
    return 2 * cnt;
}
  
// Driver code
int main()
{
    int n = 20;
    cout << getValueOfF(n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to return the value of F(N)
static int getValueOfF(int n)
{
  
    // Base cases
    if (n == 1)
        return 0;
    if (n == 2)
        return 1;
  
    int cnt = 0;
  
    // Count the number of times a number
    // if divisible by 2
    while (n % 2 == 0)
    {
        cnt += 1;
        n /= 2;
    }
  
    // Return the summation
    return 2 * cnt;
}
  
// Driver code
public static void main (String[] args)
{
    int n = 20;
    System.out.println (getValueOfF(n));
}
}
  
// This code is contributed by ajit.


Python3
# Python3 implementation of the approach
  
# Function to return the value of F(N)
def getValueOfF(n):
  
    # Base cases
    if (n == 1):
        return 0
    if (n == 2):
        return 1
  
    cnt = 0
  
    # Count the number of times a number
    # if divisible by 2
    while (n % 2 == 0):
        cnt += 1
        n /= 2
  
    # Return the summation
    return 2 * cnt
  
# Driver code
n = 20
print(getValueOfF(n))
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
          
// Function to return the value of F(N)
static int getValueOfF(int n)
{
  
    // Base cases
    if (n == 1)
        return 0;
    if (n == 2)
        return 1;
  
    int cnt = 0;
  
    // Count the number of times a number
    // if divisible by 2
    while (n % 2 == 0)
    {
        cnt += 1;
        n /= 2;
    }
  
    // Return the summation
    return 2 * cnt;
}
  
// Driver code
static public void Main ()
{
    int n = 20;
    Console.WriteLine(getValueOfF(n));
}
}
  
// This code is contributed by akt_mit.


PHP


输出:
4