logo CodeStepByStep logo

sum_up_to

Language/Type: PHP recursion

Write a recursive function named sum_up_to that accepts an integer parameter $n and returns the sum of 1 + 1/2 + 1/3 + 1/4 + ... + 1/$n.

For example, the call of sum_up_to(2) should return 1.5 (from 1 + 1/2 = 1.5). You should return 0.0 if $n is 0, and you should throw an Exception if $n is less than 0.

Type your PHP solution code here:


This is a function exercise. Write a PHP function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.