Efficiently print factorial series in a given range
Write an iterative program to print factorial series in a given range efficiently.
The factorial n! of a non-negative integer n is the product of all positive integers less than or equal to n. For example, if the given range is [5, 10].
The factorial of 6 is 720
The factorial of 7 is 5040
The factorial of 8 is 40320
The factorial of 9 is 362880
The factorial of 10 is 3628800
The iterative version uses a for loop to calculate the product of all positive integers less than equal to n. This approach is demonstrated below in C, Java, and Python:
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> // Iterative function to efficiently print factorial series in a given range void factorial(int beg, int end) { // check for invalid input if (beg < 0 || end < 0 || beg > end) { return; } for (int i = beg; i <= end; i++) { unsigned long fact = 1; for (int j = 1; j <= i; j++) { fact = fact * j; } printf("The factorial of %d is %lu\n", i, fact); } } int main() { int beg = 5; int end = 10; factorial(beg, end); return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Main { // Iterative function to efficiently print factorial series in a given range public static void factorial(int beg, int end) { // check for invalid input if (beg < 0 || end < 0 || beg > end) { return; } for (int i = beg; i <= end; i++) { long fact = 1; for (int j = 1; j <= i; j++) { fact = fact * j; } System.out.printf("The factorial of %d is %d\n", i, fact); } } public static void main(String[] args) { int beg = 5; int end = 10; factorial(beg, end); } } |
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Iterative function to efficiently print factorial series in a given range def factorial(beg, end): # check for invalid input if beg < 0 or end < 0 or beg > end: return for i in range(beg, end + 1): fact = 1 for j in range(1, i + 1): fact = fact * j print(f'The factorial of {i} is {fact}') if __name__ == '__main__': beg = 5 end = 10 factorial(beg, end) |
The factorial of 5 is 120
The factorial of 6 is 720
The factorial of 7 is 5040
The factorial of 8 is 40320
The factorial of 9 is 362880
The factorial of 10 is 3628800
The above solution calculates the factorial again and again for every number in the given range. An efficient solution is to use the previous results to calculate the factorial of the next number, as demonstrated below in C, Java, and Python:
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h> // Iterative function to efficiently print factorial series in a given range void factorial(int beg, int end) { // check for invalid input if (beg < 0 || end < 0 || beg > end) { return; } unsigned long fact = 1; for (int i = 1; i < beg; i++) { fact = fact * i; } for (int i = beg; i <= end; i++) { if (i != 0) { fact = fact * i; } printf("The factorial of %d is %lu\n", i, fact); } } int main() { int beg = 5; int end = 10; factorial(beg, end); return 0; } |
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class Main { // Iterative function to efficiently print factorial series in a given range public static void factorial(int beg, int end) { // check for invalid input if (beg < 0 || end < 0 || beg > end) { return; } long fact = 1; for (int i = 1; i < beg; i++) { fact = fact * i; } for (int i = beg; i <= end; i++) { if (i != 0) { fact = fact * i; } System.out.printf("The factorial of %d is %d\n", i, fact); } } public static void main(String[] args) { int beg = 5; int end = 10; factorial(beg, end); } } |
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Iterative function to efficiently print factorial series in a given range def factorial(beg, end): # check for invalid input if beg < 0 or end < 0 or beg > end: return fact = 1 for i in range(1, beg): fact = fact * i for i in range(beg, end + 1): if i > 0: fact = fact * i print(f'The factorial of {i} is {fact}') if __name__ == '__main__': beg = 5 end = 10 factorial(beg, end) |
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)