Combinatorics programs for the RPN calculator on the Palm Pilot

Combinatorics programs for the RPN calculator on the Palm Pilot

This is a program set I wrote for the RPN calculator that runs on the Palm Pilot. It implements factorial, permutations, combinations, Stirling numbers of the first kind, Stirling numbers of the second kind, and a program that will factor an integer.

This can be pasted into the Memo application and from there added to the RPN calculator.

// Factorial, permutations,
// combinations, stirling #s 2nd kind,
// factoring numbers.
//
// ! (factrial) returns the factorial of
// the number in the X register after
// taking the absolute value and then
// truncating it to a whole number.
//
// Pyx (permutations) returns
// permutations of y taken x at a
//  time.  This has been extended
// for negative numbers as
// suggested in Knuth's book
// Concrete Mathematics.  It is:
// (y)*(y-1)*...*(y-x+1).  If x is 0
// then 1 is returned by definition.
// If x is negative, then 0 is
// returned.
//
// Cyx (combinations) returns the
// number of ways of selecting x
// items out of y where order
// doesn't matter.  It too has been
// extended like Pyx.  If x is
// negative then 0 is returned, but
// if y is negative, then Pyx/x! is
// returned.
//
// S1yx (Stirling numbers of the
// first kind).  This is the number
// of ways to partition y items into
// x rings.  This is computationally
// intensive to calculate.  It can
// handle any pair of whole
// whole number values where
// y-x < 26, but cases where x is
// fairly large and y is quite a bit
// larger take a very long time.
// Beware of large values.
//
// S2yx (Stirling numbers of the
// second kind) This is the number
// of ways to partition y items into
// x sets.
//
// Factors.  This function takes
// the absolute value and then the
// integer portion of the number
// entered.  It then decomposes
// it into all of its prime factors
// which are placed on the stack.
//
// By Truman Collins
// (tcollins@teleport.com)
// 1/98

RPN.1.z
[f]
 Vv2<(1:v{_vv2<(B)v*});
[p]
 g10<(d1d10:g11<(d1d11:g2r2-
 #'.5'+XaVv{v1-Vvxa<(B)v*})));
[c]
 g10<(d1d10:g20>(g2f0=9
 (g2g2>(g2g2-g1g3>
 (d1:d2))))g1Cfk3Cpr2/);
[z]
 xzV1{vx@*_vv1<(B)}+;

"Combinatorics"
"_!: Factorial"
 ?1wbCf;
"Pyx: Permutations.  y items\taken x at a time when the\order matters."
 ?2wCp;
"Cyx: Combination.  y items\taken x at a time when the\order does not matter."
 ?2wCc;
~
"S1yx: Stirling #s of 1st kind.\# of ways y items can be parti-\tioned into x rings."
 ?2g2g2<(d1d10;)g2g2-g10=9(d1d1
 d11;)g1#'24'>(d1d1d1E.)Xzd1Xyxz
 V{vvX@_vv1<(B)}xyxz1+X@
 xzV0{vxz>(B)v1<(Czv1+Vvx@1+
 vX@:vx@v1+x@=9(vvX@v1+
 Vvx@1+vX@:_v))};
"S2yx: Stirling #s of 2nd kind.\# of ways y items can be parti-\tioned into x non-empty sets."
 ?2wr2wr2g1XcVXb1Xa0
 {xavxbP*vCf/xcv-Cf/+
 xanXa_vv1<(B)};
~
"Factors: Factors an integer\and places all of its prime\factors on the stack."
 ?1g1swXaXb2V
 {vxa>(B)xbv%0=9
 (vxbv/#'.0000001'+wg1XbswXa:
 vv2=9(1:2)+V)}xb;

13,594 visits (1 today, 3 this week, 9 this since December 27, 1998.

Back to my homepage.

Copyright 1998 by Truman Collins
For comments, email: Truman Collins (truman@tkcs-collins.com)
Most recent update: January 5, 2005
http://www.tkcs-collins.com/truman/rpn/rpn_comb.shtml