2
$\begingroup$

I just started using Mathematica and at the moment I'm learning some things.

As you can see in the headline, I want to make a list of numbers (up to 100) whose digit sum is greater or equal 5.

I started with

Select[Total[IntegerDigits[Range[100]], {2}], GreaterThan[4]]

that leads to the following list:

{5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9, 10, 11, 5, 6, 7, 8, \
9, 10, 11, 12, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 6, 7, 8, 9, 10, 11, \
12, 13, 14, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 8, 9, 10, 11, 12, \
13, 14, 15, 16, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 9, 10, 11, 12, \
13, 14, 15, 16, 17, 18}

So now I have the list of all digit sums meeting my requirement. But how do I use it to list up the numbers which belong to them? I wanted to use it to overwrite the "wrong" numbers with a "nothing". Or am I using a much too difficult way?

Thank you in advance! I hope my English is good enough to at least understand what I want to do

$\endgroup$
  • 3
    $\begingroup$ Select[Range[100], Total@IntegerDigits@# > 4 &] $\endgroup$ – Bob Hanlon Jun 16 at 16:22
  • 1
    $\begingroup$ Yet another way: x /. FindInstance[Mod[x, 10] + Quotient[x, 10] >= 5 && 0 <= x < 100, x, Integers, 100] $\endgroup$ – flinty Jun 16 at 17:15
3
$\begingroup$
With[{ints = Range[100]},
 Pick[ints, Clip[Total[IntegerDigits[ints], {2}], {-∞, 5}], 5]]

Using Nothing:

With[{ints = Range[100]},
 ints /. Thread[PositionIndex[Clip[Total[IntegerDigits[ints], {2}], {4, ∞}]][4] -> Nothing]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks! Is there a way using "nothing" to replace that, too? Or is it much too complicate? $\endgroup$ – Beggi01 Jun 16 at 16:12
  • 1
    $\begingroup$ @Beggi01 Added a nothing version $\endgroup$ – Coolwater Jun 16 at 16:24
  • $\begingroup$ Can't use the option to thank you - so: Thank you very much! $\endgroup$ – Beggi01 Jun 16 at 16:42

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.