
-----------------------------------
cool dude
Sun Dec 03, 2006 4:42 pm

recursion
-----------------------------------
i have to make a program that using recursion to solve this problem. the problem is to determine the time it would take for your investment to double. The user will enter the initial deposit, the percent rate (interest), and the compound period i.e. every year. 

the formula for compound interest is: 

Total = Principal   x   ( 1 + Rate )years

using this formula i keep calculating the total and adding a year everytime it calculates the total unil the total is equal to twice my intial investment.

EXAMPLE
Deposite: $5000
rate: 9%
compounded annually

Answer
It will take 8.0432 years for your investment to double. 

I do not understand why my code isn't working. here is my function where i believe the error lies in. the rest of my code is not needed since its only asking the user for deposit, rate, and compound.


	public static double Calculate (double deposit, double rate, int compound, double years, double goal)
	{	
		//investment = 0 thus no investment	
		if (deposit == 0)
		{
			return years;
		}
		//doesn't reach goal of doubling investment yet
		if (deposit < goal)
		{
			double total = deposit * Math.pow((1 + rate), compound);
			years = years + (1 / compound);
			return Calculate(total, rate, compound, years, goal);
		}
		//reached goal of doubling investment, return time it took
		else
		{
			return years;
		}
	}

-----------------------------------
ericfourfour
Sun Dec 03, 2006 5:03 pm


-----------------------------------
years = years + (1 / compound);
I have not ran your program but I don't think you should be assigning values to parameters. Instead use:
return Calculate(total, rate, compound, years + (1 / compound), goal);

-----------------------------------
OneOffDriveByPoster
Sun Dec 03, 2006 5:31 pm

Re: recursion
-----------------------------------
I sincerely doubt that you are being asked to provide an answer that is not a multiple of the compound period.  Also, is the rate an APR?

-----------------------------------
cool dude
Sun Dec 03, 2006 6:13 pm


-----------------------------------
ahh i see what the problem is. at year 8 its not exactly doubled its only $9962.813208. Thus when it doubles it says its year 9 although it doubles between year 8 and 9. So how do i find the exact month? it should be 8.0432 years. in case you want to run the program here is a sample test.


public class Investment
{
	public static void main (String [] args) throws IOException
	{
       System.out.println("\nThe Time it will take for investment to double is " + Calculate(5000, 0.09, 1, 0, 5450));
	}
	
	public static double Calculate (double deposit, double rate, int compound, double years, double goal)
	{	
		//investment = 0 thus no investment	
		if (deposit == 0)
		{
			return years;
		}
		//doesn't reach goal of doubling investment yet
		if (deposit < goal)
		{
			double total = deposit * Math.pow((1 + rate), compound);
			return Calculate(total, rate, compound, years + (1 / compound), goal);
		}
		//reached goal of doubling investment, return time it took
		else
		{
			return years;
		}
	}
}

-----------------------------------
wtd
Sun Dec 03, 2006 10:36 pm


-----------------------------------
      if (deposit == 0)
      {
         return years;
      }
      //doesn't reach goal of doubling investment yet
      if (deposit < goal)
      {
         double total = deposit * Math.pow((1 + rate), compound);
         return Calculate(total, rate, compound, years + (1 / compound), goal);
      }
      //reached goal of doubling investment, return time it took
      else
      {
         return years;
      } 

Small detail here...

      if (deposit == 0)
      {
         return years;
      }
      //doesn't reach goal of doubling investment yet
      else if (deposit < goal)
      {
         double total = deposit * Math.pow((1 + rate), compound);
         return Calculate(total, rate, compound, years + (1 / compound), goal);
      }
      //reached goal of doubling investment, return time it took
      else
      {
         return years;
      } 

Though, if we desire to get fancy about it...

return (deposit < goal) ? Calculate(deposit * Math.pow(1 + rate, compound), rate, compound, years + 1 / compound, goal) : years;

-----------------------------------
cool dude
Sun Dec 03, 2006 11:03 pm


-----------------------------------
i rearanged the formula to be able to calculate for years by isolating years.


	public static double Calculate (double deposit, double rate, double goal)
	{	
		double years = 0;
		//investment = 0 thus no investment	
		if (deposit == 0)
		{
			return years;
		}
			years = Math.log(goal/deposit)/Math.log(1+rate);
			return years;
	}

it works very well now. but small problem. when i have interest compounded semi annualy (2 times) what i do is divide my rate by 2 and send it as the parameter, thus it should work. however i get the wrong result or atleast i think its wrong. 

Sample Input
deposit: $5000
rate: 9%
commpounded: 2 (semi annually)

output
years:  7.8737

however the output i get is 15.74

-----------------------------------
ericfourfour
Sun Dec 03, 2006 11:07 pm


-----------------------------------
Or even fancier...
return (deposit != 0  && (deposit < goal)) ? Calculate(deposit * Math.pow(1 + rate, compound), rate, compound, years + 1 / compound, goal) : years;
(removes need for first if)

-----------------------------------
cool dude
Sun Dec 03, 2006 11:21 pm


-----------------------------------
is my new way of rearanging the equation still using recursion?

-----------------------------------
zylum
Sun Dec 03, 2006 11:23 pm


-----------------------------------
try binary search:


fcn calculate (principal, rate, total) 
	binSearch(principal, rate, total, 0, maxValue) 
end calculate

fcn binSearch (p, r, t,  l,  u) 
		
	if (abs(u - l) < epsilon) return l // where epsilon is how accurate 
				//you want your answer. try 1e-5 for example.
		
	mid = (l+u)/2;
		
	if (t > p*(1 + r)^mid) return binSearch(p, t, r, mid, u);
	else  return binSearch (p, t, r, l, mid);	

end binSearch

-----------------------------------
OneOffDriveByPoster
Sun Dec 03, 2006 11:36 pm


-----------------------------------
i rearanged the formula to be able to calculate for years by isolating years.
Doesn't look recursive anymore though.
Answer is probably okay:

deposit : $
rate : percent per unit time
compound per unit time

output : time in units time

=====

deposit : $5000.00
rate : 9% per year
compounded per 0.5 years

=====

deposit : $5000.00
rate : 4.5% per 0.5 years
compounded per 0.5 years

output : 15.7473 (0.5 years)


-----------------------------------
zylum
Sun Dec 03, 2006 11:53 pm


-----------------------------------
if you divide the rate by two, then it gives you the result as if it were still compounded anually, thus you should divide your output by 2.

-----------------------------------
cool dude
Mon Dec 04, 2006 3:05 pm


-----------------------------------
ha i just figured that out too zylum. before i saw your post.  :) 
The point of this assignment is to use recursion to solve the problem so i'm not allowed to use binary search. Anyhow since this isn't using recursion i will get a zero so any ideas on how to make it recursive?

-----------------------------------
zylum
Mon Dec 04, 2006 4:27 pm


-----------------------------------
:lol: binary search is recursive. take a closer look at my pseudocode...

-----------------------------------
cool dude
Mon Dec 04, 2006 4:33 pm


-----------------------------------
:lol: binary search is recursive. take a closer look at my pseudocode...

yes i just found that out. I will be learning about binary search in a few days so i will know how to do it. sorry for my ignorance :(

-----------------------------------
cool dude
Mon Dec 04, 2006 5:39 pm


-----------------------------------
This is annoying! I have to make a fibonacci program using recursion (Not allowed to use loops). i'm supposed to allow the user to enter the number of terms they want to view and then display all of them. 

Here is how i made my program using recursion except for one problem. The problem is that it returns the value of the term itself and not of all the proceeding terms.


import java.io.*;

public class Fibonacci
{
	public static void main (String [] args) throws Exception
	{
		int terms = 0;
		int ans = 0;
		
		BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
		
		System.out.println("Enter number of terms to display");
		terms = Integer.parseInt(myInput.readLine());
		
		System.out.println("\n" + terms + " terms of Fibonacci numbers are:");
		System.out.println(Fib(terms));
	}
	
	public static int Fib(int n)
	{
	   	if (n 1)
        {
            test(z-1);
        }
        return 0;
    }
 
 
    public static int Fib (int n)
    {
 
        if (n  (start + 1))
        {
            return test(z,start + 1);
        }
        return 0;
    }
 
 
    public static int Fib (int n)
    {
 
        if (n < 2)
        {
           //System.out.println (1);
            return 1;
        }
        else
        {
            //System.out.println (Fib (n - 1) + Fib (n - 2));
            return Fib (n - 1) + Fib (n - 2);
        }
    }
}

-----------------------------------
gsquare567
Tue Dec 05, 2006 6:38 pm


-----------------------------------
im not 100% sure, but i think after an int reaches its max value, it goes to the negatives where it is at its lowest point, and your method goes on infinitaly until that, so it eventually returns 0 once it reaches its max since u keep adding 1 to the variable.

-----------------------------------
cool dude
Tue Dec 05, 2006 9:31 pm


-----------------------------------
im not 100% sure, but i think after an int reaches its max value, it goes to the negatives where it is at its lowest point, and your method goes on infinitaly until that, so it eventually returns 0 once it reaches its max since u keep adding 1 to the variable.

nope not at all! read my post i actually told you where my mistake is. i just don't know how to fix it.

-----------------------------------
OneOffDriveByPoster
Wed Dec 06, 2006 12:44 am


-----------------------------------

        System.out.println (test (terms, start));

Interesting, no?

-----------------------------------
cool dude
Wed Dec 06, 2006 3:29 pm


-----------------------------------

        System.out.println (test (terms, start));

Interesting, no?

hmm. not sure?

-----------------------------------
Andy
Wed Dec 06, 2006 4:51 pm


-----------------------------------
he was pointing out the vagueness of your variable names

-----------------------------------
cool dude
Wed Dec 06, 2006 5:16 pm


-----------------------------------
he was pointing out the vagueness of your variable names

ahh yes. the variable names are pretty bad. i'm going to be edditting a lot of the code format and putting in comments. i was just stuck on this problem so i kinda started making test methods and not caring about the format which i know is wrong to do.  :(
