Mia

Finding all possible combinations of numbers

22 posts in this topic

Hi,

Any idea how to find all possible combinations of numbers of a chain of 4 numbers. (a lot of :D)

Example:

0123

0124

0125

0126

0127

...

and so on until 9999

Big thanks!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Do you mean 0 to 9999 or something else? As numbers (no leading zeros) or as strings? Which form should have the result: array or whatelse?

Try to use more precise terms else you'll get fuzzy/wrong anwsers.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
2 minutes ago, jchd said:

Do you mean 0 to 9999 or something else? As numbers (no leading zeros) or as strings? Which form should have the result: array or whatelse?

Try to use more precise terms else you'll get fuzzy/wrong anwsers.

As strings.

Example: 0001 counts as 4 chars.

Share this post


Link to post
Share on other sites
for $n = 0 to 9999
    msgbox(0, '' , StringFormat("%04i" , $n))
next

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Results can simply be shown in console with ConsoleWrite to make it simple (sorry for the double post, can't find the edit button)

Share this post


Link to post
Share on other sites

Here's another very fast algorithm:

#include <Array.au3>

Dim $aList[0]
Do
    $x = StringFormat("%04i", Random(0, 9999, 1))
    If _ArraySearch($aList, $x) == -1 Then
        ReDim $aList[UBound($aList)]
        _ArrayAdd($aList, $x)
    EndIf
Until UBound($aList) = 10000
_ArraySort($aList)
_ArrayDisplay($aList)

 


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Also check out _ArrayPermute.

8 hours ago, SadBunny said:

another very fast algorithm

@SadBunny: ReDim inside a loop is actually slowing the script down needlessly. Instead, I would just allocate the array once outside the loop and apply an upper bound in _ArraySearch that increments on successive iterations.

Share this post


Link to post
Share on other sites

@RTFC -

16 minutes ago, RTFC said:

another very fast algorithm

That may have been "tongue in cheek".  Note the random list that is generated, then sorted.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

12 minutes ago, kylomas said:

That may have been "tongue in cheek".

@kylomas: :DI think we need an emoji for that. I missed that, and maybe the OP would too.

Edited by RTFC

Share this post


Link to post
Share on other sites

No, RTFC is right. The untold millions of random numbers that the script searches the array for would be much faster inserted in that array, had it not to redim the array ten thousand times :shifty:

The speed difference between iamtheky's slow and inefficient procedure and my much superior solution using RNG and array manipulation (which if nothing else at least sounds more Random (1337,1337,1)...) should be immediately clear when running both scripts.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

That is barely passable as prng, and the speed difference is only evident because I msgbox the return.  At some point you have to loop through contents and format strings, any hopes of being superior at that are delusional.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
3 hours ago, kylomas said:
3 hours ago, RTFC said:

another very fast algorithm

That may have been "tongue in cheek".  Note the random list that is generated, then sorted.

kylomas

+1 :D

_ArraySearch + ReDim + _ArrayAdd = HighSpeed (well known)

Share this post


Link to post
Share on other sites
41 minutes ago, mikell said:

_ArraySearch + ReDim + _ArrayAdd = HighSpeed (well known)

See? Thanks. Finally someone who understands. The proof of the pudding is in the selection of random pudding molecules and then eating them, but only if they don't equal any of the molecules you have already eaten.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

ahh, the Bayesian solution to the ever difficult maths problem of N + 1


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Ok, bars open.  All y'all need a drinkl!


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

StringFormat() is slightly slower than concatenation:
$number = StringTrimLeft('000' &  $number, 4).

Edited by czardas

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

wouldnt you want to stringright, 4?

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

You were talking to @sadrazc, right?

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now