Number Combinations

Hello everyone

i'm trying to code a program in pascal that makes all the possible combinations (by 4 numbers) from 6 digits. For example if we have the digits [1 2 3 4 5 6] the possible combinations by 4 are 15:

1234 1235 1236
1245 1246 1256
1345 1346 1356
1456 2345 2346
2356 2456 3456

The combinations that can be made from 5 digits are only 5 and i've already done that in my program, but trying to make combinations from 6 digits is much more complicated. The code for combinations by 4 from 5 digits is this:

for i:=1 to 5 do
begin
for j:=1 to 5 do
if (cm[j]<>cm[i]) then write(cm[j]);
writeln;
end;

If anyone knows how to make the code that finds all the possible combinations by 4 numbers from 6 digits then please help me and write back to me.. thanks

Comments

  • you'll have more than 15 combinations. you could have 6^4 different combinations.



    for C1:=1 to 6 do
    for C2:=1 to 6 do
    for C3:=1 to 6 do
    for C4:=1 to 6 do begin
    D1:=C1;
    D2:=C2;
    D3:=C3;
    D4:=C4;
    writeln(d1,d2,d3,d4);
    end;

  • : Hello everyone
    :
    : i'm trying to code a program in pascal that makes all the possible combinations (by 4 numbers) from 6 digits. For example if we have the digits [1 2 3 4 5 6] the possible combinations by 4 are 15:
    :
    : 1234 1235 1236
    : 1245 1246 1256
    : 1345 1346 1356
    : 1456 2345 2346
    : 2356 2456 3456
    :
    : The combinations that can be made from 5 digits are only 5 and i've already done that in my program, but trying to make combinations from 6 digits is much more complicated. The code for combinations by 4 from 5 digits is this:
    :
    : for i:=1 to 5 do
    : begin
    : for j:=1 to 5 do
    : if (cm[j]<>cm[i]) then write(cm[j]);
    : writeln;
    : end;
    :
    : If anyone knows how to make the code that finds all the possible combinations by 4 numbers from 6 digits then please help me and write back to me.. thanks
    :


    You mean something like this:
    [code]
    var n1,n2,n3,n4:byte;

    begin
    for n1:=1 to 6 do
    for n2:=1 to 6 do
    for n3:=1 to 6 do
    for n4:=1 to 6 do
    if
    (n1<>n2) and
    (n1<>n3) and
    (n1<>n4) and

    (n2<>n3) and
    (n2<>n4) and

    (n3<>n4) then

    write(n1*1000+n2*100+n3*10+n4,' ');
    end.
    [/code]


    Iby


  • Here is a simple code, using an iterative routine:
    [code]
    var
    largestnumber:integer;
    numberofnumbers:integer;

    function IntToStr(io:integer):string;
    begin
    Str(i,IntToStr);
    end;

    procedure AddNumber(s:string);
    var
    i:integer;
    begin
    if length(s)<numberofnumbers then begin
    for i:=1 to largestnumber do
    if Pos(IntToStr(i),s)=0 then
    AddNumber(s+IntToStr(i));
    end else
    write(s,' ');
    end;

    procedure GetCombinations;
    begin
    AddNumber('');
    end;
    [/code]

    This code is untested.

  • are you sure this would display all possible combinations?


  • Last post I made was for meant for iby

  • I'm sorry for not making myself clear..
    ---
    Number 1: When i'm talking about number combinations, i'm not talking about mathematic combinations because in maths the digits in this order:1234
    and in this order:4321
    are a different thing. The code that i wrote for the possible combinations from 5 digits (i'm talking about the code in my 1st message) doesnt make a difference between those combinations - its the same thing with the digits placed in a different order. But each of the combinations must be printed from the smaller to the greater number. For example the combination 4321 will only be written like this: 1234. So now you can understand that this code will only print 5 combinations from 5 digits: (1234 1235 1245 1345 and 2345).
    ---
    Number 2: There's also one thing, we dont know what those 6 digits are, they might be any number between 1-10. Suppose we have the 1-d array cm[1..6]:

    cm[1]:=1;
    cm[2]:=5;
    cm[3]:=8;
    cm[4]:=6;
    cm[5]:=3;
    cm[6]:=9;

    Each number cannot be used again in an other assignment, for example if number 5 has been assigned to cm[2] then we cant assign it again to i.e cm[6].
    ---
    The program must take all those 6 digits, and find all their combinations (not with the mathematics meaning) by 4 numbers, which in this case are 15 and only 15 (they're all written in my first message). How can i do that?

    Like i said in my previous msg its easy to find the combinations from 5 digits but its more complicated when trying to do the same from 6 digits.. and i'm stuck. I hope that you can now understand and help me - and thanks for your responses.
  • : I'm sorry for not making myself clear..
    : ---
    : Number 1: When i'm talking about number combinations, i'm not talking about mathematic combinations because in maths the digits in this order:1234
    : and in this order:4321
    : are a different thing. The code that i wrote for the possible combinations from 5 digits (i'm talking about the code in my 1st message) doesnt make a difference between those combinations - its the same thing with the digits placed in a different order. But each of the combinations must be printed from the smaller to the greater number. For example the combination 4321 will only be written like this: 1234. So now you can understand that this code will only print 5 combinations from 5 digits: (1234 1235 1245 1345 and 2345).
    : ---
    : Number 2: There's also one thing, we dont know what those 6 digits are, they might be any number between 1-10. Suppose we have the 1-d array cm[1..6]:
    :
    : cm[1]:=1;
    : cm[2]:=5;
    : cm[3]:=8;
    : cm[4]:=6;
    : cm[5]:=3;
    : cm[6]:=9;
    :
    : Each number cannot be used again in an other assignment, for example if number 5 has been assigned to cm[2] then we cant assign it again to i.e cm[6].
    : ---
    : The program must take all those 6 digits, and find all their combinations (not with the mathematics meaning) by 4 numbers, which in this case are 15 and only 15 (they're all written in my first message). How can i do that?
    :
    : Like i said in my previous msg its easy to find the combinations from 5 digits but its more complicated when trying to do the same from 6 digits.. and i'm stuck. I hope that you can now understand and help me - and thanks for your responses.
    :


    Your first post didn't include numbers like 4123 or 4612 or 3214 or 2146. I'm not sure what exactly you are after. If you replace in my code [red]<>[/red] with [red]<[/red] it should generate sequence you are after (I hope).

    Iby
  • : Your first post didn't include numbers like 4123 or 4612 or 3214 or 2146. I'm not sure what exactly you are after. If you replace in my code [red]<>[/red] with [red]<[/red] it should generate sequence you are after (I hope).
    :
    : Iby
    :


    Yepp, that's the way to generate the sequence.
    All digits must be different (no repetition) and
    each digit must be smaller than the next one.

    [code]
    var n1,n2,n3,n4:byte;

    begin

    for n1:=1 to 6 do
    for n2:=1 to 6 do
    for n3:=1 to 6 do
    for n4:=1 to 6 do
    if
    (n1<n2) and
    (n1<n3) and
    (n1<n4) and

    (n2<n3) and
    (n2<n4) and

    (n3<n4) then

    write(n1*1000+n2*100+n3*10+n4,' ');
    end.
    [/code]

    Iby
  • : are you sure this would display all possible combinations?
    :
    :
    :
    I think so, but only if you run it with a known combination, can you be sure.
  • : : Your first post didn't include numbers like 4123 or 4612 or 3214 or 2146. I'm not sure what exactly you are after. If you replace in my code [red]<>[/red] with [red]<[/red] it should generate sequence you are after (I hope).
    : :
    : : Iby
    : :
    :
    :
    : Yepp, that's the way to generate the sequence.
    : All digits must be different (no repetition) and
    : each digit must be smaller than the next one.
    :
    : [code]
    : var n1,n2,n3,n4:byte;
    :
    : begin
    :
    : for n1:=1 to 6 do
    : for n2:=1 to 6 do
    : for n3:=1 to 6 do
    : for n4:=1 to 6 do
    : if
    : (n1<n2) and
    : (n1<n3) and
    : (n1<n4) and
    :
    : (n2<n3) and
    : (n2<n4) and
    :
    : (n3<n4) then
    :
    : write(n1*1000+n2*100+n3*10+n4,' ');
    : end.
    : [/code]
    :
    : Iby
    :


    And if you want to shuffle ANY six characters (not only digits)
    try this code. It simply uses small string as lookup table:

    [code]
    var n1,n2,n3,n4:byte;

    const my_six_char_set:string='3QfZw&';
    { Or you could first try '123456' to check the sequence... }

    begin

    for n1:=1 to 6 do
    for n2:=1 to 6 do
    for n3:=1 to 6 do
    for n4:=1 to 6 do
    if
    (n1<n2) and
    (n1<n3) and
    (n1<n4) and

    (n2<n3) and
    (n2<n4) and

    (n3<n4) then
    write(my_six_char_set[n1],
    my_six_char_set[n2],
    my_six_char_set[n3],
    my_six_char_set[n4],' ');
    readln;
    end.
    [/code]

    Iby


  • it works!
    thanks a lot that's what i was looking for..
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories