Honesty Rocks! truth rules.

The Problem With # Algorithm problem

HOME      >>       Programming

PureHeart

Given A,K, the definition

A#B = ( Sum of all digits of A * Maximum digit of B )+Minimum digit of B

 

and following formulas

A

A#A

(A#A)#A

(A#(A#A))#A

 

Determine and return the minimal number of necessary # to generate K from A or return -1 if there is no possible solution at all.

 

I am still looking for a solution to this. Any idea?


pyost

AA#A
(A#A)#A
(A#(A#A))#A


Each line must have a greater value than the previous one, so we just need to use the formula until the value becomes K, or exceeds it.

value = acounter = 0while (value < k) value = calculate(value, a) counter = counter + 1if (value == k) return counterelse return -1

"calculate" whould be the function you use to calculate result of the # operator. This is quite easy to achieve, as you can take out the digits from a number by using "% 10 (mod 10)", which gives you the remainder when dividing by 10 (the last digit).