simple code help

edited September 2010 in Science & Tech
Here is my project:

write a matlab function n = P01(e) which does the following: find the smallest value of n such that: 1+1/2+1/3+...+1/n > e
where e > 1 is the input of the function and n is output.

and here is my code:
function n = P01pham( e )
%Find the smallest value of n such that

% 1 + 1/2 + 1/3 +...+ 1/n > e

n = 1;
b = 0;

While ( n > e  )

n = n + 1/b;
b = b + 1;

end

there is somethings wrong, please tell me
thank you

Comments

  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    edited September 2010
    I don't do matlab, but I doubt you can divide by zero, which you would be doing during the first iteration of the line
    n = n + 1/b;
  • ardichokeardichoke Icrontian
    edited September 2010
    DivideByZero.jpg
  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited September 2010
    Like Tushon said,
    n = 0;
    b = 1;
    
    Also, your loop test condition is satisfied as long as n > e, but your problem statement implies that you're interested in finding the value of b that causes n > e. In other words, your loop condition should be
    while ( n < e )
    
Sign In or Register to comment.