-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps1c.py
executable file
·59 lines (37 loc) · 1.22 KB
/
ps1c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 13 13:15:46 2021
@author: riya
"""
annual_salary = float(input("Enter your annual_salary: "))
total_cost = 1000000
semi_annual_raise = 0.07
annual_return = 0.04
portion_down_payment = 0.25 * total_cost
low = 0
high = 10000
rate = (low + high) // 2
epilson = 100
numGuesses = 0
current_savings = 0
while abs(current_savings - portion_down_payment) >= epilson and (high - low) > 10:
starting_salary = annual_salary
months = 0
current_savings = 0
while months <= 36:
current_savings += ((current_savings * annual_return) / 12) + ((starting_salary / 12) * (rate / 10000.0))
months += 1
if months % 6 == 0:
starting_salary += (starting_salary * semi_annual_raise)
if current_savings > portion_down_payment:
high = rate
else:
low = rate
rate = (high + low) // 2
numGuesses += 1
if abs(current_savings - portion_down_payment) < epilson:
print("Best saving rate: ", rate/10000.0)
print("Steps in bisection: ", numGuesses)
else:
print("It is not possible to pay the down payment in three years.")