Count of matrices (of different orders) with given number of elements

Given a number N denotes the total number of elements in a matrix, the task is to print all possible order of matrix. An order is a pair (m, n) of integers where m is number of rows and n is number of columns. For example, if the number of elements is 8 then all possible orders are:Â
(1, 8), (2, 4), (4, 2), (8, 1).
Examples:Â
Input: N = 8Â
Output: (1, 2) (2, 4) (4, 2) (8, 1)
Input: N = 100Â
Output:Â
(1, 100) (2, 50) (4, 25) (5, 20) (10, 10) (20, 5) (25, 4) (50, 2) (100, 1)
Approach:Â
A matrix is said to be of order m x n if it has m rows and n columns. The total number of elements in a matrix is equal to (m*n). So we start from 1 and check one by one if it divides N(the total number of elements). If it divides, it will be one possible order.
Below is the implementation of the above approach:Â Â
C++
// C++ implementation of the above approach#include <iostream>using namespace std;Â
// Function to print all possible ordervoid printAllOrder(int n){    // total number of elements in a matrix     // of order m * n is equal (m*n)    // where m is number of rows and n is     // number of columns    for (int i = 1; i <= n; i++) {Â
        // if n is divisible by i then i        // and n/i will be the one        // possible order of the matrix        if (n % i == 0) {Â
            // print the given format            cout << i << " " << n / i << endl;        }    }}Â
// Driver codeint main(){Â Â Â Â int n = 10;Â Â Â Â printAllOrder(n);Â Â Â Â return 0;} |
Java
// Java implementation of the above approachÂ
Â
class GFG    {    // Function to print all possible order    static void printAllOrder(int n)    {        // total number of elements in a matrix         // of order m * n is equal (m*n)        // where m is number of rows and n is         // number of columns        for (int i = 1; i <= n; i++) {                 // if n is divisible by i then i            // and n/i will be the one            // possible order of the matrix            if (n % i == 0) {                     // print the given format                System.out.println( i + " " + n / i );            }        }    }         // Driver code    public static void main(String []args)    {        int n = 10;        printAllOrder(n);             }Â
}Â
Â
// This code is contributed by ihritik |
Python
# Python implementation of the above approachÂ
# Function to print all possible orderdef printAllOrder(n):Â
    # total number of elements in a matrix     # of order m * n is equal (m*n)    # where m is number of rows and n is     # number of columns    for i in range(1,n+1):Â
        # if n is divisible by i then i        # and n/i will be the one        # possible order of the matrix        if (n % i == 0) :Â
            # print the given format            print( i ,n // i )              Â
Â
# Driver coden = 10printAllOrder(n)Â
Â
# This code is contributed by ihritik |
C#
// C# implementation of the above approachÂ
using System;class GFG    {    // Function to print all possible order    static void printAllOrder(int n)    {        // total number of elements in a matrix         // of order m * n is equal (m*n)        // where m is number of rows and n is         // number of columns        for (int i = 1; i <= n; i++) {                 // if n is divisible by i then i            // and n/i will be the one            // possible order of the matrix            if (n % i == 0) {                     // print the given format                Console.WriteLine( i + " " + n / i );            }        }    }         // Driver code    public static void Main()    {        int n = 10;        printAllOrder(n);             }Â
}Â
// This code is contributed by ihritik |
PHP
<?php// PHP implementation of the above approach Â
// Function to print all possible order function printAllOrder($n) {     // total number of elements in a matrix     // of order m * n is equal (m*n)     // where m is number of rows and n is     // number of columns     for ($i = 1; $i <= $n; $i++)     { Â
        // if n is divisible by i then i         // and n/i will be the one         // possible order of the matrix         if ($n % $i == 0)         { Â
            // print the given format             echo $i, " ", ($n / $i), "\n";         }     } } Â
// Driver code $n = 10; printAllOrder($n); Â
// This code is contributed by Ryuga?> |
Javascript
<script>// Java Script implementation of the above approachÂ
Â
    // Function to print all possible order    function printAllOrder( n)    {        // total number of elements in a matrix        // of order m * n is equal (m*n)        // where m is number of rows and n is        // number of columns        for (let i = 1; i <= n; i++) {                 // if n is divisible by i then i            // and n/i will be the one            // possible order of the matrix            if (n % i == 0) {                     // print the given format                document.write( i + " " + n / i+"<br>" );            }        }    }         // Driver code             let n = 10;        printAllOrder(n);              // This code is contributed by sravan</script> |
1 10 2 5 5 2 10 1
Â
Time Complexity: O(n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



