sum of squares of integers

This question already has an answer here:

  • Sum of Squares using Haskell 3 answers

  • From what I understand, you want to take two numbers, eg 1 and 10 , square each number between them (inclusively), and then take the sum of that. So you'd want some function like

    sumOfSquaresBetween :: Int -> Int -> Int
    sumOfSquaresBetween m n = ???
    

    Now, you have to use recursion, so this means that ??? is going to be some expression that uses sumOfSquaresBetween .

    Now here's the trick: If you know sumOfSquares nn , then how would you find sumOfSquares (n - 1) n ? What about sumOfSquares (n - 2) n ? Can you generalize this all the way to sumOfSquares mn for m <= n ? If so, then you've just performed your desired algorithm, but in reverse.

    Hope this hint helps.


    "The sum of the squares of integers in the range m:n (where m ≥n) can be computed recursively."

    Let's break this apart....


    "integers in the range m:n"

    is the set of integers starting from m, going to n

    [m, m+1, m+2, ....n]

    ie-

    integers in the range 4:8 = [4,5,6,7,8]
    

    "squares of...."

    As you probably know, the square of a number x is x*x , so

    squares of integers in the range 4:8 = [16, 26, 36, 49, 64]
    

    "The sum of...."

    add them

    The sum of the squares of integers in the range 4:8 = 16+26+36+49+64
    

    ".... can be computer recursively"

    Well, you have to understand recursion to get this....

    Any function that contains itself in the definition is recursive. Of course you have to be careful, if done incorrectly, a recursive function could lead to infinite loops....

    For Ints, (N-1) recursion is common.... If you can use the calculation for (N-1) to evaluate the calculation for N, the computer can run down the numbers until a known value is hit (typically 0). This is better seen with an example.

    let func n = sum of integers from 0 to n
    

    (this is like your problem, but without the squares part)

    if you know the value of func (n-1) , you can easily compute the value of func n

    func n = n + func (n-1)
    func 0 = 0
    

    The computer will use func 0 to compute func 1 , func 1 to compute func 2 , etc, all the way to N.


    Recursion has two common (but actually pretty different) uses... First, as shown above, it allows for very clean function definitions.

    Secondly, it is often used in mathematics to prove truths over all integers (ie- to prove something is true for all ints, prove it is true for 0, then prove if it is true for N, it is true for N+1....).


    Really, the best way to solve this problem is also the easiest: use library functions.

    sumsquares :: Integral a => a -> a -> a
    sumsquares m n = sum (map (^2) (enumFromTo n m))
    

    You just enumerate the numbers from n to m , square each of them, and take the sum of the results. Trying to solve this problem in with direct recursion just makes things needlessly complicated.

    Exercise: Write your own versions of the library functions used in this answer.

    -- | Generate the list of all values in the given range.  Result is inclusive.
    enumFromTo :: Enum a => a -> a -> [a]
    
    -- | Apply a function individually to each element of the argument list,
    -- and collect the results as a list, respecting the order of the original.
    map :: (a -> b) -> [a] -> [b]
    
    
    -- | Calculate the sum of a list of numbers.
    sum :: Num a => [a] -> a
    
    链接地址: http://www.djcxy.com/p/80704.html

    上一篇: Python递归海龟函数,它吸引我的资本

    下一篇: 整数平方和