From mark at ecn.purdue.edu Tue Aug 6 07:29:27 2019 From: mark at ecn.purdue.edu (Mark Senn) Date: Tue, 06 Aug 2019 10:29:27 -0400 Subject: [Purdue-pm] Perl Challenge 018 In-Reply-To: <9ccb15ef-8cbe-932b-82be-fd3fb4e2b661@purdue.edu> References: <3377.1564371201@pier.ecn.purdue.edu> <34380.1564411060@pier.ecn.purdue.edu> <9ccb15ef-8cbe-932b-82be-fd3fb4e2b661@purdue.edu> Message-ID: <35706.1565101767@pier.ecn.purdue.edu> Dear Mark Daniel Ward, I've heard of Project Euler but haven't worked on any problems there. Damian Conway wrote in http://blogs.perl.org/users/damian_conway/2019/07/chopping-substrings.html The ?best practice? solution is a technique known as suffix trees, which requires some moderately complex coding. However, we can get very reasonable performance for strings up to hundreds of thousands of characters long using a much simpler approach. Damian's entire Perl 6 program to find the longest common substring for strings given on the command line is sub MAIN (*@strings) { .say for max :all, :by{.chars}, keys [?] @strings?.match(/.+/, :ex)?.Str } He discusses larger problems there and lists code for an O(N log N) algorithm. (I have not tried that code.) Perl Weekly Challenge - 020, Task #1 is Write a script to accept a string from command line and split it on change of character. For example, if the string is ?ABBCDEEF?, then it should split like ?A?, ?BB?, ?C?, ?D?, ?EE?, ?F?. I'm going to try and solve this in Perl 6 using the comb command. I think that will make it easy---but I haven't tried it yet. Regards, Mark Senn >Dear Mark, > >Aha!? I understand your motivation.? Thanks for sharing! > >I wonder if you ever worked on the Project Euler problems? > >https://projecteuler.net/ > >I think that these problems require "efficient" solutions, i.e., they >will have some problems for which it is necessary to have small space >requirements and/or fast execution time.? I think that there is a >1-minute execution rule on the site.? This prevents some brute-force >solutions, to some of the harder problems. > >I'm guessing that you know about this site.... but just curious! > >Warmest regards, > >Mark > > >On 7/29/19 10:37 AM, Mark Senn wrote: >>> I might be misreading your email, but I think you are suggesting in Task >>> #1 to use a brute force solution.? If you touch all substrings for a >>> word, then you will take a length of time that is exponential in the >>> length of the word.? In contrast, however, there is actually a solution >>> that is linear in terms of the length of the words. >>> >>> https://en.wikipedia.org/wiki/Longest_common_substring_problem >>> >>> The suffix tree solution -- which yields this optimal solution -- is >>> really beautiful in its own right, and it doesn't need to be a bunch of >>> complicated code.? It can be done easily, especially if you have a >>> library with a suffix tree included.? I don't think this is >>> over-engineering.? I think it gets to the heart of the solution. >> Hi Mark, >> >> Yes, I suggested using a brute force solution to find the longest common >> substring of "ABABC", "BABCA", and "ABCBA". The challenge didn't say >> anything about any space restraints, time constraints, or if the >> solution would be usedd for bigger problemms. The challenge used to >> include the critieria of only use Perl 5 or Perl 6 but no extra modules >> so I didn't use anything not built-in. (I don't see that criteria >> listed on the web page now.) I was trying to get a solution that was >> easy to understand and worked with as little time investment as >> possible. Human time is more expensive than computer time. The >> solution worked using Perl 6 stuff I already knew. I agree with you, if >> the problem was bigger and needed to be solved to be solved using less >> computer time or space I should have used a different method. (At a >> recent Perl Mongers meeting I think Joe Kline may have used the phrase >> "conserving a limitless resource" in connection with there is an >> abundance of computer time and space to work on small problems.) >> >> -mark >_______________________________________________ >Purdue-pm mailing list >Purdue-pm at pm.org >https://mail.pm.org/mailman/listinfo/purdue-pm From mark at purdue.edu Thu Aug 8 20:23:13 2019 From: mark at purdue.edu (Mark Senn) Date: Thu, 08 Aug 2019 23:23:13 -0400 Subject: [Purdue-pm] comb not needed Message-ID: <10036.1565320993@pier.ecn.purdue.edu> Purdue Perl Mongers, Earlier I wrote I'd try using the Perl 6 comb routine for the Perl Weekly Challenge - 020, Task 1. See https://perlweeklychallenge.org/blog/perl-weekly-challenge-020/index.html#task-1 for more information. That's not needed. A Perl 6 regex (formerly known as a regular expression in Perl 5) can be used to do the matching. The design of Perl 6 is so much nicer than Perl 5---it's a pleasure to use. -mark From jacoby.david at gmail.com Mon Aug 12 11:32:31 2019 From: jacoby.david at gmail.com (Dave Jacoby) Date: Mon, 12 Aug 2019 14:32:31 -0400 Subject: [Purdue-pm] Computing Euler's Number (Perl Weekly Challenge 21) Message-ID: > Write a script to calculate the value of e, also known as Euler?s number and Napier?s constant. Which is 1 + ( 1 / $n ) )**$n , where $n approaches infinity. It is easy to put that in a subroutine compute_euler ( $n ), and then increment $n until you blow off the top of Perl 5's integer. I think the closest you could get without getting into Math::BigFloat would be 2.71828182845904 , and I'm not seeing clear enough documentation to really work on it. I suspect that $x = Math::BigFloat->new( 1 + ( 1 / $n ) )**$n ) wouldn't work as intended, not because of problems with Math::Float, but because Perl 5 can't handle the equation within the new() well enough to give a Math::BigFloat result. Meanwhile, another site says: > But it is known to over 1 trillion digits of accuracy! So, when is it "done"? When we get to billions of digits? The 50 digits listed on the wiki page? compute_euler( 2 ** 53 ) to get the largest int Perl can handle without BigInt? I think this question is underspecified. Thoughts? Suggestions? -- Dave Jacoby jacoby.david at gmail.com I deal with my software the way I treat my eldritch abomination: It's not human, it's not even alive in the natural sense. It's nightmare-born and nightmare-shaped, and nightmares don't die easy. -- @yenzie -------------- next part -------------- An HTML attachment was scrubbed... URL: From mark at purdue.edu Mon Aug 12 12:51:12 2019 From: mark at purdue.edu (Mark Senn) Date: Mon, 12 Aug 2019 15:51:12 -0400 Subject: [Purdue-pm] Computing Euler's Number (Perl Weekly Challenge 21) In-Reply-To: References: Message-ID: <19456.1565639472@pier.ecn.purdue.edu> >> Write a script to calculate the value of e, also known as Euler?s number >> and Napier?s constant. > >Which is 1 + ( 1 / $n ) )**$n , where $n approaches infinity. > >It is easy to put that in a subroutine compute_euler ( $n ), and then >increment $n until you blow off the top of Perl 5's integer. > >I think the closest you could get without getting into Math::BigFloat would >be 2.71828182845904 , and I'm not seeing clear enough documentation to >really work on it. I suspect that $x = Math::BigFloat->new( 1 + ( 1 / $n ) >)**$n ) wouldn't work as intended, not because of problems with >Math::Float, but because Perl 5 can't handle the equation within the new() >well enough to give a Math::BigFloat result. > >Meanwhile, another site says: > >> But it is known to over 1 trillion digits of accuracy! > >So, when is it "done"? When we get to billions of digits? The 50 digits >listed on the wiki page? compute_euler( 2 ** 53 ) to get the largest int >Perl can handle without BigInt? > >I think this question is underspecified. Thoughts? Suggestions? >-- >Dave Jacoby In TeX notation e = \sum_{n=0}^\infty {1\over n!} = 1/1 + 1/1 + 1/(1*2) + 1/(1*2*3) + ... according to https://en.wikipedia.org/wiki/E_(mathematical_constant) (I'll be doing this in Pel 6 using sums of fractions where each fraction and the sum are FatRats (Fat Rationals). From https://docs.perl6.org/type/FatRat: A FatRat is a rational number stored with arbitrary size numerator and denominator. Arithmetic operations involving a FatRat and optionally Int or Rat objects return a FatRat, avoiding loss of precision. Perl 6 has "sequences" built in that make dealing with mathematical series easier and more elegant. Like pi, e is never "done"..."it just keeps going and going". The very loose specification in Perl Weekly Challenge 21: Write a script to calculate the value of e, also known as Euler?s number and Napier?s constant. doesn't spec how many significant digits. So I just make up something I'm interested in computing and documenting that. The lack of tight specifications for the problems make it impossible to automatically compare solutions objectively. On the other hand, I think that's fine---I'm more interested in seeing the different ways people choose to solve the problems instead of how numerically precise the solutions are. Damian Conway's answers are almost always the best in my opinion. -mark From djacoby at purdue.edu Fri Aug 16 08:27:20 2019 From: djacoby at purdue.edu (Dave Jacoby) Date: Fri, 16 Aug 2019 11:27:20 -0400 Subject: [Purdue-pm] September, October, November Message-ID: <4d8fda7c-8ce8-bb8d-5f32-d4b3a2bde490@purdue.edu> December we have planned in advance. We're doing "Starship Mongers", because "Starship Mongers" gives everyone a chance to talk about what they like, and keeps people from having to plan and make slides during peak holiday season. But we don't have September. Or October. Or November. Because automation, they're in Meetup, but they're listed as TBA. So, what subjects are we interested in hearing about? If there's great interest in one thing or another, we can recruit someone to talk about it. -- Dave Jacoby Developer, Purdue Genomics Core Lab Code Runs Everything Around Me. When I say "reload the page", I always mean with Ctrl-Shft-R, which clears the cache. From mark at purdue.edu Sat Aug 24 17:27:57 2019 From: mark at purdue.edu (Mark Senn) Date: Sat, 24 Aug 2019 20:27:57 -0400 Subject: [Purdue-pm] sexy primes, etc. Message-ID: <43917.1566692877@pier.ecn.purdue.edu> Perl Weekly Challenge 022, Task #1: Write a script to print first 10 Sexy Prime Pairs. Sexy primes are prime numbers that differ from each other by 6. For example, the numbers 5 and 11 are both sexy primes, because 11 - 5 = 6. The term ?sexy prime? is a pun stemming from the Latin word for six: sex. See https://engineering.purdue.edu/~mark/pwc-022-1.pdf for my solution using a circular buffer of two elements using Perl 6. A description of why only two elements are needed is my blog entry. Some neat Perl 6 stuff: # This is a loop that generates primes one at a time ((2.. Inf).grep ({.is -prime })) { # Is the numeric variable $var is in the array @a one or more times? # There is also "one" for occurs one time, and "none" for zero times. if ($var == @a.any) { if ($a %% $b) { # do following block if $a mod $b is zero if ($a < $b < $c) { # is $a < $b and $b < $c? is ([<=] @array) { # is @array sorted numerically say ([+] @array) / @array.elems; # print average of @array One think I wish Perl 6 did (Wolfram Language (Mathematica) does it): scalar times array = array 2 * (1,2) gives (2,4) in a world I want t live in But one can overload all built in operators depending on their types to do different things. I think this can be done easily in Perl but a separate definition would need to be done for + - * / etc. -mark