简体   繁体   中英

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

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 + 7^8/8^7 + 9^10/10^9

X

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.

//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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM