Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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 exploiting the Monastery: (8)
As of 2016-02-10 07:04 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (335 votes), past polls