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

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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2015-03-05 06:51 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (136 votes), past polls