0

I am Trying to implement a Euler number in drracket aka Scheme. i saw a solution by means of the search function but it is way to complex.

 (define (fakultät n)
  (cond
    [(= n 0) 0]
    [(= n 1) 1]
    [else (* (fakultät (- n 1)) n)]))
(define (e n)
  (cond
    [(= n 0) 1]
    [(= n 1) 2.72]
    [else (+ (/ 1 (fakultät n )) (e(- n 1)))]))

First i defined a factorial and afterwards I defined the e function. the result somehow isn't correct

1 Answers1

3

Sidenote You should really provide more details in your question such as what was the input and actual output (and usually the expected output but in this case it is kind of clear)

I think the bug lies in the line

[(= n 1) 2.72]

I'm not sure where you get this line from. My only guess is that this is a leftover from some other implementation that is based on some approximation method. e is approximately 2.718... so 2.72 alone is greater than the correct answer and you are going add to it a few more 1/n!.

If you remove that line the code seems to work OK as you can see here

SergGr
  • 23,570
  • 2
  • 30
  • 51
  • thank you very much for your answer . So i tried your version for (e 1) and evaluated it . the result was two. i also tried (e 3) but the result still wasn't the expected one. can you tell me what you had as an input so that the result is 2.71 –  Mar 13 '18 at 22:14
  • @AzadKygn, I think the first question is: do you understand the algorithm behind this code? Why it has `n` as a parameter at all? – SergGr Mar 14 '18 at 16:05
  • Yeah I got thank you that was a dumb question @SergGr. the higher n is the more precise our e-number is since it is a never ending row –  Mar 16 '18 at 15:16
  • @AzadKygn, I'm glad you sorted that out. I think now it should also be obvious why that line is the source of the initial bug. P.S. by the way, if some answer on SO helps you, it is customary to Accept that answer. – SergGr Mar 16 '18 at 15:30