4
$\begingroup$

I have a number $n$ such that the digits of $n$ are strictly increasing to the left except for the first digit. So for example when $n=51369$ fits the bill because:

$$1<3<6<9\tag1$$

Is there a way to write Mathematica code that checks if the number $n$ satisfy this criterion?

$\endgroup$

3 Answers 3

11
$\begingroup$
f1 = OrderedQ @* Rest @* IntegerDigits;

f1 /@ {51369, 51396}
{True, False}
f2 = Apply[LessEqual @ ##2 &] @* IntegerDigits;

f2 /@ {51369, 51396}
{True, False}
$\endgroup$
5
$\begingroup$
f = AllTrue[Rest[Differences[IntegerDigits[#]]], Positive] &

Test:

f /@ {51369, 412345, 824699, 41395, 31832}

True, True, False, False, False}


EDIT Visually,

alist = Range[1000];
blist = (Boole /@
     f /@ alist) /. {{} -> Black, 0 -> Red, 1 -> Darker@Green} // 
  Multicolumn

enter image description here

$\endgroup$
3
$\begingroup$

A slight variation on the method given by @kglr

51369//IntegerDigits[#,10,IntegerLength[#]-1]&//OrderedQ
(* True *) 
$\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.