<?xml version="1.0" encoding="windows-1252"?>
<node id="386531" title="Generator of integer partitionts of n" created="2004-08-27 21:39:15" updated="2005-07-01 22:31:13">
<type id="115">
perlquestion</type>
<author id="351915">
chiburashka</author>
<data>
<field name="doctext">
Dear monasterians,&lt;br&gt;
i've built a generator of integer partitions of n which gives you all the sets of nonzero integers that add up to exactly n.&lt;p&gt;
the code (i'm terribly sorry about the names of the variables. and there are few bugs that you'll be able to see as you run it):&lt;br&gt;
&lt;code&gt;#!/usr/bin/perl -w
print 'enter n : ';
$kk = $n = $nn = readline(*STDIN);
my @ready = @aa = ();

while ($n) {$aa[$n] = 1;$n--;}
@bb = @aa;


while ($kk) {
$aa[$nn - $kk + 1] += $aa[$nn - $kk];
$aa[$nn - $kk]='';
	$ready[($nn - $kk)*3] = "@aa\n";
$aa[$kk - 1] += $aa[$kk];
$aa[$kk]='';
	$ready[($nn - $kk)*3 + 1] = "@aa\n";
$aa[$nn - $kk + 1] += $aa[$nn - $kk];
$aa[$nn - $kk]='';
	$ready[($nn - $kk)*3 + 2] = "@bb\n";
$bb[$kk - 1] += $bb[$kk];
$bb[$kk]='';
$kk--;}

print @ready;&lt;/code&gt;
&lt;br&gt;so my "questions" is if there's a way to make a few improvments ?</field>
</data>
</node>
