![]() |
|
Factorial Series - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: Factorial Series (/Thread-Factorial-Series) Pages:
1
2
|
Factorial Series - brainfreeezed1 - 11-19-2012 Hey guy, do you know how to make a recursive function that work like this: we input a value n the function will return the sum of the factorial like this: 1! +2! + .... + (n-1)! + n! without using loop, only recursive. RE: Factorial Series - unixbreak - 11-19-2012 http://web.eecs.utk.edu/~cs102/lectures/recursion.html RE: Factorial Series - brainfreeezed1 - 11-19-2012 (11-19-2012, 05:23 AM)unixbreak Wrote: http://web.eecs.utk.edu/~cs102/lectures/recursion.html I don't want normal factorial function. i want the sum of !1 to factorial of the input. RE: Factorial Series - The Alchemist - 11-19-2012 Are you talking about this? Spoiler:Code: int fact(int n)
{
if(n==0 || n==1)
return 1;
else
return n*fact(n-1);
}
int sum(int n)
{
if(n==1)
return fact(1);
else
return fact(n)+sum(n-1);
}Or you want it in just one function? RE: Factorial Series - brainfreeezed1 - 11-19-2012 (11-19-2012, 06:16 AM)The Alchemist Wrote: Are you talking about this? yes i want it in only one function. it should takes 1 or more arguments and return the sum. RE: Factorial Series - The Alchemist - 11-19-2012 Here is the complete code : You said one or more arguments... I didn't use arrays to make it simpler... Could have used arrays but it'll make it difficult to understand.. Spoiler:Code: #include<stdio.h>
#include<conio.h>
int factsum(int num,int fact,int sum,int n)
{
if(num==n)
{
fact=fact*num; //finding the factorial
sum=sum+fact; //adding to the series
return sum;
}
else
{
fact=fact*num; //finding the factorial
sum=sum+fact; //adding it to the series sum
return factsum(num+1,fact,sum,n); //going for the next number
}
}
void main()
{
int n;
printf("Enter n : ");
scanf("%d",&n);
int sum=factsum(1,1,0,n); //initial number and factorial is 1, sum is initially zero
printf("The sum of series is : %d",sum);
getch();
}This one will work for sure... RE: Factorial Series - brainfreeezed1 - 11-19-2012 (11-19-2012, 07:05 AM)The Alchemist Wrote: Here is the complete code : great, it works. but is there any other way simpler and shorter than this? not array of course. like process the summing with only the return statement instead of using variable 'sum' to add the value of the factorial. pardon my English. RE: Factorial Series - Frooxius - 11-19-2012 If you look at the topic stickied on the top of this forums, you'll find a nice explanation of the differences between iterative and recursive functions and making the algorithm yourself shouldn't be such a problem if you give it a bit of effort. The Alchemist: The int datatype is completely inappropriate for this. If you tried any even a bit larger number you would find that it's going to start giving you bad results, because it will overflow. Also the code is kinda messy and probably not the best solution. ---------------------------------------- Here's probably the best way to do this and approach the problem: What do you need to do is to determine how to calculate each element of the series and how to nest this calculation in itself. You also need some stop condition, which will always return at the end. Let's use the same one as if we were to calculate just the factorial. The problem with that approach is that we need to return two values from a function - the factorial (so it can be used for further calculation) and the sum of the elements before. So that's what we will do! You can return more values from a function using an argument, if you make it into a pointer. Then the function can place any value into it that you can read later. Here's my implementation: Code: #include <iostream>
using namespace std;
double SumFactorials(int n, double *fact = 0)
{
// the stop condition
if(n <= 1)
{
// factorial of 1 or 0 is always 1
if(fact)
*fact = 1;
return 1; // this is the sum
}
// caltulate the subFactorial (n-1)! first
double subFact;
double sum = SumFactorials(n-1, &subFact);
// now we've got both sum of (n-1) and the factorial, so let's calculate current element
if(fact)
*fact = n*subFact;
return n*subFact + sum;
}
void main()
{
for(int n = 1; n < 20; n++)
cout << n << " -> " << SumFactorials(n) << endl;
cin.get();
}Notice how I also used default parameter for a function to make the pointer null by default - the top function doesn't need to return the factorial, only the sum, so you can call it simply by SumFactorials(n), where n is the same n as in your equation. No other arguments are needed, it's nice, simple and clean. Let me know if you don't understand any part of it or need any further help. RE: Factorial Series - brainfreeezed1 - 11-20-2012 @Frooxius Thanks, It's nice but not simple enough. And is my code acceptable? Code: #include <iostream>
using namespace std;
double sumfac(int n,int num=1)
{
if(n<=1)return 1;
else if(num<=n){return (num*(1+sumfac(n,num+1)));}
else return 0;
}
int main()
{
int i;
cin >> i;
for(int n = 1;n<=i;n++)
cout << n << " -> " <<sumfac(n)<< endl;
cin.get();
return 0;
}Anyway, what does if(fact) do in your code? what makes a pointer true? RE: Factorial Series - Frooxius - 11-20-2012 Yes, your code works, but I intentionally avoided this solution. The reason is that I wanted to make cleaner solution, where the only thing that you need to pass to the function is the "n" parameter, everything else is returned value out of the function, it doesn't need any auxiliary ("help") inputs to work correctly. If you pass incorrect value to the second parameter, it won't work correctly, but you can solve that by creating a "wrapper" instruction. From practical standpoint, the code you presented is probably better, from "recursive purity" and function isolation, mine is better. Your solution has still traces of the iterative way, just wrapped in a function, meaning it can't work without a helper "counting" variable. What I also wanted to demonstrate was passing more values from a function. if(fact) tests if the fact pointer is null or not. If it's null then the condition is false and the assignment won't execute - otherwise the program would crash, because it would try to access invalid place in memory. |