# Break a number such that sum of maximum divisors of all parts is minimum

We need to split a number n such that sum of maximum divisors of all the parts is minimum.

**Examples :**

Input: n = 27 Output: Minimum sum of maximum divisors of parts = 3 Explanation : We can split 27 as follows: 27 = 13 + 11 + 3, Maximum divisor of 13 = 1, 11 = 1, 3 = 1. Answer = 3 (1 + 1 + 1). Input : n = 4 Output: Minimum sum of maximum divisors of parts = 2 Explanation : We can write 4 = 2 + 2 Maximum divisor of 2 = 1, So answer is 1 + 1 = 2.

We need to minimize maximum divisors. It is obvious that if N is prime, maximum divisor = 1. If the number is not a prime, then the number should be atleast 2.

According to Goldbach’s Conjecture, every even integer can be expressed as sum of two prime numbers. For our problem there will be two cases:

1) When the number is even, it can be expressed as the sum of two prime numbers and our answer will be 2 because maximum divisor of a prime number is 1.

2) When the number is odd, it can also be written as sum of prime numbers, **n = 2 + (n-2);** if (n-2) is a prime number(answer = 2), otherwise. Refer odd number as sum of primes for details.

**n = 3 + (n-3);** (n-3) is an even number and it is sum of two primes(answer = 3).

Below is the implementation of this approach.

## C++

`// CPP program to break a number such ` `// that sum of maximum divisors of all ` `// parts is minimum ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to check if a number is ` `// prime or not. ` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `int` `i = 2; ` ` ` `while` `(i * i <= n) { ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` `i++; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `int` `minimumSum(` `int` `n) ` `{ ` ` ` `if` `(isPrime(n)) ` ` ` `return` `1; ` ` ` ` ` `// If n is an even number (we can ` ` ` `// write it as sum of two primes) ` ` ` `if` `(n % 2 == 0) ` ` ` `return` `2; ` ` ` ` ` `// If n is odd and n-2 is prime. ` ` ` `if` `(isPrime(n - 2)) ` ` ` `return` `2; ` ` ` ` ` `// If n is odd, n-3 must be even. ` ` ` `return` `3; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 27; ` ` ` `cout << minimumSum(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA Code for Break a number such that sum ` `// of maximum divisors of all parts is minimum ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check if a number is ` ` ` `// prime or not. ` ` ` `static` `boolean` `isPrime(` `int` `n) ` ` ` `{ ` ` ` `int` `i = ` `2` `; ` ` ` ` ` `while` `(i * i <= n) { ` ` ` `if` `(n % i == ` `0` `) ` ` ` `return` `false` `; ` ` ` `i++; ` ` ` `} ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `static` `int` `minimumSum(` `int` `n) ` ` ` `{ ` ` ` `if` `(isPrime(n)) ` ` ` `return` `1` `; ` ` ` ` ` `// If n is an even number (we can ` ` ` `// write it as sum of two primes) ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `return` `2` `; ` ` ` ` ` `// If n is odd and n-2 is prime. ` ` ` `if` `(isPrime(n - ` `2` `)) ` ` ` `return` `2` `; ` ` ` ` ` `// If n is odd, n-3 must be even. ` ` ` `return` `3` `; ` ` ` `} ` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.println(minimumSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnav Kr. Mandal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to break a number ` `# such that sum of maximum divisors ` `# of all parts is minimum ` ` ` `# Function to check if a number is ` `# prime or not. ` `def` `isPrime( n): ` ` ` `i ` `=` `2` ` ` ` ` `while` `(i ` `*` `i <` `=` `n): ` ` ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` `return` `False` ` ` `i` `+` `=` `1` ` ` ` ` `return` `True` ` ` `def` `minimumSum( n): ` ` ` ` ` `if` `(isPrime(n)): ` ` ` `return` `1` ` ` ` ` `# If n is an even number (we can ` ` ` `# write it as sum of two primes) ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `return` `2` ` ` ` ` `# If n is odd and n-2 is prime. ` ` ` `if` `(isPrime(n ` `-` `2` `)): ` ` ` `return` `2` ` ` ` ` `# If n is odd, n-3 must be even. ` ` ` `return` `3` ` ` `# Driver code ` `n ` `=` `27` `print` `(minimumSum(n)) ` ` ` `# This code is contributed by "Abhishek Sharma 44" ` |

*chevron_right*

*filter_none*

## C#

`// C# Code for Break a number ` `// such that sum of maximum ` `// divisors of all parts is minimum ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check if a number is ` ` ` `// prime or not. ` ` ` `static` `bool` `isPrime(` `int` `n) ` ` ` `{ ` ` ` `int` `i = 2; ` ` ` ` ` `while` `(i * i <= n) { ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` `i++; ` ` ` `} ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `static` `int` `minimumSum(` `int` `n) ` ` ` `{ ` ` ` `if` `(isPrime(n)) ` ` ` `return` `1; ` ` ` ` ` `// If n is an even number (we can ` ` ` `// write it as sum of two primes) ` ` ` `if` `(n % 2 == 0) ` ` ` `return` `2; ` ` ` ` ` `// If n is odd and n-2 is prime. ` ` ` `if` `(isPrime(n - 2)) ` ` ` `return` `2; ` ` ` ` ` `// If n is odd, n-3 must be even. ` ` ` `return` `3; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 27; ` ` ` `Console.WriteLine(minimumSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to break a number ` `// such that sum of maximum ` `// divisors of all parts is minimum ` ` ` `// Function to check if a ` `// number is prime or not. ` `function` `isPrime(` `$n` `) ` `{ ` ` ` `$i` `= 2; ` ` ` `while` `(` `$i` `* ` `$i` `<= ` `$n` `) ` ` ` `{ ` ` ` `if` `(` `$n` `% ` `$i` `== 0) ` ` ` `return` `false; ` ` ` `$i` `++; ` ` ` `} ` ` ` `return` `true; ` `} ` ` ` `function` `minimumSum(` `$n` `) ` `{ ` ` ` `if` `(isPrime(` `$n` `)) ` ` ` `return` `1; ` ` ` ` ` `// If n is an even ` ` ` `// number (we can ` ` ` `// write it as sum ` ` ` `// of two primes) ` ` ` `if` `(` `$n` `% 2 == 0) ` ` ` `return` `2; ` ` ` ` ` `// If n is odd and ` ` ` `// n - 2 is prime. ` ` ` `if` `(isPrime(` `$n` `- 2)) ` ` ` `return` `2; ` ` ` ` ` `// If n is odd, ` ` ` `// n - 3 must be even. ` ` ` `return` `3; ` `} ` ` ` `// Driver code ` `$n` `= 27; ` `echo` `minimumSum(` `$n` `); ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

3

## Recommended Posts:

- Break the number into three parts
- Sum of all second largest divisors after splitting a number into one or more parts
- Divide a number into two parts such that sum of digits is maximum
- Possible cuts of a number such that maximum parts are divisible by 3
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Querying maximum number of divisors that a number in a given range has
- Minimum number of Square Free Divisors
- Count elements in the given range which have maximum number of divisors
- Split a number into 3 parts such that none of the parts is divisible by 3
- Find sum of divisors of all the divisors of a natural number
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Number of subarrays whose minimum and maximum are same
- Minimum cuts required to divide the Circle into equal parts
- Minimum number of elements to be removed to make XOR maximum

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.