Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Can this be explained in layman's terms?

by huck (Prior)
on Jan 14, 2017 at 08:19 UTC ( [id://1179561]=note: print w/replies, xml ) Need Help??


in reply to Re: Can this be explained in layman's terms?
in thread [Answered; thanks.] Can this be explained in layman's terms?

Sometimes the rule S(n,k) = 0 if k>n is given for completeness

VERY important if you dont realize n elements ... k non-empty subsets and you just start with

for $n (0..10) { for $k (0..10) { print "$n $k '.fs($n,$k)."\n"; } }

use strict; use warnings; my $l='k '; $l.=sprintf "%3d ",0; for my $k (0..10){ $l.=sprintf " %10s",$k; } print $l."\n"; $l=~s/./-/g; print $l."\n"; for my $n (0..10){ printf "n %3d ",$n; for my $k (0..10){ printf " %10s",fs($n,$k); } print "\n"; } sub fs { my $n=shift; my $k=shift; if ( $n ==0 && $k == 0 ) {return 1}; if ( $k > $n ) {return 0}; # important if ( $n > 0 && $k == 0 ) {return 0} if ( $k == 1 ) {return 1} if ($n == $k ) {return 1} my $p1=fs($n-1,$k-1); my $p2=fs($n-1,$k ); return $p1 + ($k*$p2); }
And i learned never to name my subroutines s
use strict; use warnings; my $l='k '; $l.=sprintf "%3d ",0; for my $k (0..10){ $l.=sprintf " %10s",$k; } print $l."\n"; $l=~s/./-/g; print $l."\n"; for my $n (0..10){ printf "n %3d ",$n; for my $k (0..10){ printf " %10s",s($n,$k); } print "\n"; } sub s { my $n=shift; my $k=shift; if ( $n ==0 && $k == 0 ) {return 1}; if ( $k > $n ) {return 0}; # important if ( $n > 0 && $k == 0 ) {return 0} if ( $k == 1 ) {return 1} if ($n == $k ) {return 1} my $p1=s($n-1,$k-1); my $p2=s($n-1,$k ); return $p1 + ($k*$p2); }
output
Global symbol "$p2" requires explicit package name at buk-2.pl line 30 +. syntax error at buk-2.pl line 31, near "return" (Might be a runaway multi-line ;; string starting on line 29) Global symbol "$p1" requires explicit package name at buk-2.pl line 31 +. Global symbol "$p2" requires explicit package name at buk-2.pl line 31 +. Missing right curly or square bracket at buk-2.pl line 32, at end of l +ine syntax error at buk-2.pl line 32, at EOF Execution of buk-2.pl aborted due to compilation errors.

Replies are listed 'Best First'.
Re^3: Can this be explained in layman's terms?
by NetWallah (Canon) on Jan 14, 2017 at 18:54 UTC
       ...And i learned never to name my subroutines s

    I wish I could give you more ++ for that !

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

Re^3: Can this be explained in layman's terms?
by LanX (Saint) on Jan 20, 2017 at 15:15 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-24 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found