perl - multiple numbers combination -


using perl, how create possible combinations of numbers range 1..20, combination can contain set of numbers 1 15 numbers @ time.

to exemplify:

list case elements can contain possible combinations of 1 number @ time in range 1..20:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 

example of list case elements can contain possible combinations of 2 numbers @ time in range 1..20:

1 2, 1 3, 1 4, 1 5, ..., 2 3, 2 4, ..., 19 20 

example of list case elements can contain possible combinations of 3 numbers @ time in range 1..20:

1 2 3, 1 2 4, ..., 2 3 4, 2 3 5, ..., 18 19 20 

example of list case elements can contain possible combinations of 5 numbers @ time in range 1..20:

1 2 3 4 5, 1 2 3 4 6, ..., 2 3 4 5 6, 2 3 4 5 7, ..., 15 16 17 18 19 20 

thanks in advance advice.

you need algorithm::combinatorics

use strict; use warnings;  use algorithm::combinatorics qw(combinations);  (combinations([1..5], 3)) {     print "@$_\n"; } 

math::combinatorics can job, interface isn't intuitive. can better large return sets though, returns iterator instead of of combinations @ once.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -