Mr. Scrooge has a sum of money 'P' that he wants to invest. Before he does, he wants to know how many years 'Y' this sum 'P' has to be kept in the bank in order for it to amount to a desired sum of money 'D'.

The sum is kept for 'Y' years in the bank where interest 'I' is paid yearly. After paying taxes 'T' for the year the new sum is re-invested.

Note to Tax: not the invested principal is taxed, but only the year's accrued interest

Example:

      Let P be the Principal = 1000.00      
      Let I be the Interest Rate = 0.05      
      Let T be the Tax Rate = 0.18      
      Let D be the Desired Sum = 1100.00


    After 1st Year -->
      P = 1041.00
    After 2nd Year -->
      P = 1083.86
    After 3rd Year -->
      P = 1128.30
  
Thus Mr. Scrooge has to wait for 3 years for the initial principal to amount to the desired sum.
 
Your task is to complete the method provided and return the number of years 'Y' as a whole in order for Mr. Scrooge to get the desired sum.  

Assumption: Assume that Desired Principal 'D' is always greater than the initial principal. However it is best to take into consideration that if Desired Principal 'D' is equal to Principal 'P' this should return 0 Years.

Difficulty: EASY
Tags: Fundamentals

Starter code:
def calculate_years(principal, interest, tax, desired):
	

Solutions:
def calculate_years(principal, interest, tax, desired):
	years = 0
	while principal < desired:
		principal += interest * principal * (1 - tax)
		years += 1
	return years


from math import ceil, log

def calculate_years(principal, interest, tax, desired):
	if principal >= desired:
		return 0
	return ceil(log(float(desired) / principal, 1 + interest * (1 - tax)))


def calculate_years(principal, interest, tax, desired):
	years = 0
	if principal == desired:
		return years
	while principal < desired:
		x = principal * interest
		y = x * tax
		principal = principal + x - y
		years += 1
	return years


def calculate_years(principal, interest, tax, desired):
	year = 0
	while principal < desired:
		increment = principal * interest * (1 - tax)
		principal = principal + increment
		year = year + 1
	return year


import math

def calculate_years(principal, interest, tax, desired):
	return 0 if principal >= desired else math.ceil((math.log(desired) - math.log(principal)) / math.log(1 + interest * (1 - tax)))


def calculate_years(p, i, t, d, n=0):
	if p >= d:
		return n
	p = p + p * i * (1 - t)
	return calculate_years(p, i, t, d, n + 1)


def calculate_years(principal, interest, tax, desired):
	return 0 if principal >= desired else 1 + calculate_years(principal * (1 + interest * (1 - tax)), interest, tax, desired)


calculate_years = lambda p, i, t, d, n=0: calculate_years(p + p * i * (1 - t), i, t, d, n + 1) if p < d else n


from math import ceil, log

def calculate_years(P, I, T, D):
	return ceil((log(D) - log(P)) / log(1 + I * (1 - T)))


def calculate_years(principal, interest, tax, desired):
	y = 0
	while principal < desired:
		extra = principal * interest
		principal += extra - tax * extra
		y += 1
	return y


from math import ceil, log

def calculate_years(p, i, t, d):
	x = float(d) / p
	base = 1 + i * (1 - t)
	return ceil(log(x, base))


def calculate_years(principal, interest, tax, desired):
	x = 0
	while principal < desired:
		add = principal * interest
		add = add - add * tax
		principal = principal + add
		x = x + 1
	return x


import math

def calculate_years(principal, interest, tax, desired):
	x = float(desired) / float(principal)
	base = 1 + float(interest) * (1 - float(tax))
	years = math.log(x, base)
	return math.ceil(years)


def calculate_years(sum, intr, tax, goal):
	(year, fix) = (0, intr * (1 - tax) + 1)
	while sum < goal:
		year += 1
		sum *= fix
	return year


def calculate_years(principal, interest, tax, desired):
	if desired == principal:
		return 0
	else:
		years = 0
		while principal < desired:
			principal += principal * interest - principal * interest * tax
			years += 1
		return years


def calculate_years(principal, interest, tax, desired):
	year = 0
	while principal < desired:
		realint = principal * interest
		realtax = realint * tax
		principal = principal + realint - realtax
		year += 1
	return year


def calculate_years(principal, interest, tax, desired):
	if principal == desired:
		return 0
	p = principal
	y = 0
	fixed = 1 + interest * (1 - tax)
	while p < desired:
		p *= fixed
		y += 1
	return y


def calculate_years(principal, interest, tax, desired):
	year = 0
	while principal < desired:
		year = year + 1
		interest_beforetax = interest * principal
		principal = principal + (1 - tax) * interest_beforetax
	return year


calculate_years = lambda principal, interest, tax, desired, count=0: count if principal >= desired else calculate_years(principal * (1 + interest * (1 - tax)), interest, tax, desired, count + 1)


def calculate_years(p, i, t, d):
	y = 0
	while p < d:
		p = p + p * i - p * i * t
		y += 1
	return y


def calculate_years(p, i, t, d):
	years = 0
	while p < d:
		p = p * (1 + i) - p * i * t
		years += 1
	return years


import math

def calculate_years(principal, interest, tax, desired):
	year = 0
	if int(principal) >= int(desired):
		return year
	while principal <= desired:
		principal = principal + principal * interest - principal * interest * tax
		year += 1
		if int(principal) >= int(desired):
			return year


def calculate_years(principal, interest, tax, desired):
	years = 0
	gain = 0
	taxed = 0
	while principal < desired:
		years = years + 1
		gain = principal * interest
		taxed = gain * tax
		principal = principal + gain - taxed
	return years
	raise NotImplementedError('TODO: calculate_years')


def calculate_years(principal, interest, tax, desired, years=0):
	if principal >= desired:
		return years
	principal += principal * interest * (1.0 - tax)
	return calculate_years(principal, interest, tax, desired, years + 1)


def calculate_years(principal, interest, tax, desired):
	if principal >= desired:
		return 0
	else:
		gain = principal * interest
		taxed = gain * tax
		new_sum = principal + gain - taxed
		return 1 + calculate_years(new_sum, interest, tax, desired)


def calculate_years(principal, interest, tax, desired):
	years = 0
	if desired <= principal:
		return 0
	total_summ = principal
	while total_summ <= desired:
		summ_year = total_summ * interest
		summ_tax = summ_year * tax
		total_summ += summ_year - summ_tax
		print(total_summ)
		years += 1
		if total_summ >= desired:
			return years


def calculate_years(P, I, T, D):
	total = []
	while P < D:
		x = P * I
		y = x * T
		z = P + (x - y)
		total.append(z)
		P = z
	return len(total)


def calculate_years(principal, interest, tax, desired):
	years_required = 0
	current = principal
	while current < desired:
		current += current * interest * (1 - tax)
		years_required += 1
	return years_required


def calculate_years(p, i, t, d):
	c = 0
	while p < d:
		p += p * i + p * i * -t
		c += 1
	return c


def calculate_years(principal, interest, tax, desired):
	year_counter = 0
	while principal < desired:
		year_counter += 1
		principal += principal * interest * (1 - tax)
	return year_counter


def calculate_years(principal, interest, tax, desired):
	current = principal
	years = 0
	while current < desired:
		currentstep = current * (1 + interest)
		taxloss = (currentstep - current) * tax
		current = currentstep - taxloss
		years += 1
	return years


def calculate_years(principal, interest, tax, desired):
	import math
	count = 1
	compounding = principal * interest * (1 - tax) + principal
	if desired == principal:
		return 0
	while compounding < desired:
		compounding = compounding * interest * (1 - tax) + compounding
		count += 1
	return count


def calculate_years(principal, interest, tax, desired):
	years = 0
	if principal >= desired:
		return 0
	while True:
		yearly_interest = principal * interest
		principal = principal + yearly_interest - yearly_interest * tax
		years += 1
		if principal >= desired:
			break
	return years


def calculate_years(prin, inter, tax, desired):
	yrs = 0
	while prin < desired:
		prin = prin * (1 + inter * (1 - tax))
		yrs += 1
	return yrs


import math

def calculate_years(principal, interest, tax, desired):
	effInt = interest * (1 - tax)
	timeFloat = math.log(desired / principal) / math.log(1 + effInt)
	if desired > principal:
		return int(timeFloat) + 1
	else:
		return 0


def calculate_years(principal, interest, tax, desired):
	year_count = 0
	while principal < desired:
		principal += interest * principal * (1 - tax)
		year_count += 1
	return year_count