Determining occurrence of a sequence of numbers in the first 50,000 primes

9

1

I have to determine how many of the first 50,000 prime numbers (digits) contain the sequence 5, 4, 3, in that order. The numbers don't have to necessarily be consecutive. For example, 566453 is a prime number that would count. All I've been able to do is determine the first 50,000 prime numbers.

Claire

Posted 2020-03-11T23:19:57.277

Reputation: 151

Answers

13

out = Select[MatchQ[IntegerDigits[#], {___, 5, ___, 4, ___, 3, ___}] &]@
   Prime[Range[50000]];

Length@out

1588

Short@out

{1543, 2543, 5413, 5431, 5437, 5443, 5483, 5743, 5843, 8543, <<1568>>, 599243, 599413, 599843, 601543, 602543, 605413, 605443, 605543, 610543, 611543}

If you need just the count:

count = Count[{___, 5, ___, 4, ___, 3, ___}]@*IntegerDigits@*Prime@*Range;
count @ 50000

1588

kglr

Posted 2020-03-11T23:19:57.277

Reputation: 302 076

11

Prime and IntegerDigits are Listable, ___ (i.e. BlankNullSequence) is a pattern object that can stand for any sequence of zero or more expressions.

This is a nice approach and quite fast:

Length @ Cases[ IntegerDigits @ Prime @ Range @ 50000,
                          {___, 5, ___, 4, ___, 3, ___}]
1588

Remarks

This metod and kglr's operator approach based on Count seem to be equally efficient. However a reliable estimation of efficiency in case of large sets of primes should be carefully compared (with refreshing the kernels) since the algorithms behind Prime use sparse caching and sieving, to gain an insight I recommend to think over this question What is so special about Prime?. Instead of Length one might exploit CountDistinct in case of duplicates presence.

Artes

Posted 2020-03-11T23:19:57.277

Reputation: 51 831

8

This should be speedier...

Pick[p = Prime@Range@5*^4, StringMatchQ[IntegerString[p], "*5*4*3*"]]

ciao

Posted 2020-03-11T23:19:57.277

Reputation: 23 752

6

Prime[Range[50000]] // Short
IntegerDigits /@ % // Short
Flatten[SequenceCases[#, {___, 5, ___, 4, ___, 3, ___}] & /@ %, 1] // Length

(* 1588 *)

If you want the actual primes, replace the last line by

FromDigits /@ Flatten[SequenceCases[#, {___, 5, ___, 4, ___, 3, ___}] & /@ %, 1]
(* {1543, 2543, 5413, 5431, 5437, 5443, 5483, 5743, 5843, ..., 602543, 605413, 605443, 605543, 610543, 611543} *)

AccidentalFourierTransform

Posted 2020-03-11T23:19:57.277

Reputation: 9 042