📜  将 n 拆分为最大合数

📅  最后修改于: 2021-10-26 05:22:12             🧑  作者: Mango

给定 n,打印总和为 n 的最大合数。前几个合数是 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, ………
例子:

Input: 90   
Output: 22
Explanation: If we add 21 4's, then we 
get 84 and then add 6 to it, we get 90.

Input: 10
Output: 2
Explanation: 4 + 6 = 10

以下是一些重要的观察结果。

  1. 如果数字小于 4,则不会有任何组合。
  2. 如果数字是 5、7、11,则不会有任何分裂。
  3. 由于最小合数是 4,因此使用最大数量的 4 是有意义的。
  4. 对于除以 4 时不会留下复合余数的数字,我们执行以下操作。如果余数是1,我们减去9得到可以被4完全整除的数。如果余数是2,那么从中减去6得到一个可以被4完全整除的数。如果余数是3,那么从中减去 15 使 n 完全可以被 4 整除,而 15 可以由 9 + 6 组成。

所以主要的观察是使 n 由最大数量的 4 组成,其余的可以由 6 和 9 组成。我们不需要更多的合数,因为可以组成 9 以上的合数4、6 和 9。
下面是上述方法的实现

C++
// CPP program to split a number into maximum
// number of composite numbers.
#include 
using namespace std;
 
// function to calculate the maximum number of
// composite numbers adding upto n
int count(int n)
{
    // 4 is the smallest composite number
    if (n < 4)
        return -1;
 
    // stores the remainder when n is divided
    // by 4
    int rem = n % 4;
 
    // if remainder is 0, then it is perfectly
    // divisible by 4.
    if (rem == 0)
        return n / 4;
 
    // if the remainder is 1
    if (rem == 1) {
 
        // If the number is less then 9, that
        // is 5, then it cannot be expressed as
        // 4 is the only composite number less
        // than 5
        if (n < 9)
            return -1;
 
        // If the number is greater then 8, and
        // has a remainder of 1, then express n
        // as n-9 a and it is perfectly divisible
        // by 4 and for 9, count 1.
        return (n - 9) / 4 + 1;
    }
 
     
    // When remainder is 2, just subtract 6 from n,
    // so that n is perfectly divisible by 4 and
    // count 1 for 6 which is subtracted.
    if (rem == 2)
        return (n - 6) / 4 + 1;
 
 
    // if the number is 7, 11 which cannot be
    // expressed as sum of any composite numbers
    if (rem == 3) {
        if (n < 15)
            return -1;
 
        // when the remainder is 3, then subtract
        // 15 from it and n becomes perfectly
        // divisible by 4 and we add 2 for 9 and 6,
        // which is getting subtracted to make n
        // perfectly divisible by 4.
        return (n - 15) / 4 + 2;
    }
}
 
// driver program to test the above function
int main()
{
    int n = 90;
    cout << count(n) << endl;
 
    n = 143;
    cout << count(n) << endl;
    return 0;
}


Java
// Java program to split a number into maximum
// number of composite numbers.
import java.io.*;
 
class GFG
{
    // function to calculate the maximum number of
    // composite numbers adding upto n
    static int count(int n)
    {
        // 4 is the smallest composite number
        if (n < 4)
            return -1;
     
        // stores the remainder when n is divided
        // by 4
        int rem = n % 4;
     
        // if remainder is 0, then it is perfectly
        // divisible by 4.
        if (rem == 0)
            return n / 4;
     
        // if the remainder is 1
        if (rem == 1) {
     
            // If the number is less then 9, that
            // is 5, then it cannot be expressed as
            // 4 is the only composite number less
            // than 5
            if (n < 9)
                return -1;
     
            // If the number is greater then 8, and
            // has a remainder of 1, then express n
            // as n-9 a and it is perfectly divisible
            // by 4 and for 9, count 1.
            return (n - 9) / 4 + 1;
        }
     
         
        // When remainder is 2, just subtract 6 from n,
        // so that n is perfectly divisible by 4 and
        // count 1 for 6 which is subtracted.
        if (rem == 2)
            return (n - 6) / 4 + 1;
     
     
        // if the number is 7, 11 which cannot be
        // expressed as sum of any composite numbers
        if (rem == 3)
        {
            if (n < 15)
                return -1;
     
            // when the remainder is 3, then subtract
            // 15 from it and n becomes perfectly
            // divisible by 4 and we add 2 for 9 and 6,
            // which is getting subtracted to make n
            // perfectly divisible by 4.
            return (n - 15) / 4 + 2;
        }
        return 0;
    }
     
    // Driver program
    public static void main (String[] args)
    {
        int n = 90;
        System.out.println(count(n));
 
        n = 143;
        System.out.println(count(n));
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 program to split a number into
# maximum number of composite numbers.
 
# Function to calculate the maximum number
# of composite numbers adding upto n
def count(n):
 
    # 4 is the smallest composite number
    if (n < 4):
        return -1
 
    # stores the remainder when n 
    # is divided n is divided by 4
    rem = n % 4
 
    # if remainder is 0, then it is 
    # perfectly divisible by 4.
    if (rem == 0):
        return n // 4
 
    # if the remainder is 1
    if (rem == 1):
 
        # If the number is less then 9, that
        # is 5, then it cannot be expressed as
        # 4 is the only composite number less
        # than 5
        if (n < 9):
            return -1
 
        # If the number is greater then 8, and
        # has a remainder of 1, then express n
        # as n-9 a and it is perfectly divisible
        # by 4 and for 9, count 1.
        return (n - 9) // 4 + 1
     
 
     
    # When remainder is 2, just subtract 6 from n,
    # so that n is perfectly divisible by 4 and
    # count 1 for 6 which is subtracted.
    if (rem == 2):
        return (n - 6) // 4 + 1
 
 
    # if the number is 7, 11 which cannot be
    # expressed as sum of any composite numbers
    if (rem == 3):
        if (n < 15):
            return -1
 
        # when the remainder is 3, then subtract
        # 15 from it and n becomes perfectly
        # divisible by 4 and we add 2 for 9 and 6,
        # which is getting subtracted to make n
        # perfectly divisible by 4.
        return (n - 15) // 4 + 2
 
# Driver Code
n = 90
print(count(n))
 
n = 143
print(count(n))
 
# This code is contributed by Anant Agarwal.


C#
// C# program to split a number into maximum
// number of composite numbers.
using System;
 
class GFG {
     
    // function to calculate the maximum number
    // of composite numbers adding upto n
    static int count(int n)
    {
         
        // 4 is the smallest composite number
        if (n < 4)
            return -1;
      
        // stores the remainder when n is divided
        // by 4
        int rem = n % 4;
      
        // if remainder is 0, then it is perfectly
        // divisible by 4.
        if (rem == 0)
            return n / 4;
      
        // if the remainder is 1
        if (rem == 1) {
      
            // If the number is less then 9, that
            // is 5, then it cannot be expressed as
            // 4 is the only composite number less
            // than 5
            if (n < 9)
                return -1;
      
            // If the number is greater then 8, and
            // has a remainder of 1, then express n
            // as n-9 a and it is perfectly divisible
            // by 4 and for 9, count 1.
            return (n - 9) / 4 + 1;
        }
      
          
        // When remainder is 2, just subtract 6 from n,
        // so that n is perfectly divisible by 4 and
        // count 1 for 6 which is subtracted.
        if (rem == 2)
            return (n - 6) / 4 + 1;
      
      
        // if the number is 7, 11 which cannot be
        // expressed as sum of any composite numbers
        if (rem == 3)
        {
            if (n < 15)
                return -1;
      
            // when the remainder is 3, then subtract
            // 15 from it and n becomes perfectly
            // divisible by 4 and we add 2 for 9 and 6,
            // which is getting subtracted to make n
            // perfectly divisible by 4.
            return (n - 15) / 4 + 2;
        }
         
        return 0;
    }
      
    // Driver program
    public static void Main()
    {
        int n = 90;
        Console.WriteLine(count(n));
  
        n = 143;
        Console.WriteLine(count(n));
    }
}
  
// This code is contributed by Anant Agarwal.


PHP


Javascript


输出:

22 
34 

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程