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

前端 未结 2 1483
情话喂你
情话喂你 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.

    0 讨论(0)
  • 2021-01-29 15:01
    //power function
    (define (power a b)
         (if (zero? b) //base case
          1    
         (* a (power a (- b 1))))) //or return power of a,b
    
    // sum function for series
    
        (define (sum n)
         (if (< n 3) //base case 
             0.5
           (+ (/  (power (- n 1) n) (power n (- n 1))) (sum (- n 2 ))  ))) //recursion call
    
    >(sum 10) // call sum function here .
    
    0 讨论(0)
提交回复
热议问题