Hey i have been given a task:
Given a positive integer n. Find out if n! can be expressed as a sum of three consecutive numbers. Use a function that calculates a factorial in the solution.
1 2 3 4 5 6 7
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
elsereturn 1;
}
I know that i have to use this function, but i am stuck on how to check if its a sum of 3 numbers. Thanks for help in advance!
A method to check is to check if the number is equal to 1*2*3. If it's not, check if it's equal to 2*3*4. Then check if it's equal to 3*4*5. Then check if it's equal to 4*5*6. Then check if it's equal to 5*6*7. Then check if it's equal to 6*7*8. Then check if it's equal to 7*8*9. Then check if it's equal to 8*9*10...
I'll leave you to work out when to stop checking, and also to work out how you can avoid having to always start at 1*2*3