MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply TodayA recurrence relation is given by

P(1) := 1 P(n+1) := exp(1) - (n+1)*P(n)

Write a function that, given an integer `n`, returns `P(n)`. The `n` will be smaller than 100 in all test-suite problems and an absolute precision of 10*eps (~2e-15, i.e. almost machine precision) is required.

36 correct solutions
108 incorrect solutions

Last solution submitted on Mar 27, 2016

1 player likes this solution

1 Comment

Aurelien Queffurust
on 24 Jul 2012

different matlab fnction used to solve this problem

1 Comment

Aurelien Queffurust
on 24 Jul 2012

lol ! need a translator for this solution!

2 Comments

@bmtran
on 31 Jan 2012

This solution should provide minimal rounding error since it's done in an essentially symbolic domain and then evaluated at the very end, but it doesn't satisfy the test cases. *sigh*

Tomasz
on 31 Jan 2012

The sequence is strictly positive. (Not easy to see, but true). If your pn's get negative at some higher n, you still have the roundoff problem. It would be indeed avoided if the integer calculation within the loop was exact, but the numbers quickly become larger than the 48bit mantissa of a double.

2 Comments