To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 5005
5004  |  5006
Subject: 
Re: A recursion question/test...
Newsgroups: 
lugnet.robotics
Date: 
Wed, 12 May 1999 03:52:16 GMT
Original-From: 
JR Conlin <jrconlin@=nomorespam=email.com>
Viewed: 
578 times
  
At 08:19 PM 5/11/99 -0500, Jim Choate wrote:
Describe as simple a recursive algorithm as possible for summing the first n
numbers.


Well, you didn't specify if the numbers existed in a list or were
iterative, and you didn't specify language so:

Smalltalk:
^list sum.
or
^(1..n) sum.

sorry..
if you'll excuse Perl...

List:
foreach $num (@list)
{$result += $num;}
return $result;

Iterative:
my $result = $lowBoundry;
while ($result++ <=$highBoundry);
return $result;

of course the obvious problem is that neither of these are recursive, so:

sub recurse
{
my ($count, $rList)=@_;
if ($count > 0)
{
return recurse($count-1,$rList)+$rList[$count];
}
return $rList[$count];
}

$count = $list;
return &recurse($count,\@list);

Obviously this is simple to modify for iterative numbers, however this
introduces the possibility for stack overrun. You could do other tricks
like create a seperate pointer stack that used virtual memory, or use
longjumps, but these would boil down to being the same as the
non-recursive, iterative solutions...

I guess I'm out of solutions (heck, I haven't even tried compiling these),
but I know there's got to be better ones.

Good Challenge...

Aloha,
JR
- - - - - - - - - -
JR Conlin                  jrconlin@email.com
Techno-Athiest yahooPage: jrconlin
                           <http://home.earthlink.net/~jrconlin>
--
Did you check the web site first?: http://www.crynwr.com/lego-robotics



Message has 3 Replies:
  Re: A recursion question/test...
 
I've been meaning to ask: Do you believe technology exists? It would seem hard to maintain a postition of a Techno-Atheist, given that you're using a computer to post. Just ribbing ya, sorry ;-) (...) -- Did you check the web site first?: (URL) (25 years ago, 12-May-99, to lugnet.robotics)
  Re: A recursion question/test...
 
(...) If you're not going to make it recursive, why not: $result = $n * ($n + 1) / 2; That is effectively the sum of all numbers from 0 to n. -Kekoa (25 years ago, 12-May-99, to lugnet.robotics)
  Re: A recursion question/test...
 
Actually, it came from a conversation I was having about how "religious" some people are about technology. My stance was that there is no supreme hardware or software, they all suck in their own unique way. "So what you're saying is that you are a (...) (25 years ago, 13-May-99, to lugnet.robotics)

4 Messages in This Thread:



Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR