Article View: fr.comp.lang.c
Article #152Re: decomposition de nombre
From: Lionel Cons
Date: Thu, 27 Aug 1998 00:00
Date: Thu, 27 Aug 1998 00:00
49 lines
1780 bytes
1780 bytes
Huu Chau Nguyen <huuchau.nguyen@hol.fr> writes:
>
> je cherche pour un ami, l'algorithme de d�composition des
> nombres comme dans l'exemple suivant :
>
> ? 5
>
> (1, 4)
> (1, 3, 1)
> (1, 1, 1, 2)
> .....
> (1, 1, 1, 1, 1)
>
> les d�compositions ne sont pas n�cesairement des nombres premiers.
> un programme en code C ou pascal ne serait pas de trop.
En Perl c'est facile:
#!/usr/local/bin/perl -w
sub decomp {
my($n, $i, $d, @d) = @_;
foreach $i (1 .. $n-1) {
foreach $d (decomp($n-$i)) {
next if $i < $d->[0];
push(@d, [$i, @$d]);
}
}
return(@d, [$n]);
}
foreach $d (decomp(5)) {
print "(", join(",", @$d), ")\n";
}
donne:
(1,1,1,1,1)
(2,1,1,1)
(2,2,1)
(3,1,1)
(3,2)
(4,1)
(5)
________________________________________________________
Lionel Cons http://wwwinfo.cern.ch/~cons
CERN http://www.cern.ch
Murphy's 7th Law:
Left to themselves, things tend to go from bad to worse.
Technical Details
Message-ID:
<vfqn28q6gbp.fsf@mercury.cern.ch>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<35DFDF15.8D274F40@hol.fr>Show Full Headers
Complete Article Headers
X-Google-Language: FRENCH,ASCII X-Google-Thread: fff80,2513fb7498cb8f18 X-Google-Attributes: gidfff80,public X-Google-Thread: 10a726,2513fb7498cb8f18 X-Google-Attributes: gid10a726,public X-Google-Thread: f9822,2513fb7498cb8f18 X-Google-Attributes: gidf9822,public From: Lionel Cons <cons@mercury.cern.ch> Subject: Re: decomposition de nombre Date: 1998/08/27 Message-ID: <vfqn28q6gbp.fsf@mercury.cern.ch> X-Deja-AN: 385198823 References: <35DFDF15.8D274F40@hol.fr> X-Complaints-To: news@sunnews.cern.ch X-Trace: sunnews.cern.ch 904202463 1528 (None) 137.138.32.90 Organization: CERN - European Laboratory for Particle Physics Newsgroups: fr.comp.lang.c,fr.comp.lang.pascal,fr.comp.divers