Dados dos números m y n, la tarea es encontrar el factorial de todos los números, incluidos m y n, y luego imprimir el siguiente formato.
Ejemplos:
Input : 6 10 Output : 720 5040 40320 362880 3628800 Input : 10 20 Output : 3628800 39916800 479001600 6227020800 87178291200 1307674368000 20922789888000 355687428096000 6402373705728000 121645100408832000 2432902008176640000
Usamos la biblioteca de multiprecisión boost para almacenar el factorial de un gran número e imprimir el factorial con la función setw().
setw(int) -> setw(int) es una función que se usa para la intención en el resultado.
C++
// CPP Program to print format of factorial #include <boost/multiprecision/cpp_int.hpp> #include <iostream> #include <vector> using namespace std; using boost::multiprecision::cpp_int; vector<cpp_int> find_factorial(int num1, int num2) { // vector for store the result vector<cpp_int> vec; // variable for store the // each number factorial cpp_int fac = 1; // copy of first number int temp = num1; // found first number factorial while (1) { if (temp == 1) break; fac *= temp; temp--; } // push the first number in result vector vec.push_back(fac); // increment the first number num1++; // found the all remaining number // factorial loop is working until // all required number factorial founded while (num1 <= num2) { fac *= num1; // store the result of factorial vec.push_back(fac); // increment the first number num1++; } // return the result return (vec); } // function for print the result void print_format(vector<cpp_int>& result) { // setw() is used for fill the blank // right is used for right justification of data int digits = result.back().str().size(); for (int i = 0; i < result.size(); i++) { cout << setw(digits + 1) << right << result[i] << endl; } } // Driver function int main() { // number which found the factorial // of between range int m = 10, n = 20; // store the result of factorial vector<cpp_int> result_fac; // function for found factorial result_fac = find_factorial(m, n); // function for print format print_format(result_fac); return 0; }
Java
// Java Program to print // format of factorial import java.util.*; class GFG { static ArrayList<Long> find_factorial(int num1, int num2) { // vector for store // the result ArrayList<Long> vec = new ArrayList<Long>(); // variable for store the // each number factorial long fac = 1; // copy of first number long temp = num1; // found first // number factorial while (true) { if (temp == 1) break; fac *= temp; temp--; } // push the first number // in result vector vec.add(fac); // increment the // first number num1++; // found the all remaining // number factorial loop // is working until all // required number // factorial founded while (num1 <= num2) { fac *= num1; // store the result // of factorial vec.add(fac); // increment // the first number num1++; } // return the result return (vec); } // function for // print the result static void print_format(ArrayList<Long> result) { // setw() is used for fill // the blank right is used // for right justification // of data int x = result.size(); int digits = String.valueOf(result.get(x - 1)).length(); for (int i = 0; i < x; i++) { System.out.println(String.format( "%1$" + (digits + 1) + "s", result.get(i))); } } // Driver Code public static void main(String[] args) { // number which found // the factorial // of between range int m = 10; int n = 20; // store the result // of factorial // using // function for // found factorial var result_fac = find_factorial(m, n); // function for // print format print_format(result_fac); } } // This code is contributed // by phasing17
Python
# Python Program to print # format of factorial def find_factorial(num1, num2): # vector for store # the result vec = [] # variable for store the # each number factorial fac = 1 # copy of first number temp = num1 # found first # number factorial while (1): if (temp == 1): break fac *= temp temp = temp - 1 # push the first number # in result vector vec.append(fac) # increment the # first number num1 = num1 + 1 # found the all remaining # number factorial loop # is working until all # required number # factorial founded while (num1 <= num2): fac *= num1 # store the result # of factorial vec.append(fac) # increment # the first number num1 = num1 + 1 # return the result return (vec) # function for # print the result def print_format(result): # setw() is used for fill # the blank right is used # for right justification # of data x = len(result) digits = len(str(result[x-1])) for i in range(x): # Creating a temp string of # empty spaces, to right # align them. tmp = "" for j in range(x-i): tmp = tmp + ' ' result[i] = tmp + str(result[i]) print(str(result[i])) # Driver Code # number which found # the factorial # of between range m = 10 n = 20 # function for # found factorial result_fac = find_factorial(m, n) # function for # print format print_format(result_fac) # The code is contributed by Gautam goel (gautamgoel962)
C#
// C# Program to print // format of factorial using System; using System.Collections.Generic; class GFG { static List<long> find_factorial(int num1, int num2) { // vector for store // the result List<long> vec = new List<long>(); // variable for store the // each number factorial long fac = 1; // copy of first number long temp = num1; // found first // number factorial while (true) { if (temp == 1) break; fac *= temp; temp--; } // push the first number // in result vector vec.Add(fac); // increment the // first number num1++; // found the all remaining // number factorial loop // is working until all // required number // factorial founded while (num1 <= num2) { fac *= num1; // store the result // of factorial vec.Add(fac); // increment // the first number num1++; } // return the result return (vec); } // function for // print the result static void print_format(List<long> result) { // setw() is used for fill // the blank right is used // for right justification // of data int x = result.Count; int digits = Convert.ToString(result[x - 1]).Length; for (int i = 0; i < x; i++) { Console.WriteLine(Convert.ToString(result[i]).PadLeft(digits + 1)); } } // Driver Code public static void Main(string[] args) { // number which found // the factorial // of between range int m = 10; int n = 20; // store the result // of factorial // using // function for // found factorial var result_fac = find_factorial(m, n); // function for // print format print_format(result_fac); } } // This code is contributed // by phasing17
PHP
<?PHP // PHP Program to print // format of factorial function find_factorial($num1, $num2) { // vector for store // the result $vec; $t = 0; // variable for store the // each number factorial $fac = 1; // copy of first number $temp = $num1; // found first // number factorial while (1) { if ($temp == 1) break; $fac *= $temp; $temp--; } // push the first number // in result vector $vec[$t++] = $fac; // increment the // first number $num1++; // found the all remaining // number factorial loop // is working until all // required number // factorial founded while ($num1 <= $num2) { $fac *= $num1; // store the result // of factorial $vec[$t++] = $fac; // increment // the first number $num1++; } // return the result return ($vec); } // function for // print the result function print_format($result) { // setw() is used for fill // the blank right is used // for right justification // of data $x = count($result); $digits = strlen((string)$result[$x - 1]); for ($i = 0; $i < $x; $i++) { echo str_pad($result[$i], ($digits + 1), " ", STR_PAD_LEFT) . "\n"; } } // Driver Code // number which found // the factorial // of between range $m = 10; $n = 20; // store the result // of factorial $result_fac; // function for // found factorial $result_fac = find_factorial($m, $n); // function for // print format print_format($result_fac); // This code is contributed // by mits ?>
Javascript
<script> // Javascript Program to print // format of factorial function find_factorial(num1, num2) { // vector for store // the result let vec = []; let t = 0; // variable for store the // each number factorial let fac = 1; // copy of first number let temp = num1; // found first // number factorial while (1) { if (temp == 1) break; fac *= temp; temp--; } // push the first number // in result vector vec[t++] = fac; // increment the // first number num1++; // found the all remaining // number factorial loop // is working until all // required number // factorial founded while (num1 <= num2) { fac *= num1; // store the result // of factorial vec[t++] = fac; // increment // the first number num1++; } // return the result return (vec); } // function for // print the result function print_format(result) { // setw() is used for fill // the blank right is used // for right justification // of data let x = result.length; let digits = String(result[x - 1]).length; for (let i = 0; i < x; i++) { result[i] = new Array(x - i).fill(" ").join(" ") + result[i]; document.write(String(result[i]) + "<br>"); } } // Driver Code // number which found // the factorial // of between range let m = 10; let n = 20; // store the result // of factorial let result_fac; // function for // found factorial result_fac = find_factorial(m, n); // function for // print format print_format(result_fac); // This code is contributed // by gfgking </script>
Producción:
3628800 39916800 479001600 6227020800 87178291200 1307674368000 20922789888000 355687428096000 6402373705728000 121645100408832000 2432902008176640000