5
$\begingroup$

Well, I have the following question:

How can I write a program such that an input number $n$ is divided by the number $k$ as long as the resulting number does not have one digit, if the resulting number is not an integer we need to take the floor-function of the fraction. Then I need to count how many divisions it took to do that.

Example:

When I have the number $n=100$ and $k=2$. Now divide the number by $2$ to get $n/k=50$, that is not a one digit number so we divide by $2$ again and get $n/(2k)=25$, still not a one digit number so divide by $2$ again $n/(3k)=12.5$ which is not an integer so we need to take the floor function $\lfloor12.5\rfloor=12$ now divide by $2$ again and get $6$ which is a one digit number so we stop. The number of divisions is: $4$.

$\endgroup$
4
  • $\begingroup$ What have you tried so far? $\endgroup$
    – yarchik
    Commented Jun 11, 2020 at 9:55
  • 1
    $\begingroup$ @yarchik I have no clue where to start. $\endgroup$ Commented Jun 11, 2020 at 10:21
  • 1
    $\begingroup$ If I'm not mistaken, the number of divisions required is always $d = \lfloor \log_k (n/10) \rfloor + 1$. This can be proven by noting that all integers between $10, ... 10k - 1$ require one division by $k$ to be reduced to one digit, all integers between $10k, ... 10k^2 - 1$ require two divisions to be reduced to one digit, and in general all integers between $10 k^{d-1}, ... 10 k^d - 1$ require $d$ divisions to be reduced to one digit. (This can be proved formally by induction.) But this is more of mathematical proof than a program, and the question specifically requested a program. $\endgroup$ Commented Jun 11, 2020 at 20:12
  • $\begingroup$ So basically the question is how to write a program that calculates the logarithm. $\endgroup$
    – M. Stern
    Commented Jun 13, 2020 at 14:43

3 Answers 3

7
$\begingroup$

One way might be

NestWhileList[Floor[#/2] &, 100, Length[IntegerDigits[Floor[#]]] > 1 &]

(* {100, 50, 25, 12, 6} *)

NestWhileList[Floor[#/2] &, 500,Length[IntegerDigits[Floor[#]]] > 1 &]

(* {500, 250, 125, 62, 31, 15, 7} *)

Or if you prefer to code it yourself

foo[n_Integer, k_Integer] := Module[{z},
   If[k == 0, Return["Error k=0", Module]];
   If[n == 0, Print[0]; Return[Null, Module]];
   If[k == 1, Print[n]; Return[Null, Module]];
   If[Abs[n] < Abs[k], Print[n]; Return[Null, Module]];
   If[n == k, Print[1]; Return[Null, Module]];
   Print[n];
   z = n/k;

   If[Length[IntegerDigits[Floor[z]]] == 1,
    Print[Floor[z]];
    Return[Null, Module]
    ,
    foo[Floor[z], k]
    ];
   ];

   foo[100, 2]

Mathematica graphics

   foo[500, 2]

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ Nice procedural approach ;-) ... good programmers write FORTRAN (BASIC, Java, C...) code in every language $\endgroup$
    – mgamer
    Commented Jun 11, 2020 at 10:50
  • $\begingroup$ Is it Ok that foo[1,2] yields 1, 0 ? $\endgroup$
    – yarchik
    Commented Jun 11, 2020 at 11:00
  • 1
    $\begingroup$ @yarchik I thought I had check for n<k, I must forgot it. Added now. Thanks. The procedural version was just for fun. I would use the Mathematica build in version myself. $\endgroup$
    – Nasser
    Commented Jun 11, 2020 at 11:05
  • $\begingroup$ NestWhileList[Floor[#/2] &, 100, # > 9 &] is sufficient. $\endgroup$ Commented May 2 at 17:54
4
$\begingroup$

You can also use

FixedPointList

ClearAll[quotients1]

quotients1[n_, k_] := Most @ 
   FixedPointList[If[IntegerLength[#] > 1, Quotient[#, k], #] &, n]

Examples:

quotients1[100, 2]
 {100, 50, 25, 12, 6}
quotients1[950, 3]
 {950, 316, 105, 35, 11, 3}

ReplaceRepeated

ClearAll[quotients2]

quotients2[n_, k_] := {n} //. 
   {a___, b_} /; IntegerLength[b] > 1 :> {a, b,  Quotient[b, k]}

Examples:

quotients2[100, 2]
 {100, 50, 25, 12, 6}
quotients2[950, 3]
 {950, 316, 105, 35, 11, 3}
$\endgroup$
2
$\begingroup$
g[x_ /; x <= 9] := x
g[x_] := Floor[x/2]
                     // Thanks to @eldo

 

Most@FixedPointList[g,500]

{500, 250, 125, 62, 31, 15, 7}

Original Answer

g[x_ /; x <= 9] := x
g[x_ /; x > 9] := Floor[x/2]
$\endgroup$
1
  • 2
    $\begingroup$ +1, Very nice! You don't even need the redundant /; x > 9 $\endgroup$
    – eldo
    Commented May 2 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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