There are a number of ways to go about this, have you created your algorithm for completing this yet?
Also, are you going to just be printing out the final answer of that exact equation, or do you want to be able to change the highest number that it can go up to? Because if it is the first, you can just figure it out by hand and write a print statement. The latter will require actually creating an algorithm to figure out factorials.
For S(n) == 1! + 2! + 3! + n! you can easily find that S(n)= S(n-1)+ (n-1)!*n so
if you keep the last term apart from the grand result you can always easily
calculate the next term.
e.g. User says to sum the first 6 number's factorials.
You make an array of length 6. The first element (array[0]) is 1. (This represents 1!).
Then, for any element N, assuming the previous element has already been set, the value needed in array[N-1] is array[N-2] * (N-1).
To find the sum, add all the numbers in the array.
This is a much faster method of calculating factorials then either nested loops or a recursive function, which are the two most common methods I've seen for calculating factorials.
Comment