Skip to content

Instantly share code, notes, and snippets.

@IvanaGyro
Created July 12, 2023 15:09
Show Gist options
  • Save IvanaGyro/d975eb77935d949eb75f727b8830ec8f to your computer and use it in GitHub Desktop.
Save IvanaGyro/d975eb77935d949eb75f727b8830ec8f to your computer and use it in GitHub Desktop.
Calculate how much money to deposit in a fixed deposit account in order to earn the maximum amount of interest after rounding up or down.
import math
def actual_final(month, rate, save):
current = save
for _ in range(month):
current = round(current * (1 + rate / 100 / 12))
return current
def ideal_final(month, rate, save):
return save * (1 + rate / 100 / 12)**month
def get_most_money(month, rate, start, end):
cur_max = -math.inf
best_begin = None
best_final = None
for save in range(start, end):
actual = actual_final(month, rate, save)
ideal = ideal_final(month, rate, save)
diff = actual - ideal
if diff > cur_max:
cur_max = diff
best_begin = save
best_final = actual
print(f'Save {best_begin} will get more {cur_max} and total {best_final}')
get_most_money(12, 1.58, 8000, 15000)
get_most_money(12, 1.58, 25000, 35000)
get_most_money(12, 1.58, 45000, 55000)
get_most_money(12, 1.58, 65000, 75000)
get_most_money(12, 1.58, 85000, 95000)
get_most_money(12, 1.58, 105000, 115000)
get_most_money(12, 1.58, 135000, 141000)
get_most_money(12, 1.58, 1, 2000000)
# Save 8735 will get more 4.983156386773771 and total 8879
# Save 25444 will get more 3.06072479737486 and total 25852
# Save 54344 will get more 1.1194791852103663 and total 55210
# Save 71773 will get more 1.738303760488634 and total 72917
# Save 85444 will get more 1.1654051873483695 and total 86805
# Save 114309 will get more 1.7811818449554266 and total 116130
# Save 140914 will get more 1.3646822078735568 and total 143158
# Save 380 will get more 5.952329642469863 and total 392
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment