# Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N

Given an integer **N**, the task is to find the value of **F(N)** if:

- F(1) = 0
- F(2) = 2
- F(N) = 0, if N is odd prime.
- F(N) = F(a) + F(b), where a and b are factors of N and (a + b) is minimum among all factors. Also a * b = N

**Examples:**

Input:N = 5

Output:0

Since 5 is an odd prime.

Input:N = 4

Output:4

4 can be written as 2 * 2, hence f(2) + f(2) = 4

Input:N = 20

Output:4

20 can be written as f(4) + f(5), and f(4) can be written as f(2) + f(2), which is 4.

**Approach**: The following steps can be followed to solve the problem:

- If N is 1 or 2, the answer is 0 or 2 respectively.
- On breaking the recurrence f(n) = f(a) + f(b), we get it is the number of times a number is divisible by 2.
- The answer for f(n) is
**2 * (number of times a number if divisible by 2)**

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//PHP implementation of the approach ` `// Function to return the value of F(N) ` ` ` `function` `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; ` ` ` `echo` `getValueOfF(` `$n` `); ` ` ` `// This code is contributed by Tushil.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Find minimum x such that (x % k) * (x / k) == n
- Find minimum x such that (x % k) * (x / k) == n | Set-2
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Find minimum sum of factors of number
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Find minimum area of rectangle with given set of coordinates
- Find the maximum possible value of the minimum value of modified array
- Find minimum number of coins that make a given value
- Find minimum speed to finish all Jobs
- Find the minimum number of steps to reach M from N
- Find minimum possible digit sum after adding a number d
- Find minimum number of Log value needed to calculate Log upto N
- Find the minimum number of rectangles left after inserting one into another
- Find an index such that difference between product of elements before and after it is minimum
- Find the K-th minimum element from an array concatenated M times

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.