Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Generator of integer partitionts of n

by jdalbec (Deacon)
on Aug 28, 2004 at 15:39 UTC ( #386609=note: print w/ replies, xml ) Need Help??


in reply to Generator of integer partitionts of n

My $0.02:

#! /usr/bin/perl -w use strict; sub partitions { my $n = shift; return partmax($n, $n) }; sub partmax { my ($n, $maxpart) = @_; return [] if $n < 0; return [[]] if $n == 0; my $partitions = []; foreach my $part (reverse 1..$maxpart) { my $subpartitions = partmax($n - $part, $part); foreach (@$subpartitions) { unshift @$_, $part; } push @$partitions, @$subpartitions; } return $partitions; } my $example = partitions shift; print scalar @$example, "\n"; foreach my $partition (@$example) { print join(" ", @$partition), "\n"; }


Comment on Re: Generator of integer partitionts of n
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2014-04-16 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (434 votes), past polls