Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: help with simplifying program

by BillKSmith (Chaplain)
on May 24, 2013 at 14:19 UTC ( #1035147=note: print w/ replies, xml ) Need Help??


in reply to help with simplifying program

Consider replacing the first four loops of the original script with Algorithm::Combinatorics

Bill


Comment on Re: help with simplifying program
Re^2: help with simplifying program
by crunch_this! (Acolyte) on May 24, 2013 at 16:57 UTC

    This is the part of the program it comes from. I thought all the math stuff would be Math::Something so I didn't know about that combinatorics module! That sounds very interesting. I wonder if/how it could be applied here? It runs through all the possible polynomials of a certain form and spits out the ones whose derivatives have (approximately) integer roots.

    # $lep means smallest nonzero root in the interval [0, $rep] # $rep means right endpoint of the interval [0, $rep] my $lep = int 1; my $rep = int 100; foreach my $x ($lep+2 .. $rep ) { foreach my $y ($lep+1 .. $x-1 ) { foreach my $z ($lep .. $y-1 ) { foreach my $s (1..$rep/4) { unless ($y == $x + $s && $z == $y + $s ) { # assigns a truth value to whether or not it is wi +thin 0.0001 of an integer (1=true, 0=false) sub is_approximately_an_integer { my $eps = 0.0001; while( my $w = shift ) { # need to use "round", "int" does not work! return 0 if abs( $w-round($w) ) > $eps; } return 1 } } } push @wants, map { { join(', ', $x, $y, $z) => $_ } } grep { is_approximately_an_integer( @$_ ) } [ poly_roots( poly_derivative( # expanded form of x*(x - $x)*(x - $y)*(x +- $z) 1, -$x - $y - $z, $x*$y + $x*$z + $y*$z, - +$x*$y*$z, 0 ) ) ]; } } }

      You should figure this out yourself!! Read the documentation!!

      use strict; use warnings; use Algorithm::Combinatorics qw(combinations); my $rep = 5; # should be 100 my @data = 0..$rep; my $iter = combinations( \@data, 4 ); while( my $p = $iter->next ) { my ( $z, $y, $x, $w ) = @$p; next unless $w-2*$x+$y or $x-2*$y+$z; print "$w, $x, $y, $z\n"; }
        I've got a bunch of different cases I want to look at so this is only a template. I pasted the stuff with map/grep instead of printing the 4 variables. I can figure out how to modify things it's just the first step that I usually get stuck on. Thx very much for helping anyway.
      Oops, I was slightly wrong. Combinations will only replace three of your loops. You still have to select the applicable combinations.
      use Algorithm::Combinatorics qw(combinations); my $lep = int 1; my $rep = int 100; my $iter = combinations( [$lep+2 ..$rep], 3 ); while (my $c = $iter->next) { my ($x, $y, $z) = @$c; foreach my $s (1..$rep/4) { # as before } }
      Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1035147]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-08-29 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (283 votes), past polls