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 / 5012
5011  |  5013
Subject: 
A recursion question/test...
Newsgroups: 
lugnet.robotics
Date: 
Wed, 12 May 1999 01:19:43 GMT
Original-From: 
Jim Choate <ravage@^nospam^EINSTEIN.ssz.com>
Viewed: 
511 times
  
Hi,

With all the discussion on understanding recursion and such for programmers
I thought I'd share a little problem I find enlightening....


Describe as simple a recursive algorithm as possible for summing the first n
numbers.


    ____________________________________________________________________

       Three step plan: 1. Take over world. 2. Get lot's of cookies.
                        3. Eat the cookies.

                                                    Anonymous

       The Armadillo Group       ,::////;::-.          James Choate
       Austin, Tx               /:'///// ``::>/|/      ravage@ssz.com
       www.ssz.com            .',  ||||    `/( e\      512-451-7087
                           -====~~mm-'`-```-mm --'-
    --------------------------------------------------------------------
--
Did you check the web site first?: http://www.crynwr.com/lego-robotics



Message has 6 Replies:
  Re: A recursion question/test...
 
(...) d'oh. my ($n)=@ARGV; my $result=$n; $result += $n while($n--); print $result."\n"; What a difference a compile makes... but it's still not very recursive. I'll be interested in seeing your answer. Aloha, JR - - - - - - - - - - JR Conlin (...) (25 years ago, 12-May-99, to lugnet.robotics)
  Re: A recursion question/test...
 
Jim Choate poses the problem: (...) What other than sum(n) = n + sum(n-1) (with proper termination conditions, of course - sum(1) = 1). Ralph deal@kzoo.edu -- Did you check the web site first?: (URL) (25 years ago, 12-May-99, to lugnet.robotics)
  Re: A recursion question/test...
 
It should be pointed out that the problem of summing the first n natural numbers is isomorphic to that of finding the factorial of n. Compare these two functions FUNCTION sum(n: natural): natural = IF n = 0 THEN 0 ELSE n + sum(n - 1) FUNCTION fac(n: (...) (25 years ago, 12-May-99, to lugnet.robotics)
  Re: A recursion question/test... (correction)
 
Whoops! I typed the last two lines with the two aggregate operators interchanged! Obviously they should have read FUNCTION sum(n: natural): natural = SUM [0..n] FUNCTION fac(n: natural): natural = PRODUCT [1..n] Malcolm -- Did you check the web site (...) (25 years ago, 12-May-99, to lugnet.robotics)
  Re: A recursion question/test...
 
It should be pointed out that the problem of summing the first n natural numbers is isomorphic to that of finding the factorial of n. Compare these two functions FUNCTION sum(n: natural): natural = IF n = 0 THEN 0 ELSE n + sum(n - 1) FUNCTION fac(n: (...) (25 years ago, 12-May-99, to lugnet.robotics)
  Re: A recursion question/test...
 
(...) int sum(int n) { return n*(n-1)/2; } A degenerate recursive algorithm, of course. Cheers, Ben. -- (URL) grandfather once told me that there are two kinds of people: those who work and those who take the credit. He told me to try to be in the (...) (25 years ago, 12-May-99, to lugnet.robotics)

7 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