Search code examples
rangematchexceptraku

perl6, how to match 1 to 10000 times except prime number of times?


What is the best way to match a string that occurs anywhere from 1 to 10000 times except prime number of times?

say so "xyz" ~~ m/ <[x y z]> ** <[ 1..10000] - [ all prime numbers ]> /

Thanks !!!


Solution

  • Not necessarily the best way (in particular, it will create up to 10_000 submatch objects), but a way:

    $ perl6 -e 'say "$_ ", so <x y z>.roll x $_ ~~ /^ (<[xyz]>) ** 1..10_000 <!{$0.elems.is-prime}> $/ for 1..10'
    1 True
    2 False
    3 False
    4 True
    5 False
    6 True
    7 False
    8 True
    9 True
    10 True
    

    If the substring of interest has fixed length, you could also capture the repetition as a whole and check its length, avoiding submatch creation.