7
$\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$

5 Answers 5

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

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

f2 /@ {51369, 51396}
{True, False}
$\endgroup$
7
$\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$
4
$\begingroup$

A slight variation on the method given by @kglr

51369//IntegerDigits[#,10,IntegerLength[#]-1]&//OrderedQ
(* True *) 
$\endgroup$
3
$\begingroup$

Using SequenceCount

f = SequenceCount[Rest @ IntegerDigits[#], x_ /; LessEqual @@ x] == 1 &;

f /@ {51369, 51396}

{True, False}

$\endgroup$
2
$\begingroup$

Using Split:

f = Length@Split[Rest@IntegerDigits[#], LessEqual] == 1 &;

f /@ {51369, 51396}

(*{True, False}*)
$\endgroup$

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.