
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Find the maximum number of composite summands of a number in Python
Suppose we have a given number N that in range (1<=N<=10^9), we have to represent N as a sum of the largest possible number of composite summands and return this largest number, otherwise when we cannot find any split, then return -1.
So, if the input is like 16, then the output will be 4 as 16 can be written as 4 + 4 + 4 + 4 or 8 + 8, but (4 + 4 + 4 + 4) has maximum summands.
To solve this, we will follow these steps −
max_val := 16
Define a function pre_calc() . This will take
table := a list of size max_val, then store -1 at each position
table[0] := 0
v := [4, 6, 9]
for i in range 1 to max_val, increase by 1, do
for k in range 0 to 2, do
j := v[k]
if i >= j and table[i - j] is not -1, then
table[i] := maximum of table[i], table[i - j] + 1
return table
Define a function max_summ() . This will take table, n
if n < max_val, then
return table[n]
otherwise,
t := integer of ((n - max_val) / 4) + 1
return t + table[n - 4 * t]
From the main method, do the following −
table := pre_calc()
display max_summ(table, n)
Example
Let us see the following implementation to get better understanding −
global max_val max_val = 16 def pre_calc(): table = [-1 for i in range(max_val)] table[0] = 0 v = [4, 6, 9] for i in range(1, max_val, 1): for k in range(3): j = v[k] if (i >= j and table[i - j] != -1): table[i] = max(table[i], table[i - j] + 1) return table def max_summ(table, n): if (n < max_val): return table[n] else: t = int((n - max_val) / 4)+ 1 return t + table[n - 4 * t] n = 16 table = pre_calc() print(max_summ(table, n))
Input
16
Output
4
- Related Articles
- A die is thrown. Find the probability of getting a composite number.
- What are composite numbers? Can a composite number be odd? If yes, write the smallest odd composite number.
- Program to find maximum number of eaten apples in Python
- Program to find maximum number of balls in a box using Python
- Program to find number of columns flips to get maximum number of equal Rows in Python?
- Program to find maximum number of balanced groups of parentheses in Python
- Find row number of a binary matrix having maximum number of 1s in C++
- Program to find maximum number of non-overlapping substrings in Python
- Program to find maximum difference of any number and its next smaller number in Python
- What is the HCF of smallest prime number and the smallest composite number?
- Find the Number of Maximum Product Quadruples in C++
- Program to find maximum number of coins we can collect in Python
- Program to find maximum number of groups getting fresh donuts in Python
- Which is the smallest composite number?
- Which factors are not included in the prime factorisation of a composite number?
