GeeksforGeeks App
Open App
Browser
Continue

# Break the number into three parts

Given a really large number, break it into 3 whole numbers such that they sum up to the original number and count the number of ways to do so.

Examples :

```Input : 3
Output : 10
The possible combinations where the sum
of the numbers is equal to 3 are:
0+0+3 = 3
0+3+0 = 3
3+0+0 = 3
0+1+2 = 3
0+2+1 = 3
1+0+2 = 3
1+2+0 = 3
2+0+1 = 3
2+1+0 = 3
1+1+1 = 3

Input : 6
Output : 28```

A total of 10 ways, so answer is 10.

Naive Approach: Try all combinations from 0 to the given number and check if they add up to the given number or not, if they do, increase the count by 1 and continue the process.

## C++

 `// C++ program to count number of ways to break``// a number in three parts.``#include ``#define ll long long int``using` `namespace` `std;` `// Function to count number of ways``// to make the given number n``ll count_of_ways(ll n)``{``    ``ll count = 0;``    ``for` `(``int` `i = 0; i <= n; i++)``        ``for` `(``int` `j = 0; j <= n; j++)``            ``for` `(``int` `k = 0; k <= n; k++)``                ``if` `(i + j + k == n)``                    ``count++;``    ``return` `count;``}` `// Driver Function``int` `main()``{``    ``ll n = 3;``    ``cout << count_of_ways(n) << endl;``    ``return` `0;``}`

## Java

 `// Java program to count number of ways to break``// a number in three parts``import` `java.io.*;` `class` `GFG {``    ``// Function to count number of ways``    ``// to make the given number n``    ``static` `long` `count_of_ways(``long` `n)``    ``{``        ``long` `count = ``0``;``        ``for` `(``int` `i = ``0``; i <= n; i++)``            ``for` `(``int` `j = ``0``; j <= n; j++)``                ``for` `(``int` `k = ``0``; k <= n; k++)``                    ``if` `(i + j + k == n)``                        ``count++;``        ``return` `count;``    ``}` `    ``// driver program``    ``public` `static` `void` `main(String[] args)``    ``{``        ``long` `n = ``3``;``        ``System.out.println(count_of_ways(n));``    ``}``}` `// Contributed by Pramod Kumar`

## Python3

 `# Python3 program to count number of``# ways to break``# a number in three parts.` `# Function to count number of ways``# to make the given number n``def` `count_of_ways(n):` `    ``count ``=` `0``    ``for` `i ``in` `range``(``0``, n``+``1``):``        ``for` `j ``in` `range``(``0``, n``+``1``):``            ``for` `k ``in` `range``(``0``, n``+``1``):``                ``if``(i ``+` `j ``+` `k ``=``=` `n):``                    ``count ``=` `count ``+` `1``    ``return` `count` `# Driver Function``if` `__name__``=``=``'__main__'``:``    ``n ``=` `3``    ``print``(count_of_ways(n))`  `# This code is contributed by``# Sanjit_Prasad`

## C#

 `// C# program to count number of ways``// to break a number in three parts``using` `System;` `class` `GFG {``    ` `    ``// Function to count number of ways``    ``// to make the given number n``    ``static` `long` `count_of_ways(``long` `n)``    ``{``        ``long` `count = 0;``        ``for` `(``int` `i = 0; i <= n; i++)``            ``for` `(``int` `j = 0; j <= n; j++)``                ``for` `(``int` `k = 0; k <= n; k++)``                    ``if` `(i + j + k == n)``                        ``count++;``        ``return` `count;``    ``}` `    ``// driver program``    ``public` `static` `void` `Main()``    ``{``        ``long` `n = 3;``        ``Console.WriteLine(count_of_ways(n));``    ``}``}` `// This code is Contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output

`10`

Time Complexity : O(n3
Auxiliary Space: O(1)

Another Approach:

The idea is to run two nested for loops to choose two numbers between 0 to n. After that, on subtracting both numbers from n, if we get any value greater than or equal to 0, then the third number exists that satisfies the required condition and it is that number. So increment the count.

Steps to implement-

• Initialize a variable count with a value of 0
• After that run two nested loops from 0 to n
• If after subtracting those numbers from n we get any value greater than or equal to 0. Then the third number exists that satisfies the question’s condition and it is that number.
• So in that case increment count by 1
• In the last print/return the count

Code-

## C++

 `// C++ program to count number of ways to break``// a number in three parts.``#include ``#define ll long long int``using` `namespace` `std;` `// Function to count number of ways``// to make the given number n``ll count_of_ways(ll n)``{``    ``ll count = 0;``    ``for` `(``int` `i = 0; i <= n; i++)``        ``for` `(``int` `j = 0; j <= n; j++)``        ``//Third number satisfying the input condition``        ``//will be n-(i+j) if n-(i+j)>=0``            ``if``(n-(i+j)>=0){count++;}``            ` `    ``//Return the number of ways          ``    ``return` `count;``}` `// Driver Function``int` `main()``{``    ``ll n = 3;``    ``cout << count_of_ways(n) << endl;``    ``return` `0;``}`

## Python3

 `# Python program to count number of ways to break``# a number in three parts.``  ` `# Function to count number of ways``# to make the given number n``def` `count_of_ways(n):``    ``count ``=` `0``    ``for` `i ``in` `range``(n``+``1``):``        ``for` `j ``in` `range``(n``+``1``):``            ``# Third number satisfying the input condition``            ``# will be n-(i+j) if n-(i+j)>=0``            ``if` `n``-``(i``+``j)>``=``0``:``                ``count ``+``=` `1``  ` `    ``# Return the number of ways``    ``return` `count``  ` `# Driver Function``if` `__name__ ``=``=` `'__main__'``:``    ``n ``=` `3``    ``print``(count_of_ways(n))`

Output

`10`

Time Complexity : O(n2) ,because of two nested loops
Auxiliary Space: O(1) , because no extra space has been used

Efficient Approach: If we carefully observe the test cases then we realize that the number of ways to break a number n into 3 parts is equal to (n+1) * (n+2) / 2.

## C++

 `// C++ program to count number of ways to break``// a number in three parts.``#include ``#define ll long long int``using` `namespace` `std;` `// Function to count number of ways``// to make the given number n``ll count_of_ways(ll n)``{``    ``long` `long` `int` `mod = 1000000007;``        ``int` `count = ((n + 1) % mod * (n + 2) % mod) / 2;``        ``return` `count;``}` `// Driver Function``int` `main()``{``    ``ll n = 3;``    ``cout << count_of_ways(n) << endl;``    ``return` `0;``}`

## Java

 `// Java program to count number of ways to break``// a number in three parts``import` `java.io.*;` `class` `GFG {``    ``// Function to count number of ways``    ``// to make the given number n``    ``static` `long` `count_of_ways(``long` `n)``    ``{``        ``long` `count = ``0``;``        ``count = (n + ``1``) * (n + ``2``) / ``2``;``        ``return` `count;``    ``}` `    ``// driver program``    ``public` `static` `void` `main(String[] args)``    ``{``        ``long` `n = ``3``;``        ``System.out.println(count_of_ways(n));``    ``}``}` `// Contributed by Pramod Kumar`

## Python3

 `# Python 3 program to count number of``# ways to break a number in three parts.` `# Function to count number of ways``# to make the given number n``def` `count_of_ways(n):``    ``count ``=` `0``    ``count ``=` `(n ``+` `1``) ``*` `(n ``+` `2``) ``/``/` `2``    ``return` `count` `# Driver code``n ``=` `3``print``(count_of_ways(n))` `# This code is contributed by Shrikant13`

## C#

 `// C# program to count number of ways to``// break a number in three parts``using` `System;` `class` `GFG {``    ` `    ``// Function to count number of ways``    ``// to make the given number n``    ``static` `long` `count_of_ways(``long` `n)``    ``{``        ``long` `count = 0;``        ``count = (n + 1) * (n + 2) / 2;``        ``return` `count;``    ``}` `    ``// driver program``    ``public` `static` `void` `Main()``    ``{``        ``long` `n = 3;``        ``Console.WriteLine(count_of_ways(n));``    ``}``}` `// This code is Contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output

`10`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up