discrete and cominatorial math chapter 1_2

Run Settings
LanguageRaku
Language Version
Run Command
#/usr/bin/env perl6 use v6; sub postfix:<!>(Int $N where $N > 0) is tighter(&infix:</>) { factorial( $N ) } "counting arrangement by factorial".say; say q:to/EOF/; EX: 10!/5! = ? EOF say 10! / 5!; "counting permutation".say; say q:to/EOF/; EX: P( 7 , 2 ) EOF say "P( 7 , 2 ) = ",count( ^7 ):p(2) ; say q:to/EOF/; EX: counting the arrangement of MASSASAUGA EOF say "10! / 4! * 3! = ", count('MASSASAUGA'.split('',:skip-empty), :a) ; say q:to/EOF/; EX: There are 6 people at a party sitting at a round table with 6 seats: How many ways can the 6 people be seated? EOF say "we have ", count(^6):cp , " way to sit"; say q:to/EOF/; EX: There are 6 people at a party sitting at a round table with 6 seats: A, B, C, D, E and F. A CANNOT sit next to either D or F. How many ways can the 6 people be seated? need use Inclusion-exclusion principle EOF say "we have ", count(<A B C D E F>,:cp) - ( count(<AD B C E F>,:cp) * 2! + count(<AF B C D E>, :cp) * 2! ) + count(<DAF B C E>, :cp) * 2! , " way to sit"; sub factorial( Int $N where $N > 0 ) { [*] 1..$N; } # p flag = permutation multi sub count(@N where @N.elems > 0, Int :$p! where $p < @N.elems --> Int:D) { Int( @N.elems! / (@N.elems - $p )! ) } # a flag = arrangement multi sub count(@N where @N.elems > 0, Bool :$a! --> Int:D) { Int( @N.elems! / [*] @N.Bag.grep({ $_.value !== 1 }).map({ $_.value! }) ) ; } # cp flag = circular permutation multi sub count(@N where @N.elems > 0, Bool :$cp! --> Int:D) { Int( (@N.elems - 1)! ) }
Editor Settings
Theme
Key bindings
Full width
Lines