Writing a program that divide numbers until I hit a one digit number
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]
foo[500, 2]
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}