Problem:
The task is to minimize the value of the fraction x/f(x) where x is any 3-digit number and f(x) is its digits sum. What is the 3-digit number that satisfies these conditions?
Answer: 199
Solved using the script below
console.log("*************************************")
var min=100;
for(var i=0; i<1000; i++){
if(i>=100){
var temp = i.toString();
var hun = parseInt(temp[0]);
var ten = parseInt(temp[1]);
var one = parseInt(temp[2]);
var digsum = hun+ten+one;
var frac = i/digsum;
if(frac<min){
console.log(i+" "+digsum+" "+frac);
min=frac;
}
}
}
console.log("min is "+min);
No comments:
Post a Comment