Given a really large number, break it into 3 whole numbers such that they sum up to the original number and count 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 upto the given number or not, if they do, increase the count by 1 and continue the process.

## C/C++

`// C++ program to count number of ways to break ` `// a number in three parts. ` `#include <bits/stdc++.h> ` `#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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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 ` `function` `count_of_ways( ` `$n` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `for` `(` `$j` `= 0; ` `$j` `<= ` `$n` `; ` `$j` `++) ` ` ` `for` `(` `$k` `= 0; ` `$k` `<= ` `$n` `; ` `$k` `++) ` ` ` `if` `(` `$i` `+ ` `$j` `+ ` `$k` `== ` `$n` `) ` ` ` `$count` `++; ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver Code ` `$n` `= 3; ` `echo` `count_of_ways(` `$n` `); ` ` ` `// This code is Contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

10

**Time Complexity :** O(n^{3})

**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++

`// C++ program to count number of ways to break ` `// a number in three parts. ` `#include <bits/stdc++.h> ` `#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; ` ` ` `count = (n + 1) * (n + 2) / 2; ` ` ` `return` `count; ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` `ll n = 3; ` ` ` `cout << count_of_ways(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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 ` `function` `count_of_ways( ` `$n` `) ` `{ ` ` ` `$count` `; ` ` ` `$count` `= (` `$n` `+ 1) * (` `$n` `+ 2) / 2; ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver Code ` `$n` `= 3; ` `echo` `count_of_ways(` `$n` `); ` ` ` `// This code is Contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

10

**Time Complexity:** O(1)

This article is contributed by **Aditya Gupta**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Break a number such that sum of maximum divisors of all parts is minimum
- Split a number into 3 parts such that none of the parts is divisible by 3
- Count number of ways to divide a number in 4 parts
- Divide a big number into two parts that differ by k
- Partition a number into two divisble parts
- Divide number into two parts divisible by given numbers
- Possible cuts of a number such that maximum parts are divisible by 3
- Divide a number into two parts such that sum of digits is maximum
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Program to find the Break Even Point
- Partiton N into M parts such that difference between Max and Min part is smallest
- Divide an isosceles triangle in two parts with ratio of areas as n:m
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Minimum cuts required to divide the Circle into equal parts
- Count number of triplets with product equal to given number with duplicates allowed