What is the required recursive function(s) in Scheme programming language to compute the following series?

前端 未结 2 1523
情话喂你
情话喂你 2021-01-29 14:13

what is the required recursive function(s) in Scheme programming language to compute the following series?? Explanation needed

1^2/2^1 + 3^4/4^3 + 5^6/6^5 +

2条回答
  •  感动是毒
    2021-01-29 14:57

    So, well, what does each term look like? It's n^(n+1)/(n+1)^n. And you want to stop when you reach 10 (so if n > 10, stop). So write a function of a single argument, n, which either:

    • returns 0 if n > 10;
    • adds n^(n+1)/(n+1)^n to the result of calling itself on n + 2.

    Then this function with argument 1 will compute what you want. Going backwards may be easier:

    • return 0 if n < 1;
    • add n^(n+1)/(n+1)^n to the result of calling itself on n - 2;

    then the function with argument 10 is what you want.


    Or you could do this which is more entertaining:

    (define s
      (λ (l)
        ((λ (c i a)
           (if (> i l)
               a
               (c c
                  (+ i 2)
                  (+ a (/ (expt i (+ i 1))
                          (expt (+ i 1) i))))))
         (λ (c i a)
           (if (> i l)
               a
               (c c
                  (+ i 2)
                  (+ a (/ (expt i (+ i 1))
                          (expt (+ i 1) i))))))
         1 0)))
    

    But I don't recommend it.

提交回复
热议问题