/* Program to see if a number can be obtained
by repeatedly adding 5 or multiplying by 3 */
function evaluateCandidate(candidate){
var counter=0;
var operationsPerformed="";
while(candidate>1) {
if(candidate%3==0) {
operationsPerformed+=(++counter)+". candidate/3\n";
candidate=candidate/3;
}else{
operationsPerformed+=(++counter)+". candidate-5\n";
candidate=candidate-5;
}
}
if(candidate==1) {
return operationsPerformed;
}else{
return "Invalid candidate";
}
}
evaluateCandidate(24);
Monday, April 13, 2015
Javascript: Program to see if a given number can be reached by repeatedly adding 5 or multiplying by 3
Subscribe to:
Posts (Atom)