A recursive function for the sum of "n" number of a series

  • .Una funcion recursiva para la suma de los "n" primeros numeros de la serie

    A=1+2+3+4+...+n

  • Homework or a test?

    Take a look at: http://msdn.microsoft.com/en-us/library/ms186243.aspx

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • .

  • Thought so.

    Did you take a look at the recursive CTE article I linked to?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Of course, the real way to do this in T-SQL isn't recursive, but that would violate your assignment.

    select sum(X)

    from (select top (n) X

    from dbo.MyTable

    order by MySequenceColumn) as Subq1 ;

    Another recursive option would be a recursive UDF, instead of a recursive CTE. You might want to find out which one(s) are acceptable for the assignment. Either one will be slower, less efficient, and just plain a bad idea, compared to the set-based one here.

    But homework is sometimes like that. After all, if school ever actually resembled real life, that would be bad! 😛

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Actually, if you really want to impress, do the problem as assigned. Then show how much more efficient the non-recursive solution is. For bonus credit, do a little research and explain why. (Then you could come back and tell us!)

  • champions (10/21/2011)


    .Una funcion recursiva para la suma de los "n" primeros numeros de la serie

    A=1+2+3+4+...+n

    Tell your instructor to read the following article for why this should be taught ONLY as a very bad thing to do...

    http://www.sqlservercentral.com/articles/T-SQL/74118/

    Also, the quickest way to find the SUM of sequential ascending numbers starting a 0 or 1 is...

    SUM of numbers 0/1 through n = (n2 + n) / 2

    ... but as Gus suggested, that would probably violate the intent of the homework.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply