Hey, a beginner needs Help

Oct 19, 2018 at 9:02am
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);
    else
        return 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!
Oct 19, 2018 at 9:11am
Im sorry its not a sum of three consecutive numbers but multiplication.
Oct 19, 2018 at 9:11am
You need to call the factorial function, store its value and then check if the value can be expressed as a sum of three consecutive numbers.
Oct 19, 2018 at 9:45am
Oh thank you, but how do you check it?
Oct 19, 2018 at 10:05am
@Lucky7 - please make up your mind.

Do you want the SUM or the PRODUCT of three consecutive numbers?

If you want the SUM, then n! will be the sum of three consecutive numbers iff n >= 3.

If you want the PRODUCT, then check whether (r-1)r(r+1) equals n!, where
r = (int)( 1 + (n!)1/3)
That would be the only possibility.
Last edited on Oct 19, 2018 at 10:39am
Oct 19, 2018 at 10:06am
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
Topic archived. No new replies allowed.