However, he always infers: Infinity
Since you are doing 1/0 in the next steps of the code that Infinity gives.
else if (n > 0) { return 1/n + 1/Sumto(n - 1);
You thought n > 0 avoids n / 0 products, but no! Think about the case where n = 1 , which goes through the case n > 0 , but falls into the trap:
1/Sumto(n - 1) 1/Sumto(1 - 1) 1/Sumto(0)
where Sumto(0) returns 0.0 . Hence,
1/0.0
gives Infinity . Also, use 1.0/n instead of 1/n , since this is a floating point separation .
So add another condition like
if(n == 1) return 1;
source share