# 20171221 java basic learning - compound programming exercises

[Q]
Let's say your monthly income is 3000 yuan. Apart from the usual expenses, you can leave 1000 yuan for investment every month. Then you carefully studied "stock and fund 21 days from entry to Mastery", achieving an annual return of 20%.
So the question is, how many years of continuous investment at the pace of 1000 yuan per month, with a total income of 1 million yuan. (compound interest is calculated based on 12000 investment per year instead of monthly interest)
Compound interest formula: F = p * ((1 + r) ^ n); F final income, p principal, r annual interest rate, and how many years have n been deposited.
[A]

```public class Millionaire {
static int p = 12000;
static float r = 0.20f;
static int year = 0;
static float num = 0.0f;
public static void main(String[] args) {
System.out.println("Method 1: year as node");
Way_1();
System.out.println("Method 2: using the formula of equal ratio sequence∑Sn");
Way_2();
System.out.println("Method 3: Based on the cumulative calculation of each investment ");
Way_3();
}

static void Way_1(){
//  Method 1 ← Recommended Practice
for(;num < 1000000;year++){
num = (num + p) * (1 + r);/*Calculate the compound interest of Σ year with the year as the node */
System.out.println("year = "+year+", num = "+num);
System.out.println("");
}

static void Way_2() {
for(float m = 1.0f;num < 1000000.0;year++){
num = p * (1 + r) * (1 - (1 + r) * m) / (-r);//The formula of sum of the first n terms の is obtained by using the sequence of equal proportions
m = m * (1 + r);
}
System.out.println("year = "+year+", num = "+num);
System.out.println();
}

static void Way_3(){
for(float m = 1.0f;num < 1000000;year++){
num = num+ p * (1 + r) * m;/*Based on the cumulative calculation of each investment */
m = m * (1 + r);
}
System.out.println("year = "+year+", num = "+num);
System.out.println();
}
}
/*
* Conclusion:
Investment for one year, gain = 14400.00057220459
Investment for 2 years, income = 31679.99782562324
Investment for 3 years, gain = 52416.00139617934
Investment 4 years, gain = 77299.20568084664
Investment for 5 years, gain = 107159.04395599371
Investment 6 years, gain = 142990.86018585076
Investment 7 years, gain = 185989.0396616792
Investment 8 years, gain = 237586.8515994465
Investment for 9 years, gain = 299504.2396576747
Investment for 10 years, gain = 373805.1053275485
11 years of investment, income = 462966.1441313971
Investment for 12 years, income = 569959.3975624691
Investment for 13 years, gain = 698351.3016797554
Investment for 14 years, income = 852421.614086314
Investment for 15 years, gain = 1037305.9615083694
Continuous investment for 15 years, with total income of 100W
*/```

summary

The difference between the above codes lies in the different calculation methods. One is calculated by year [i.e. method 1], the other is calculated by each investment [i.e. method 2, method 3].

So can we say that different methods [or different solutions] are algorithms?

Posted on Sat, 02 May 2020 21:56:51 -0700 by derekm