# Board index » delphi » could someone help me? (mostly math problem)

## could someone help me? (mostly math problem)

I'm trying to write a D2 app and need it to list every combination of a
series of letters (or numbers).

A formula that could do strings as well as integers would be nice, but not
necessary.  If it could take a number/string of any length would also be
nice, but i could do that myself once i figure out the formula.

example:
if you started with ABCD, then i want:
ABCD
ABDC
ACBD
ACDB
etc.

i don't care what order the letters are in as long as i get all of them

I know there must be a formula for this, but i can't seem to figure it out.

BTW there should be 120 different combonations of 4 letters (4! = 120).
--

Eli{*word*106}inson
Lightning Computer Techologies, Inc.
el...@nyc.pipeline.com
http://www.nethosting.com/~eli/

## Re:could someone help me? (mostly math problem)

##### Quote
el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:
>BTW there should be 120 different combonations of 4 letters (4! = 120).

Sorry, I have no algorithm for you, but perhaps a little help
with your math: 4! = 24 not 120.

This will probably help in the debuging process ;-)

Best regards,
Frank
--
// Frank Mikalsen, System Developer, Finale a.s
// Homepage: http://home.sol.no/frankm
// Author of ShareWare: Silent Partner Backup Screensaver v2.60

## Re:could someone help me? (mostly math problem)

In article <52a2p4\$...@news1.t1.usa.pipeline.com>,
el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:
]-I'm trying to write a D2 app and need it to list every combination of a
]-series of letters (or numbers).
]-
]-A formula that could do strings as well as integers would be nice, but not
]-necessary.  If it could take a number/string of any length would also be
]-nice, but i could do that myself once i figure out the formula.
]-
]-example:
]-  if you started with ABCD, then i want:
]-  ABCD
]-  ABDC
]-  ACBD
]-  ACDB
]-  etc.
]-
]-i don't care what order the letters are in as long as i get all of them
]-
]-I know there must be a formula for this, but i can't seem to figure it out.
]-
]-BTW there should be 120 different combonations of 4 letters (4! = 120).

to SWAG.  in the math section there are several code snippets
for obtaining all the permutations of a set of [integers,
characters, whatever...]

if you want a thoroughly documented solution, check Chapter 6,
Section 5 of "Scientific Pascal" by Harley Flanders
( http://www.birkhauser.com/cgi-win/ISBN/0-8176-3760-5 )

Mark Vaughan

## Re:could someone help me? (mostly math problem)

##### Quote
Frank Mikalsen wrote:

> el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:

> >BTW there should be 120 different combonations of 4 letters (4! = 120).

> Sorry, I have no algorithm for you, but perhaps a little help
> with your math: 4! = 24 not 120.

> This will probably help in the debuging process ;-)

> Best regards,
> Frank
> --
> // Frank Mikalsen, System Developer, Finale a.s
> // Homepage: http://home.sol.no/frankm
> // Author of ShareWare: Silent Partner Backup Screensaver v2.60

Frank,
Where did you learn about factorials?

4 * 1 =  4
4 * 2 =  8
4 * 3 = 12
4 * 4 = 16
--
120

Lawrence C. Thurman Jr.

## Re:could someone help me? (mostly math problem)

##### Quote
Eli{*word*106}inson wrote:

> I'm trying to write a D2 app and need it to list every combination of a
> series of letters (or numbers).

> A formula that could do strings as well as integers would be nice, but not
> necessary.  If it could take a number/string of any length would also be
> nice, but i could do that myself once i figure out the formula.

> example:
>   if you started with ABCD, then i want:
>   ABCD
>   ABDC
>   ACBD
>   ACDB
>   etc.

Hmm. The possible permutations of ABCD fall into 4 categories:
A followed by all possible permutations of BCD,
B followed by all possible permutations of ACD,
C followed by all possible permutations of ABD,
D followed by all possible permutations of ABC.

Seems like some sort of recursive gizmo is called for - the exact
syntax would depend on what sort of format you want for output. (And then
it might or might not be worthwhile to unwrap the recursion into a loop.)

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

## Re:could someone help me? (mostly math problem)

CFC <cfis...@mail.idt.net> wrote in article <3249722A.4...@mail.idt.net>...

##### Quote
>Frank said (essentially):
>> 4! = 24
> Frank,
>     Where did you learn about factorials?

>       4 * 1 =  4
>       4 * 2 =  8
>       4 * 3 = 12
>       4 * 4 = 16
>               --
>               120

> Lawrence C. Thurman Jr.

Same place most people, I'd guess!

x! = x * (x-1) * (x-2) * ... * 1

ergo,

4! = 4 * 3 * 2 * 1 = 24

Verify with the Calculator that comes with Windows if you don't believe me!

Where did *you* learn about factorials?  ;)

-------------
Steve Neumann
sneum...@bridge.com

## Re:could someone help me? (mostly math problem)

On Sep 25, 1996 15:11:18 in article <Re: could someone help me? (mostly
math problem)>, '"Steven C. Neumann" <sneum...@bridge.com>' wrote:

##### Quote
>>> 4! = 24
>> Frank,
>>     Where did you learn about factorials?

>>       4 * 1 =  4
>>       4 * 2 =  8
>>       4 * 3 = 12
>>       4 * 4 = 16
>>               --
>>               120

>> Lawrence C. Thurman Jr.

>Same place most people, I'd guess!

>x! = x * (x-1) * (x-2) * ... * 1

>ergo,

>4! = 4 * 3 * 2 * 1 = 24

>Verify with the Calculator that comes with Windows if you don't believe
me!

>Where did *you* learn about factorials?  ;)

Steve is right everybody, 4! = 24. I was thinking of 5!, not 4!
my mistake :-)
--

Eli{*word*106}inson
Lightning Computer Techologies, Inc.
el...@nyc.pipeline.com
http://www.nethosting.com/~eli/

## Re:could someone help me? (mostly math problem)

##### Quote
el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:
>I'm trying to write a D2 app and need it to list every combination of a
>series of letters (or numbers).

>A formula that could do strings as well as integers would be nice, but not
>necessary.  If it could take a number/string of any length would also be
>nice, but i could do that myself once i figure out the formula.

>example:
>  if you started with ABCD, then i want:
>  ABCD
>  ABDC
>  ACBD
>  ACDB
>  etc.

>i don't care what order the letters are in as long as i get all of them

>I know there must be a formula for this, but i can't seem to figure it out.
>BTW there should be 120 different combonations of 4 letters (4! = 120).

Hi,

Sorry, but 4! = 24.

I didn't give this much thought. There is probably a
nice recursive algorithm but anyway...

Here is one possible way:
If you generate all possibilities with the first letter
ABCD
ABDC
ACBD
ACDB
Then all you have to do is rotate each of these 3 (n-1) times.
ABCD->BCDA->CDAB->DABC
ABDC->BDCA->DCAB->CABD
ACBD->CBDA->BDAC->DACB
...

Good Luck,
Al

## Re:could someone help me? (mostly math problem)

.

##### Quote
CFC wrote:

>
> Frank Mikalsen wrote:
> >
> > el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:
> >
> > >BTW there should be 120 different combonations of 4 letters (4! =
120).
> >
> > Sorry, I have no algorithm for you, but perhaps a little help
> > with your math: 4! = 24 not 120.
> >
> > This will probably help in the debuging process ;-)
> >
> > Best regards,
> > Frank
> > --
> > // Frank Mikalsen, System Developer, Finale a.s
> > // Homepage: http://home.sol.no/frankm
> > // Author of ShareWare: Silent Partner Backup Screensaver v2.60
>

##### Quote
> Frank,
>     Where did you learn about factorials?

>       4 * 1 =  4
>       4 * 2 =  8
>       4 * 3 = 12
>       4 * 4 = 16
>               --
>               120

> Lawrence C. Thurman Jr.

Actually Frank probably learnt his factorials at a considerably better
school than you did.

A factorial is the product of numbers from 1 to n. So 4! = 24 and 5! =
120.

--
The reason people get lost in thought...
is because its unfamiliar territory.

## Re:could someone help me? (mostly math problem)

##### Quote
Al Hall wrote:

> el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:

> >I'm trying to write a D2 app and need it to list every combination of a
> >series of letters (or numbers).

> >A formula that could do strings as well as integers would be nice, but not
> >necessary.  If it could take a number/string of any length would also be
> >nice, but i could do that myself once i figure out the formula.

> >example:
> >  if you started with ABCD, then i want:
> >  ABCD
> >  ABDC
> >  ACBD
> >  ACDB
> >  etc.

> >i don't care what order the letters are in as long as i get all of them

> >I know there must be a formula for this, but i can't seem to figure it out.

> >BTW there should be 120 different combonations of 4 letters (4! = 120).

Hello,

Here is a little Pascal function I made up that will return the Nth
combination for an original string of any length.  Calling it with an
increasing N value until you get your original string back will yield
all possible combinations.

Function Shuffle(S:String; N:Longint):String
Var
p,r,j:integer;
C:char;
begin
for p:=lenght(S) downto 2 do
begin
r:= N mod p;
N:= N div p;
for j:= p-r to p-1 do
begin
C:=S[j];
S[j]:=S[j+1];
S[j+1]:=C;
end;
end;
Shuffle:=S;
end;

To use the function call it several times with your original string and
with an increasing N value.

Here is an example calling loop:

S0:='ABCDE';  {S0 and S1 are strings.  SO is the original}
N:=0;
repeat
begin
N:=N+1;
S1:=shuffle(S0,N);
end
until S1=S0;
ListBox1.Sorted:=true;  {This makes it easyer to check}

Hope this helps...

BTW feel free to submit more little problems like this.
That one was fun.

Alain Toutant.

## Re:could someone help me? (mostly math problem)

Hello,

Here is a little Pascal function I made up that will return the Nth
combination for an original string of any length.  Calling it with an
increasing N value until you get your original string back will yield
all possible combinations.

Function Shuffle(S:String; N:Longint):String
Var
p,r,j:integer;
C:char;
begin
for p:=lenght(S) downto 2 do
begin
r:= N mod p;
N:= N div p;
for j:= p-r to p-1 do
begin
C:=S[j];
S[j]:=S[j+1];
S[j+1]:=C;
end;
end;
Shuffle:=S;
end;

To use the function call it several times with your original string and
with an increasing N value.

Here is an example calling loop:

S0:='ABCDE';  {S0 and S1 are strings.  SO is the original}
N:=0;
repeat
begin
N:=N+1;
S1:=shuffle(S0,N);
end
until S1=S0;
ListBox1.Sorted:=true;  {This makes it easyer to check}

Hope this helps...

BTW feel free to submit more little problems like this.
That one was fun.

Alain Toutant.

## Re:could someone help me? (mostly math problem)

##### Quote
Al Hall wrote:

> el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:

> >I'm trying to write a D2 app and need it to list every combination of a
> >series of letters (or numbers).

> >A formula that could do strings as well as integers would be nice, but not
> >necessary.  If it could take a number/string of any length would also be
> >nice, but i could do that myself once i figure out the formula.

> >example:
> >  if you started with ABCD, then i want:
> >  ABCD
> >  ABDC
> >  ACBD
> >  ACDB
> >  etc.

> >i don't care what order the letters are in as long as i get all of them

> >I know there must be a formula for this, but i can't seem to figure it out.

> >BTW there should be 120 different combonations of 4 letters (4! = 120).

Hello,

Here is a little Pascal function I made up that will return the Nth
combination for an original string of any length.  Calling it with an
increasing N value until you get your original string back will yield
all possible combinations.

Function Shuffle(S:String; N:Longint):String
Var
p,r,j:integer;
C:char;
begin
for p:=lenght(S) downto 2 do
begin
r:= N mod p;
N:= N div p;
for j:= p-r to p-1 do
begin
C:=S[j];
S[j]:=S[j+1];
S[j+1]:=C;
end;
end;
Shuffle:=S;
end;

To use the function call it several times with your original string and
with an increasing N value.

Here is an example calling loop:

S0:='ABCDE';  {S0 and S1 are strings.  SO is the original}
N:=0;
repeat
begin
N:=N+1;
S1:=shuffle(S0,N);
end
until S1=S0;
ListBox1.Sorted:=true;  {This makes it easyer to check}

Hope this helps...

BTW feel free to submit more little problems like this.
That one was fun.

Alain Toutant.

## Re:could someone help me? (mostly math problem)

##### Quote
Al Hall wrote:

> el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:

> >I'm trying to write a D2 app and need it to list every combination of a
> >series of letters (or numbers).

> >A formula that could do strings as well as integers would be nice, but not
> >necessary.  If it could take a number/string of any length would also be
> >nice, but i could do that myself once i figure out the formula.

> >example:
> >  if you started with ABCD, then i want:
> >  ABCD
> >  ABDC
> >  ACBD
> >  ACDB
> >  etc.

> >i don't care what order the letters are in as long as i get all of them

> >I know there must be a formula for this, but i can't seem to figure it out.

> >BTW there should be 120 different combonations of 4 letters (4! = 120).

Hello,

Here is a little Pascal function I made up that will return the Nth
combination for an original string of any length.  Calling it with an
increasing N value until you get your original string back will yield
all possible combinations.

Function Shuffle(S:String; N:Longint):String
Var
p,r,j:integer;
C:char;
begin
for p:=lenght(S) downto 2 do
begin
r:= N mod p;
N:= N div p;
for j:= p-r to p-1 do
begin
C:=S[j];
S[j]:=S[j+1];
S[j+1]:=C;
end;
end;
Shuffle:=S;
end;

To use the function call it several times with your original string and
with an increasing N value.

Here is an example calling loop:

S0:='ABCDE';  {S0 and S1 are strings.  SO is the original}
N:=0;
repeat
begin
N:=N+1;
S1:=shuffle(S0,N);
end
until S1=S0;
ListBox1.Sorted:=true;  {This makes it easyer to check}

Hope this helps...

BTW feel free to submit more little problems like this.
That one was fun.

Alain Toutant.

## Re:could someone help me? (mostly math problem)

Here's something I found in PC Magazine's Tutbo Pascal 6.0 Techniques and
Utilities:
(Modified by me)

type
PermString = string;

function Factorial( B : byte ) : extended;
var n: byte;
begin
Result := 1;
for n := 1 to B
do Result := Result * N;
end;

procedure XChg( var A, B : char )
var T : char;
begin
T := A;
A := B;
B := T;
end;

function PermOf( Org : PermString; L : longint ) : PermString;
var P, Q : byte;
Nump : longint;
begin
Result := '';
if L < 1
then exit;

Nump := round( Factorial( length( Org )));
if L > Nump
then exit;

dec( L );
P := 0;
for Q := length( Org ) downto 2
do begin
inc( P );
Nump := Nump div Q;
XChg( Org[P], Org[P + (L DIV Nump)]);
L := L mod Nump;
end;
Result := Org;
end;

To use:

var N : longint;
begin
for N := 1 to round( Factorial( 5 ))
do write( PermOf( 'abcde', N) : 8);
end;

Eli{*word*106}inson <el...@nyc.pipeline.com> wrote in article
<52a2p4\$...@news1.t1.usa.pipeline.com>...

##### Quote
> I'm trying to write a D2 app and need it to list every combination of a
> series of letters (or numbers).

> A formula that could do strings as well as integers would be nice, but
not
> necessary.  If it could take a number/string of any length would also be
> nice, but i could do that myself once i figure out the formula.

> example:
>   if you started with ABCD, then i want:
>   ABCD
>   ABDC
>   ACBD
>   ACDB
>   etc.

> i don't care what order the letters are in as long as i get all of them

> I know there must be a formula for this, but i can't seem to figure it
out.

> BTW there should be 120 different combonations of 4 letters (4! = 120).
> --

> Eli{*word*106}inson
> Lightning Computer Techologies, Inc.
> el...@nyc.pipeline.com
> http://www.nethosting.com/~eli/

## Re:could someone help me? (mostly math problem)

##### Quote
CFC <cfis...@mail.idt.net> wrote:
>Frank Mikalsen wrote:

>> el...@nyc.pipeline.com(Eli{*word*106}inson) wrote:

>> >BTW there should be 120 different combonations of 4 letters (4! = 120).

>> Sorry, I have no algorithm for you, but perhaps a little help
>> with your math: 4! = 24 not 120.

>> This will probably help in the debuging process ;-)

>Frank,
>    Where did you learn about factorials?
>      4 * 1 =  4
>      4 * 2 =  8
>      4 * 3 = 12
>      4 * 4 = 16
>              --
>              120

As I remember: 4! = 1 * 2 * 3 * 4 = 24

Go ahead and find 120 different permutations of ABCD, and
drop me a note when you have them all ;-)

Best regards,
Frank
--
// Frank Mikalsen, System Developer, Finale a.s
// Homepage: http://home.sol.no/frankm
// Author of ShareWare: Silent Partner Backup Screensaver v2.60