Create a gist now

Instantly share code, notes, and snippets.

Mortgage – SRM 189
public class Mortgage{
public static void main(String[] args){
Mortgage mortgage = new Mortgage();
System.out.println(mortgage.monthlyPayment(1000,50,1));
System.out.println(mortgage.monthlyPayment(2000000000,6000,1));
System.out.println(mortgage.monthlyPayment(1000000,1000000,1000));
System.out.println(mortgage.monthlyPayment(1000000,129,30));
System.out.println(mortgage.monthlyPayment(1999999999,1000000,1));
}
int monthlyPayment(long load, long interest, int term){
long low,high;
term=term*12;
low=load/term;
high=load;
long s=0;
while(low<high){
long mid=low+(high-low)/2;
System.out.printf("%d,%d,%d -> ",low,mid,high);
s=load;
for(int m=0;m<term;m++){
s=s-mid;
if(s<=0) break;
s=(long)Math.ceil(s+(s*(double)interest/12/1000));
}
System.out.printf("%d\n",s);
if(s<=0) high=mid;
else low=mid+1;
}
return (int)Math.ceil(low);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment