Algorithms

Moderators: None (Apply to moderate this forum)
Number of threads: 402
Number of posts: 786

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Need some advice with a recursive problem Posted by Howler72 on 11 Apr 2004 at 7:01 PM
I having a hard time trying to create an recursive funtion that will determine the fp value of 1/1 + 1/2 - 1/3 + 1/4 - 1/5....for any number of n (1/n). Any pointers will be greatly appreciated.

Thanks,

O
Report
Re: Need some advice with a recursive problem Posted by gaetano on 5 May 2004 at 4:31 AM
I having a hard time trying to create an recursive funtion that will determine the fp value of 1/1 + 1/2 - 1/3 + 1/4 - 1/5....for any number of n (1/n). Any pointers will be greatly appreciated.



Hi!
The following code should be all you need. Do me a favour and inform the community next time about your language of choice. If you need the program in Delphi or Perl or any other language, just mail me and I'll see what I can do.

HTH



#include <iostream>
using namespace std;

double calc1_x (int n);

int
main (void)
{
	int	n;

	cout << "n: ";
	cin >> n;

	cout << "Result = " << calc1_x (n) << endl;

	return 0;
}

double
calc1_x (int n)
{
	if (n == 1)
		return 1;

	return 1.0/n + calc1_x (n - 1);
}

Report
Re: Need some advice with a recursive problem Posted by vijaykumarbali on 10 May 2004 at 5:42 AM
friend iam writing the function ; read it once

int sum(int beg,float n)
{
if(n==0)
return -1;
if(beg>n)
return 0;
else
return (1/float(beg)+pow(-1,beg+1)*sum(beg++,n));
}
this is c styled function which takes 2 parameters
beg should be initialzed to 1 and n>= beg
for example u want to calculate ur problem for n=10
call it as sum(1,10)
it will return u the required answer if any bug do
ask me.
thanx















: I having a hard time trying to create an recursive funtion that will determine the fp value of 1/1 + 1/2 - 1/3 + 1/4 - 1/5....for any number of n (1/n). Any pointers will be greatly appreciated.
:
: Thanks,
:
: O
:

Report
Corrections and annotations Posted by gaetano on 11 May 2004 at 2:43 AM
This message was edited by gaetano at 2004-5-11 2:46:35


Hi!
After a few corrections this algorithm works at least, but some questions just won't be answered in my considerations. So please help me:
- What advantages offers this algorithm which my don't?
- Why do you take an float as the second parameter? It's obviously unnecessary as you're always checking against an integer ...

And some other Comments I'd like to do:
- What about calculating the n-th power of 1? It's always 1! At last the whole term with the power is useless, because the 1 should be positive and _not_ negative.
- All these unnecessary calculations have no effect and prolong the calculation time!


Your corrected code:

double
sum(int beg,float n)
{
	if(n==0)
		return -1;
	if(beg>n)
		return 0;
	else
		return (1/(double) beg + pow (1, beg+1) * sum (++beg, n));
} 


My code:

double
calc1_x (int n)
{
	if (n == 1)
		return 1;

	return 1.0/n + calc1_x (n - 1);
}









 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.