From mark at purdue.edu Mon Jun 11 04:57:53 2018 From: mark at purdue.edu (Mark Senn) Date: Mon, 11 Jun 2018 07:57:53 -0400 Subject: [Purdue-pm] Perl 6 get primes Message-ID: <4739.1528718273@pier.ecn.purdue.edu> From https://twitter.com/nogoodnickleft/status/1001477056985747456 here is how to get the first 1000 primes in Perl 6: (1..*).grep(&is-prime).head(1000); I read this to myself as For 1 to whatever if the number is prime save it to a list and use the first 1000 primes on that list. NOTE: The is-prime routine reportedly uses the Miller-Rabin primality test which relies on the unproven extended Riemann hypothesis. Perl6 computes these in a lazy fashion so they are only computed as they are used (you can also read this is "as they are needed"). The idea is don't take the time to compute all of them and then use use one or more of them at a time---instead compute the ones you need, use them, compute more, use those, etc. -mark From mark at purdue.edu Wed Jun 13 07:17:05 2018 From: mark at purdue.edu (Mark Senn) Date: Wed, 13 Jun 2018 10:17:05 -0400 Subject: [Purdue-pm] Perl 6 regexes Message-ID: <43045.1528899425@pier.ecn.purdue.edu> Perl 5 has regular expressions that can be used to match character strings. These have been MUCH improved and are known as regexes in Perl 6. Below is a message I sent to the Perl6 users mailing list about how to remove leading zeros. Thought you might be interested in it, especially how to use name captures in the last example. To: Norman Gaywood cc: perl6-users Subject: Re: How do I remove leading zeros? Date: Wed, 13 Jun 2018 10:02:43 -0400 From: Mark Senn If \$x is set to "01.02.03" (Not using normal message quoting to make message shorter. [1] ToddAndMargo [2] Norman Gaywood [3] Mark Senn) DOING \$x ~~ s:global/"0"(\d)/ \$0 /; [1] SETS \$x TO "1 . 2 . 3" [1] COMMENT Spaces on right hand side of s command are significant. [2] DOING \$x ~~ s:global/"0"(\d)/\$0/; [2] SETS \$x to "1.2.3" [2] COMMENT Zeroes would de deleted from "101.102.103". [3] (I suspect the general case is delete leading zeroes or zeroes immediately following periods.) DOING \$x ~~ s:g/(^|".")0+/\$0/; [3] or, using named captures, \$x ~~ s:g/\$=(^|".")0+/\$/; SETS \$x to "1.2.3" [3] COMMENT Zeroes would not be deleted from "101.102.103". [3] For me, the last solution is the clearest: replace all beginning of strings or "." followed by one or more zeroes by the beginning of the string or ".", whichever was matched. -mark