4
$\begingroup$

I am relatively new to Mathematica and I would like to program a function taking 2 variables in and giving 1 output. I think the best way to describe the function is like so: Given a base 10 number x and a second number b, convert x to a "polynomial base b", if you will and take the derivative of the polynomial. Then evaluate it at b.

I will illustrate what I mean with 2 examples: F(37,5) would be calculated as 121 (base 5) then d(1*5^2+2*5+1)/d(5) if you excuse the notation for "derivative with respect to 5" using power rule. The "derivative with respect to 5" would be 2*5+2 which is 12. Therefore F(37,5)=12.

Example 2: F(988,4): 988 is 33130 in base 4, d(3*4^4+3*4^3+4^2+3*4)/d(4) = 12*4^3+9*4^2+2*4+3 = 923.

$\endgroup$
3
$\begingroup$
f[n_, b_] := Module[{dig},
  dig = IntegerDigits[n, b];
  D[dig.Table[x^i, {i, Length@dig - 1, 0, -1}], x] /. x -> b
  ]

Maybe a different example for illustration:

f[2016, 28]

128

$\endgroup$
4
$\begingroup$

For your particular problem:

D[IntegerDigits[37, 5] {b^2, b, 1}, b] /. {b -> 5} // Total
12

In general you can create a function:

f[num_, base_] := Module[{}, dig = IntegerDigits[num, base]; len = Length[dig]; 
        D[dig Reverse[b^Range[0, len - 1] ], b] /. {b -> base} // Total]

So that f[37,5] returns 12 and f[988,4] is 923.

$\endgroup$
3
$\begingroup$
db[n_Integer, b_Integer] := 
 FromDigits[(# Range[Length@#, 1, -1]) &@ Most@IntegerDigits[n, b], b]

or

db[n_Integer, b_Integer] :=
 Total@MapIndexed[ ( #2[[1]] - 1 )   #   b^(#2[[1]] - 2) & , 
   Reverse@IntegerDigits[n, b]]

db[37, 5]
db[988, 4]

12

923

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.