Break a number such that sum of maximum divisors of all parts is minimum
Last Updated :
23 Jun, 2022
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;
}
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.
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"
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.
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
?>
JavaScript
<script>
// JavaScript 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)
{
let 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
let n = 27;
document.write(minimumSum(n));
// This code is contributed by Surbhi Tyagi.
</script>
Output :
3
Time Complexity: O(?n)
Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Similar Reads
Sum of all perfect square divisors of numbers from 1 to N Given a number N, the task is to find the sum of all the perfect square divisors of numbers from 1 to N. Examples: Input: N = 5 Output: 9 Explanation: N = 5 Perfect square divisors of 1 = 1. Similarly, perfect square divisors of 2, 3 = 1. Perfect square divisors of 4 = 1, 4. Perfect square divisors
12 min read
Maximum sum of distinct numbers such that LCM of these numbers is N Given a positive number N. The task is to find the maximum sum of distinct numbers such that the LCM of all these numbers is equal to N. Examples: Input : 2 Output : 3 The distinct numbers you can have are just 1 and 2 and their sum is equal to 3. Input : 5 Output : 6Recommended PracticeMaximum Sum
4 min read
Find sum of divisors of all the divisors of a natural number Given a natural number n, the task is to find sum of divisors of all the divisors of n. Examples: Input : n = 54 Output : 232 Divisors of 54 = 1, 2, 3, 6, 9, 18, 27, 54. Sum of divisors of 1, 2, 3, 6, 9, 18, 27, 54 are 1, 3, 4, 12, 13, 39, 40, 120 respectively. Sum of divisors of all the divisors of
7 min read
Sum of all the prime divisors of a number Given a number N. The task is to find the sum of all the prime divisors of N. Examples: Input: 60Output: 102, 3, 5 are prime divisors of 60Input: 39Output: 163, 13 are prime divisors of 39A naive approach will be to iterate for all numbers till N and check if the number divides N. If the number divi
15+ min read
Maximize sum of minimum difference of divisors of nodes in N-ary tree Given a n-ary tree having nodes with a particular weight, our task is to find out the maximum sum of the minimum difference of divisors of each node from root to leaf. Examples: Input: 18 / \ 7 15 / \ \ 4 12 2 / 9 Output: 10 Explanation: The maximum sum is along the path 18 -> 7 -> 12 -> 9
8 min read
Split array into minimum number of subsets with every element of a subset divisible by its minimum Given an array arr[] of size N, the task is to split the array into the minimum number of subsets such that every element belongs to exactly one subset and is divisible by the minimum element present in each subset. Examples: Input: arr[] = {10, 2, 3, 5, 4, 2}Output: 3Explanation:The three possible
7 min read
Querying maximum number of divisors that a number in a given range has Given Q queries, of type: L R, for each query you must print the maximum number of divisors that a number x (L <= x <= R) has. Examples: L = 1 R = 10: 1 has 1 divisor. 2 has 2 divisors. 3 has 2 divisors. 4 has 3 divisors. 5 has 2 divisors. 6 has 4 divisors. 7 has 2 divisors. 8 has 4 divisors.
15+ min read
Sum of all proper divisors of a natural number Given a natural number, calculate sum of all its proper divisors. A proper divisor of a natural number is the divisor that is strictly less than the number. For example, number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.Examples : Input : num = 10
6 min read
Find the number of divisors of all numbers in the range [1, n] Given an integer N. The task is to find the number of divisors of all the numbers in the range [1, N]. Examples: Input: N = 5 Output: 1 2 2 3 2 divisors(1) = 1 divisors(2) = 1 and 2 divisors(3) = 1 and 3 divisors(4) = 1, 2 and 4 divisors(5) = 1 and 5 Input: N = 10 Output: 1 2 2 3 2 4 2 4 3 4 Approac
5 min read
Sum of all proper divisors of natural numbers in an array Given an array of natural numbers count the sum of its proper divisors for every element in array. Example: Input : int arr[] = {8, 13, 24, 36, 59, 75, 87} Output : 7 1 36 55 1 49 21 Number 8 has 3 proper divisors 1, 2, 4 and their sum comes out to be 7.Recommended PracticeSum of Divisors in an arra
9 min read