JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
AutoLoan - SRM 258 | Reply
AutoLoan - SRM 258
==================

I used this code to submit the above problem.

public double interestRate(double price, double monthlyPayment, int loanTerm){
double lo=0;
double hi=100;
double mid=0;
double sum=price;
while(hi-lo>1e-12){
mid=lo +(hi-lo)/2;
double mi=mid*((double)1/12);
double term=loanTerm;
sum=price;
while(term>0){
sum+=(sum*(mi/100));
sum=sum-monthlyPayment;
term--;
}

if(sum>1e-12){
hi=mid;
}else{
lo=mid;
}

}

return hi;
}

if i am giving the condition hi-lo>1e-12 to hi-lo>1e-9 it is faling the system test.(Arg : {1000.0, 500.0, 2})
Please give me your comments on this.
Re: AutoLoan - SRM 258 (response to post by java2007fan) | Reply
1. 1e-9 is just on the border of error, it's bad idea to use it
2. 1e-12 is bad too, if hi could be as big as for example 1e6 or 1e9. Instead, use for loop that will execute 100 or 200 times, that will give you precision without risking TLE.
Re: AutoLoan - SRM 258 (response to post by MB__) | Reply
Thanks for the information.

So the 2nd point applies only if we are dealing with decimal values.
isn't?
Re: AutoLoan - SRM 258 (response to post by java2007fan) | Reply
[ java ] [ /java ] tags are your friends.

public double interestRate(double price, double monthlyPayment, int loanTerm){
	double lo=0;
	double hi=100;
	double mid=0;
	double sum=price;
	while(hi-lo>1e-12){
		mid=lo +(hi-lo)/2;
		double mi=mid*((double)1/12);
		double term=loanTerm;
		sum=price;
		while(term>0){ 
			sum+=(sum*(mi/100));
			sum=sum-monthlyPayment;
			term--;
		}
 
		if(sum>1e-12){
			hi=mid;
		}else{
			lo=mid;
		}
 
	}
 
	return hi;
}
Re: AutoLoan - SRM 258 (response to post by MB__) | Reply
Can you show example how to get better precision with iterations ?

Thanks
Re: AutoLoan - SRM 258 (response to post by java2007fan) | Reply
what does 1e-12 mean ?
Re: AutoLoan - SRM 258 (response to post by Softwalker) | Reply
why we calculate double mi = mid*((double)1/12);
Re: AutoLoan - SRM 258 (response to post by abiradar) | Reply
1e-12 = 10 on power -12 = 0,000000000001
Re: AutoLoan - SRM 258 (response to post by shahid786) | Reply
The problem asks for the annual rate, so we are looking for that between (low, high).
But to calculate the next balance, we need the monthly rate, so we have to divide the annual rate (mid) to 12 (there 12 months in a year).
RSS