Skip to main content

Split a number into three parts

The given problem is to split a given integer number into three parts such that their sum is equal to the original number. The number can be positive or negative, and we need to find all possible combinations of three integers that add up to the given number.

Problem Statement

Given an integer number, we want to find all possible combinations of three integers that sum up to the given number. The three integers can be positive or negative.

Example

Number : 5
0  0  5
0  1  4
0  2  3
0  3  2
0  4  1
0  5  0
1  0  4
1  1  3
1  2  2
1  3  1
1  4  0
2  0  3
2  1  2
2  2  1
2  3  0
3  0  2
3  1  1
3  2  0
4  0  1
4  1  0
5  0  0

Idea to Solve the Problem

To solve this problem, we can use three nested loops to generate all possible combinations of three integers between the range of the input number and zero. We need to iterate through all possible values of the three integers and check if their sum equals the given number. If it matches, we print the combination.

Pseudocode

Here's the pseudocode to split the number into three parts:


1. Function break_number(number)
2.     start = 0, ends = number
3.     if number < 0
4.         start = number
5.         ends = 0
6.     end if
7.     Display "Number : number"
8.     for i from start to ends
9.         for j from start to ends
10.            for k from start to ends
11.                if number == (i + j + k)
12.                    Display "i  j  k"
13.                end if
14.            end for
15.        end for
16.    end for
17. End Function
18. Main
19.    Call break_number(7)
20.    Call break_number(5)
21.    Call break_number(-5)
22.    Call break_number(2)
23. End Main

Algorithm Explanation

  1. The break_number function initializes the start and ends variables to control the execution of the loop. If the input number is negative, the start is set to the input number, and ends is set to zero.
  2. We then display the input number using the printf function.
  3. The three nested loops iterate over all possible combinations of three integers between start and ends.
  4. For each combination, we check if their sum is equal to the input number. If it matches, we display the combination using the printf function.

Code Solution

/*
  C Program 
  Break the number into three parts
*/
#include <stdio.h>

//Break a number in three parts that sum equal to given value
void break_number(int number)
{
    //Variables which is controlling the execution process of loop
    int start = 0, ends = number;
    if (number < 0)
    {
        //Special case
        //When number is less than zero
        start = number;
        ends = 0;
    }
    printf("\nNumber : %d\n\n", number);
    //This loop indicates first number
    for (int i = start; i <= ends; ++i)
    {
        //This loop indicates second number
        for (int j = start; j <= ends; ++j)
        {
            //This loop indicates third number
            for (int k = start; k <= ends; ++k)
            {
                //Check whether sum of three numbers is equal to given number
                if (number == (i + j + k))
                {
                    //Display the resultant numbers
                    printf("%d  %d  %d\n", i, j, k);
                }
            }
        }
    }
}
int main()
{
    //Simple Case
    break_number(7);
    break_number(5);
    break_number(-5);
    break_number(2);
    return 0;
}

Output

Number : 7

0  0  7
0  1  6
0  2  5
0  3  4
0  4  3
0  5  2
0  6  1
0  7  0
1  0  6
1  1  5
1  2  4
1  3  3
1  4  2
1  5  1
1  6  0
2  0  5
2  1  4
2  2  3
2  3  2
2  4  1
2  5  0
3  0  4
3  1  3
3  2  2
3  3  1
3  4  0
4  0  3
4  1  2
4  2  1
4  3  0
5  0  2
5  1  1
5  2  0
6  0  1
6  1  0
7  0  0

Number : 5

0  0  5
0  1  4
0  2  3
0  3  2
0  4  1
0  5  0
1  0  4
1  1  3
1  2  2
1  3  1
1  4  0
2  0  3
2  1  2
2  2  1
2  3  0
3  0  2
3  1  1
3  2  0
4  0  1
4  1  0
5  0  0

Number : -5

-5  0  0
-4  -1  0
-4  0  -1
-3  -2  0
-3  -1  -1
-3  0  -2
-2  -3  0
-2  -2  -1
-2  -1  -2
-2  0  -3
-1  -4  0
-1  -3  -1
-1  -2  -2
-1  -1  -3
-1  0  -4
0  -5  0
0  -4  -1
0  -3  -2
0  -2  -3
0  -1  -4
0  0  -5

Number : 2

0  0  2
0  1  1
0  2  0
1  0  1
1  1  0
2  0  0
// Java Program
// Break the number into three parts
class MyMath
{
    //Break a number in three parts that sum equal to given value
    public void break_number(int number)
    {
        //Variables which is controlling the execution process of loop
        int start = 0, ends = number;
        if (number < 0)
        {
            //Special case
            //When number is less than zero
            start = number;
            ends = 0;
        }
        System.out.print("\nNumber : " + number + "\n");
        //This loop indicates first number
        for (int i = start; i <= ends; ++i)
        {
            //This loop indicates second number
            for (int j = start; j <= ends; ++j)
            {
                //This loop indicates third number
                for (int k = start; k <= ends; ++k)
                {
                    //Check whether sum of three numbers is equal to given number
                    if (number == (i + j + k))
                    {
                        //Display the resultant numbers
                        System.out.print(i + " " + j + " " + k + "\n");
                    }
                }
            }
        }
    }
    public static void main(String[] args)
    {
        MyMath obj = new MyMath();
        //Simple Case
        obj.break_number(7);
        obj.break_number(5);
        obj.break_number(-5);
        obj.break_number(2);
    }
}

Output

Number : 7
0 0 7
0 1 6
0 2 5
0 3 4
0 4 3
0 5 2
0 6 1
0 7 0
1 0 6
1 1 5
1 2 4
1 3 3
1 4 2
1 5 1
1 6 0
2 0 5
2 1 4
2 2 3
2 3 2
2 4 1
2 5 0
3 0 4
3 1 3
3 2 2
3 3 1
3 4 0
4 0 3
4 1 2
4 2 1
4 3 0
5 0 2
5 1 1
5 2 0
6 0 1
6 1 0
7 0 0

Number : 5
0 0 5
0 1 4
0 2 3
0 3 2
0 4 1
0 5 0
1 0 4
1 1 3
1 2 2
1 3 1
1 4 0
2 0 3
2 1 2
2 2 1
2 3 0
3 0 2
3 1 1
3 2 0
4 0 1
4 1 0
5 0 0

Number : -5
-5 0 0
-4 -1 0
-4 0 -1
-3 -2 0
-3 -1 -1
-3 0 -2
-2 -3 0
-2 -2 -1
-2 -1 -2
-2 0 -3
-1 -4 0
-1 -3 -1
-1 -2 -2
-1 -1 -3
-1 0 -4
0 -5 0
0 -4 -1
0 -3 -2
0 -2 -3
0 -1 -4
0 0 -5

Number : 2
0 0 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0
// C++ Program
// Break the number into three parts
#include<iostream>

using namespace std;
class MyMath
{
    public:
        //Break a number in three parts that sum equal to given value
        void break_number(int number)
        {
            //Variables which is controlling the execution process of loop
            int start = 0, ends = number;
            if (number < 0)
            {
                //Special case
                //When number is less than zero
                start = number;
                ends = 0;
            }
            cout << "\nNumber : " << number << "\n";
            //This loop indicates first number
            for (int i = start; i <= ends; ++i)
            {
                //This loop indicates second number
                for (int j = start; j <= ends; ++j)
                {
                    //This loop indicates third number
                    for (int k = start; k <= ends; ++k)
                    {
                        //Check whether sum of three numbers is equal to given number
                        if (number == (i + j + k))
                        {
                            cout << i << " " << j << " " << k << "\n";
                        }
                    }
                }
            }
        }
};
int main()
{
    MyMath obj ;
    //Simple Case
    obj.break_number(7);
    obj.break_number(5);
    obj.break_number(-5);
    obj.break_number(2);
    return 0;
}

Output

Number : 7
0 0 7
0 1 6
0 2 5
0 3 4
0 4 3
0 5 2
0 6 1
0 7 0
1 0 6
1 1 5
1 2 4
1 3 3
1 4 2
1 5 1
1 6 0
2 0 5
2 1 4
2 2 3
2 3 2
2 4 1
2 5 0
3 0 4
3 1 3
3 2 2
3 3 1
3 4 0
4 0 3
4 1 2
4 2 1
4 3 0
5 0 2
5 1 1
5 2 0
6 0 1
6 1 0
7 0 0

Number : 5
0 0 5
0 1 4
0 2 3
0 3 2
0 4 1
0 5 0
1 0 4
1 1 3
1 2 2
1 3 1
1 4 0
2 0 3
2 1 2
2 2 1
2 3 0
3 0 2
3 1 1
3 2 0
4 0 1
4 1 0
5 0 0

Number : -5
-5 0 0
-4 -1 0
-4 0 -1
-3 -2 0
-3 -1 -1
-3 0 -2
-2 -3 0
-2 -2 -1
-2 -1 -2
-2 0 -3
-1 -4 0
-1 -3 -1
-1 -2 -2
-1 -1 -3
-1 0 -4
0 -5 0
0 -4 -1
0 -3 -2
0 -2 -3
0 -1 -4
0 0 -5

Number : 2
0 0 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0
// C# Program
// Break the number into three parts
using System;
class MyMath
{
    //Break a number in three parts that sum equal to given value
    public void break_number(int number)
    {
        //Variables which is controlling the execution process of loop
        int start = 0, ends = number;
        if (number < 0)
        {
            //Special case
            //When number is less than zero
            start = number;
            ends = 0;
        }
        Console.Write("\nNumber : " + number + "\n");
        //This loop indicates first number
        for (int i = start; i <= ends; ++i)
        {
            //This loop indicates second number
            for (int j = start; j <= ends; ++j)
            {
                //This loop indicates third number
                for (int k = start; k <= ends; ++k)
                {
                    //Check whether sum of three numbers is equal to given number
                    if (number == (i + j + k))
                    {
                        Console.Write(i + " " + j + " " + k + "\n");
                    }
                }
            }
        }
    }
    public static void Main(String[] args)
    {
        MyMath obj = new MyMath();
        //Simple Case
        obj.break_number(7);
        obj.break_number(5);
        obj.break_number(-5);
        obj.break_number(2);
    }
}

Output

Number : 7
0 0 7
0 1 6
0 2 5
0 3 4
0 4 3
0 5 2
0 6 1
0 7 0
1 0 6
1 1 5
1 2 4
1 3 3
1 4 2
1 5 1
1 6 0
2 0 5
2 1 4
2 2 3
2 3 2
2 4 1
2 5 0
3 0 4
3 1 3
3 2 2
3 3 1
3 4 0
4 0 3
4 1 2
4 2 1
4 3 0
5 0 2
5 1 1
5 2 0
6 0 1
6 1 0
7 0 0

Number : 5
0 0 5
0 1 4
0 2 3
0 3 2
0 4 1
0 5 0
1 0 4
1 1 3
1 2 2
1 3 1
1 4 0
2 0 3
2 1 2
2 2 1
2 3 0
3 0 2
3 1 1
3 2 0
4 0 1
4 1 0
5 0 0

Number : -5
-5 0 0
-4 -1 0
-4 0 -1
-3 -2 0
-3 -1 -1
-3 0 -2
-2 -3 0
-2 -2 -1
-2 -1 -2
-2 0 -3
-1 -4 0
-1 -3 -1
-1 -2 -2
-1 -1 -3
-1 0 -4
0 -5 0
0 -4 -1
0 -3 -2
0 -2 -3
0 -1 -4
0 0 -5

Number : 2
0 0 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0
<?php
// Php Program
// Break the number into three parts
class MyMath
{
    //Break a number in three parts that sum equal to given value
    public  function break_number($number)
    {
        //Variables which is controlling the execution process of loop
        $start = 0;
        $ends = $number;
        if ($number < 0)
        {
            //Special case
            //When number is less than zero
            $start = $number;
            $ends = 0;
        }
        echo "\nNumber : ". $number ."\n";
        //This loop indicates first number
        for ($i = $start; $i <= $ends; ++$i)
        {
            //This loop indicates second number
            for ($j = $start; $j <= $ends; ++$j)
            {
                //This loop indicates third number
                for ($k = $start; $k <= $ends; ++$k)
                {
                    //Check whether sum of three numbers is equal to given number
                    if ($number == ($i + $j + $k))
                    {
                        echo $i ." ". $j ." ". $k ."\n";
                    }
                }
            }
        }
    }
}

function main()
{
    $obj = new MyMath();
    //Simple Case
    $obj->break_number(7);
    $obj->break_number(5);
    $obj->break_number(-5);
    $obj->break_number(2);
}
main();

Output

Number : 7
0 0 7
0 1 6
0 2 5
0 3 4
0 4 3
0 5 2
0 6 1
0 7 0
1 0 6
1 1 5
1 2 4
1 3 3
1 4 2
1 5 1
1 6 0
2 0 5
2 1 4
2 2 3
2 3 2
2 4 1
2 5 0
3 0 4
3 1 3
3 2 2
3 3 1
3 4 0
4 0 3
4 1 2
4 2 1
4 3 0
5 0 2
5 1 1
5 2 0
6 0 1
6 1 0
7 0 0

Number : 5
0 0 5
0 1 4
0 2 3
0 3 2
0 4 1
0 5 0
1 0 4
1 1 3
1 2 2
1 3 1
1 4 0
2 0 3
2 1 2
2 2 1
2 3 0
3 0 2
3 1 1
3 2 0
4 0 1
4 1 0
5 0 0

Number : -5
-5 0 0
-4 -1 0
-4 0 -1
-3 -2 0
-3 -1 -1
-3 0 -2
-2 -3 0
-2 -2 -1
-2 -1 -2
-2 0 -3
-1 -4 0
-1 -3 -1
-1 -2 -2
-1 -1 -3
-1 0 -4
0 -5 0
0 -4 -1
0 -3 -2
0 -2 -3
0 -1 -4
0 0 -5

Number : 2
0 0 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0
// Node Js Program
// Break the number into three parts
class MyMath
{
    //Break a number in three parts that sum equal to given value
    break_number(number)
    {
        //Variables which is controlling the execution process of loop
        var start = 0;
        var ends = number;
        if (number < 0)
        {
            //Special case
            //When number is less than zero
            start = number;
            ends = 0;
        }
        process.stdout.write("\nNumber : " + number + "\n");
        //This loop indicates first number
        for (var i = start; i <= ends; ++i)
        {
            //This loop indicates second number
            for (var j = start; j <= ends; ++j)
            {
                //This loop indicates third number
                for (var k = start; k <= ends; ++k)
                {
                    //Check whether sum of three numbers is equal to given number
                    if (number == (i + j + k))
                    {
                        process.stdout.write(i + " " + j + " " + k + "\n");
                    }
                }
            }
        }
    }
}

function main()
{
    var obj = new MyMath();
    //Simple Case
    obj.break_number(7);
    obj.break_number(5);
    obj.break_number(-5);
    obj.break_number(2);
}
main();

Output

Number : 7
0 0 7
0 1 6
0 2 5
0 3 4
0 4 3
0 5 2
0 6 1
0 7 0
1 0 6
1 1 5
1 2 4
1 3 3
1 4 2
1 5 1
1 6 0
2 0 5
2 1 4
2 2 3
2 3 2
2 4 1
2 5 0
3 0 4
3 1 3
3 2 2
3 3 1
3 4 0
4 0 3
4 1 2
4 2 1
4 3 0
5 0 2
5 1 1
5 2 0
6 0 1
6 1 0
7 0 0

Number : 5
0 0 5
0 1 4
0 2 3
0 3 2
0 4 1
0 5 0
1 0 4
1 1 3
1 2 2
1 3 1
1 4 0
2 0 3
2 1 2
2 2 1
2 3 0
3 0 2
3 1 1
3 2 0
4 0 1
4 1 0
5 0 0

Number : -5
-5 0 0
-4 -1 0
-4 0 -1
-3 -2 0
-3 -1 -1
-3 0 -2
-2 -3 0
-2 -2 -1
-2 -1 -2
-2 0 -3
-1 -4 0
-1 -3 -1
-1 -2 -2
-1 -1 -3
-1 0 -4
0 -5 0
0 -4 -1
0 -3 -2
0 -2 -3
0 -1 -4
0 0 -5

Number : 2
0 0 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0
#  Python 3 Program
#  Break the number into three parts
class MyMath :
    # Break a number in three parts that sum equal to given value
    def break_number(self, number) :
        # Variables which is controlling the execution process of loop
        start = 0
        ends = number
        if (number < 0) :
            # Special case
            # When number is less than zero
            start = number
            ends = 0
        
        print("\nNumber : ", number ,"\n", end = "")
        
        i = start
        j = start
        k = start
        # This loop indicates first number
        while (i <= ends) :
            j = start
            # This loop indicates second number
            while (j <= ends) :
                k = start
                # This loop indicates third number
                while (k <= ends) :
                    # Check whether sum of three numbers is equal to given number
                    if (number == (i + j + k)) :
                        print(i ," ", j ," ", k ,"\n", end = "")
                    
                    k += 1
                
                j += 1
            
            i += 1
        
    

def main() :
    obj = MyMath()
    # Simple Case
    obj.break_number(7)
    obj.break_number(5)
    obj.break_number(-5)
    obj.break_number(2)

if __name__ == "__main__": main()

Output

Number :  7
0   0   7
0   1   6
0   2   5
0   3   4
0   4   3
0   5   2
0   6   1
0   7   0
1   0   6
1   1   5
1   2   4
1   3   3
1   4   2
1   5   1
1   6   0
2   0   5
2   1   4
2   2   3
2   3   2
2   4   1
2   5   0
3   0   4
3   1   3
3   2   2
3   3   1
3   4   0
4   0   3
4   1   2
4   2   1
4   3   0
5   0   2
5   1   1
5   2   0
6   0   1
6   1   0
7   0   0

Number :  5
0   0   5
0   1   4
0   2   3
0   3   2
0   4   1
0   5   0
1   0   4
1   1   3
1   2   2
1   3   1
1   4   0
2   0   3
2   1   2
2   2   1
2   3   0
3   0   2
3   1   1
3   2   0
4   0   1
4   1   0
5   0   0

Number :  -5
-5   0   0
-4   -1   0
-4   0   -1
-3   -2   0
-3   -1   -1
-3   0   -2
-2   -3   0
-2   -2   -1
-2   -1   -2
-2   0   -3
-1   -4   0
-1   -3   -1
-1   -2   -2
-1   -1   -3
-1   0   -4
0   -5   0
0   -4   -1
0   -3   -2
0   -2   -3
0   -1   -4
0   0   -5

Number :  2
0   0   2
0   1   1
0   2   0
1   0   1
1   1   0
2   0   0
#  Ruby Program
#  Break the number into three parts
class MyMath

    # Break a number in three parts that sum equal to given value
    def break_number(number)
    
        # Variables which is controlling the execution process of loop
        start = 0
        ends = number
        if (number < 0)
        
            # Special case
            # When number is less than zero
            start = number
            ends = 0
        end
        print("\nNumber : ", number ,"\n")
        
        i = start
        j = start
        k = start
        # This loop indicates first number
        while (i <= ends)
        
            j = start
            # This loop indicates second number
            while (j <= ends)
            
                k = start
                # This loop indicates third number
                while (k <= ends)
                
                    # Check whether sum of three numbers is equal to given number
                    if (number == (i + j + k))
                    
                        # Display the resultant numbers
                        print(i ," ", j ," ", k ,"\n")
                    end
                    k += 1
                end
                j += 1
            end
            i += 1
        end
    end
end
def main()

    obj = MyMath.new()
    # Simple Case
    obj.break_number(7)
    obj.break_number(5)
    obj.break_number(-5)
    obj.break_number(2)
end
main()

Output

Number : 7
0 0 7
0 1 6
0 2 5
0 3 4
0 4 3
0 5 2
0 6 1
0 7 0
1 0 6
1 1 5
1 2 4
1 3 3
1 4 2
1 5 1
1 6 0
2 0 5
2 1 4
2 2 3
2 3 2
2 4 1
2 5 0
3 0 4
3 1 3
3 2 2
3 3 1
3 4 0
4 0 3
4 1 2
4 2 1
4 3 0
5 0 2
5 1 1
5 2 0
6 0 1
6 1 0
7 0 0

Number : 5
0 0 5
0 1 4
0 2 3
0 3 2
0 4 1
0 5 0
1 0 4
1 1 3
1 2 2
1 3 1
1 4 0
2 0 3
2 1 2
2 2 1
2 3 0
3 0 2
3 1 1
3 2 0
4 0 1
4 1 0
5 0 0

Number : -5
-5 0 0
-4 -1 0
-4 0 -1
-3 -2 0
-3 -1 -1
-3 0 -2
-2 -3 0
-2 -2 -1
-2 -1 -2
-2 0 -3
-1 -4 0
-1 -3 -1
-1 -2 -2
-1 -1 -3
-1 0 -4
0 -5 0
0 -4 -1
0 -3 -2
0 -2 -3
0 -1 -4
0 0 -5

Number : 2
0 0 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0
// Scala Program
// Break the number into three parts
class MyMath
{
    //Break a number in three parts that sum equal to given value
    def break_number(number: Int): Unit = {
        //Variables which is controlling the execution process of loop
        var start: Int = 0;
        var ends: Int = number;
        if (number < 0)
        {
            //Special case
            //When number is less than zero
            start = number;
            ends = 0;
        }
        print("\nNumber : " + number + "\n");
        
        var i: Int = start;
        var j: Int = start;
        var k: Int = start;
        //This loop indicates first number
        while (i <= ends)
        {
            j = start;
            //This loop indicates second number
            while (j <= ends)
            {
                k = start;
                //This loop indicates third number
                while (k <= ends)
                {
                    //Check whether sum of three numbers is equal to given number
                    if (number == (i + j + k))
                    {
                        //Display the resultant numbers
                        print(" "+i + " " + j + " " + k + "\n");
                    }
                    k += 1;
                }
                j += 1;
            }
            i += 1;
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var obj: MyMath = new MyMath();
        //Simple Case
        obj.break_number(7);
        obj.break_number(5);
        obj.break_number(-5);
        obj.break_number(2);
    }
}

Output

Number : 7
 0 0 7
 0 1 6
 0 2 5
 0 3 4
 0 4 3
 0 5 2
 0 6 1
 0 7 0
 1 0 6
 1 1 5
 1 2 4
 1 3 3
 1 4 2
 1 5 1
 1 6 0
 2 0 5
 2 1 4
 2 2 3
 2 3 2
 2 4 1
 2 5 0
 3 0 4
 3 1 3
 3 2 2
 3 3 1
 3 4 0
 4 0 3
 4 1 2
 4 2 1
 4 3 0
 5 0 2
 5 1 1
 5 2 0
 6 0 1
 6 1 0
 7 0 0

Number : 5
 0 0 5
 0 1 4
 0 2 3
 0 3 2
 0 4 1
 0 5 0
 1 0 4
 1 1 3
 1 2 2
 1 3 1
 1 4 0
 2 0 3
 2 1 2
 2 2 1
 2 3 0
 3 0 2
 3 1 1
 3 2 0
 4 0 1
 4 1 0
 5 0 0

Number : -5
 -5 0 0
 -4 -1 0
 -4 0 -1
 -3 -2 0
 -3 -1 -1
 -3 0 -2
 -2 -3 0
 -2 -2 -1
 -2 -1 -2
 -2 0 -3
 -1 -4 0
 -1 -3 -1
 -1 -2 -2
 -1 -1 -3
 -1 0 -4
 0 -5 0
 0 -4 -1
 0 -3 -2
 0 -2 -3
 0 -1 -4
 0 0 -5

Number : 2
 0 0 2
 0 1 1
 0 2 0
 1 0 1
 1 1 0
 2 0 0
// Swift Program
// Break the number into three parts
class MyMath
{
    //Break a number in three parts that sum equal to given value
    func break_number(_ number: Int)
    {
        //Variables which is controlling the execution process of loop
        var start: Int = 0;
        var ends: Int = number;
        if (number < 0)
        {
            //Special case
            //When number is less than zero
            start = number;
            ends = 0;
        }
        print("\nNumber : ", number ,"\n", terminator: "");
        
        var i: Int = start;
        var j: Int = start;
        var k: Int = start;
        //This loop indicates first number
        while (i <= ends)
        {
            j = start;
            //This loop indicates second number
            while (j <= ends)
            {
                k = start;
                //This loop indicates third number
                while (k <= ends)
                {
                    //Check whether sum of three numbers is equal to given number
                    if (number == (i + j + k))
                    {
                        print(i ," ", j ," ", k ,"\n", terminator: "");
                    }
                    k += 1;
                }
                j += 1;
            }
            i += 1;
        }
    }
}
func main()
{
    let obj: MyMath = MyMath();
    //Simple Case
    obj.break_number(7);
    obj.break_number(5);
    obj.break_number(-5);
    obj.break_number(2);
}
main();

Output

Number :  7
0   0   7
0   1   6
0   2   5
0   3   4
0   4   3
0   5   2
0   6   1
0   7   0
1   0   6
1   1   5
1   2   4
1   3   3
1   4   2
1   5   1
1   6   0
2   0   5
2   1   4
2   2   3
2   3   2
2   4   1
2   5   0
3   0   4
3   1   3
3   2   2
3   3   1
3   4   0
4   0   3
4   1   2
4   2   1
4   3   0
5   0   2
5   1   1
5   2   0
6   0   1
6   1   0
7   0   0

Number :  5
0   0   5
0   1   4
0   2   3
0   3   2
0   4   1
0   5   0
1   0   4
1   1   3
1   2   2
1   3   1
1   4   0
2   0   3
2   1   2
2   2   1
2   3   0
3   0   2
3   1   1
3   2   0
4   0   1
4   1   0
5   0   0

Number :  -5
-5   0   0
-4   -1   0
-4   0   -1
-3   -2   0
-3   -1   -1
-3   0   -2
-2   -3   0
-2   -2   -1
-2   -1   -2
-2   0   -3
-1   -4   0
-1   -3   -1
-1   -2   -2
-1   -1   -3
-1   0   -4
0   -5   0
0   -4   -1
0   -3   -2
0   -2   -3
0   -1   -4
0   0   -5

Number :  2
0   0   2
0   1   1
0   2   0
1   0   1
1   1   0
2   0   0

Resultant Output Explanation

The program executes the break_number function for each test case and displays all possible combinations of three integers that sum up to the given number.

Time Complexity

The time complexity of the code is cubic (O(n^3)), where 'n' is the absolute value of the input number. This is because the program uses three nested loops to iterate over all possible combinations of three integers between start and ends, and the range of iterations is proportional to the input number. As the input number becomes larger, the time complexity increases rapidly.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment