Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:30:36+00:00 2026-06-10T06:30:36+00:00

this is my simple code to generate all possible combinations of a set for

  • 0

this is my simple code to generate
all possible combinations of a set for
example

1,2,3:

  • Display:
    123
    132
    213
    231
    312
    321

i want to create variable number of for loops to let the user determine the length of given string…

does anyone have an idea…

thank’s in advance.

type
  TNumber = '0'..'9';

procedure TForm1.Button1Click(Sender: TObject);
var
  Numbers: array[0..3] of TNumber;
  a, b, c, d: Integer;
  s: string;
begin
  Numbers[0] := '1';
  Numbers[1] := '8';
  Numbers[2] := '7';
  Numbers[3] := '2';
  for a := low(Numbers) to High(Numbers) do
    for b := low(Numbers) to High(Numbers) do
      for c := low(Numbers) to High(Numbers) do
        for d := low(Numbers) to High(Numbers) do
        begin
          s := Numbers[a] + Numbers[b] + Numbers[c]  + Numbers[d];
          if
            (Occurrences('1', s) > 1 ) or
            (Occurrences('8', s) > 1 ) or
            (Occurrences('7', s) > 1 ) or
            (Occurrences('2', s) > 1 )
          then
            Continue
          else
            Memo1.Lines.Add(s);
  end;
end;

function TForm1.Occurrences(const Substring, Text: string): Integer;
var
  Offset: Integer;
begin
  Result := 0;
  Offset := PosEx(Substring, Text, 1);
  while Offset <> 0 do
  begin
    Inc(Result);
    Offset := PosEx(Substring, Text, offset + length(Substring));
  end;
end;

end.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:30:38+00:00Added an answer on June 10, 2026 at 6:30 am

    Here is some code that produces the output you desire. You’d need to work it around a bit for your needs, but the concept expressed in this recursive solution is the important thing:

    program Permuatations;
    
    {$APPTYPE CONSOLE}
    
    type
      TElements = '1'..'3';
    
    procedure EnumerateCombinations(const Stem: string; Len: Integer);
    var
      i: Integer;
      el: TElements;
      Used: set of TElements;
    begin
      if Len=0 then
        exit;
      Used := [];
      for i := 1 to Length(Stem) do
        Include(Used, Stem[i]);
      for el := low(el) to high(el) do
      begin
        if el in Used then
          continue;
        if Len=1 then
          Writeln(Stem+el)
        else
          EnumerateCombinations(Stem+el, Len-1)
      end;
    end;
    
    procedure Main;
    begin
      EnumerateCombinations('', 1+ord(high(TElements))-ord(low(TElements)));
    end;
    
    begin
      Main;
      Readln;
    end.
    

    Output:

    123
    132
    213
    231
    312
    321
    

    If you change the definition of TElements, for example to '1'..'4' then you will see the 24 possible permutations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple code where I am sending http request and reading all
I have this simple code in my user_controller.rb file #listing all users def index
I'm writing a simple script to generate all combinations of a and b of
take this simple code: class A{ public: virtual void foo() = 0; void x(){
I have this simple code (for making things shorted, the important bits are probably
I have this simple code, using Joda-time. Works fine, but I have a problem.
I have this simple code in a WPF application: ThreadStart start = delegate() {
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
i have this simple code to load data from other php page using get
I am testing this simple code below and it works... $(document).ready( function() { $('.show-modal').click(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.