Jump to content

Factorials and Permutations

Recommended Posts

I intend on doing more probability functions later this year (maybe in August or September), I would do them now, if only I still remembered Geometric and Arithmetic sequences and such and so forth...

Here is the UDF for doing factorials (6! = 6*5*4*3*2*1 = 720) and permutations (6 items taken 3 at a time will offer 6P3 ways, or 120 ways).

For those curious, there are roughly 53400 different ways to unlock a standard rotary lock.



Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to post
Share on other sites

Good Job. :)

Now we can do factorials and permutations in AutoIt. These funcs are useful when trying to calculate probabilty. You should see if they can get put in Math.au3 to be preinstalled with AutoIt. These should be useful to many people.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Link to post
Share on other sites

Grrr....Any number in the permutations function greater than 20 returns a strange result (i.e. 20P3 etc). Actually its in probability too. Could it be possible that it is a string limitation or something, its returning negative numbers and decimal points...

I'm about to start pulling hair here :)

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to post
Share on other sites
  • 9 years later...

Hi MLSx Fanboy,

I am looking for a permutation factorial solution and saw your post. I noticed as well that using a number larger than 20 is returning decimal points. I was wondering if perhaps you had found a solution and wouldn't mind posting it. Thank you.

Link to post
Share on other sites

This post is 9 years old and the user hasn't been on here in 6 years, don't expect an answer any time in the near (or distant) future.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites

This post is 9 years old, and the OP has not been on in 6 years. I doubt you will get a response from them.. ;)

Snips & Scripts

My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ


Link to post
Share on other sites
  • Moderators

millisys, the post is 9 years old, and the OP hasn't even been on the forum in 6 years. Do you really expect a response??

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to post
Share on other sites
  • Moderators


I think he got the message! :D


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Link to post
Share on other sites

Nevertheless the example of Factorial for Long number exists:

For $i = 40 To 50
  $vInt = String($i)
    MsgBox(0,"", $vInt & "! = " & _LongFactorial($i))

func _LongFactorial ($vInt)
  If $vInt == "0" then
   Return "1"
   Return _LongProduct($vInt,_LongFactorial ( String (Number($vInt)-1) ))

; #FUNCTION# ==============================
;From http://www.autoitscript.com/forum/topic/157301-longproduct/?hl=%2Bbignum#entry1139178
; Name...........: _LongProduct
; Description ...: Multiplies two large positive integers together using old school long multiplication method
; Syntax.........: _LongProduct($vInt1, $vInt2)
; Parameters ....: $vInt1  - First integer - can be a number or a string of digits
;                  $vInt2  - Second integer - as above
; Return values .: Success - Returns the product of the two numbers
;                  Failure - Sets @error to 1 if either parameter contains non digit characters
; Author ........: czardas
; Comments ......; A separate function must be created if you want to preprocess floating point numbers
; =========================================

Func _LongProduct($vInt1, $vInt2)
    If Not (StringIsDigit($vInt1) And StringIsDigit($vInt2)) Then Return SetError(1)

    Local $vTemp ; Reuseable placeholder
    If StringLen($vInt2) > StringLen($vInt1) Then
        ; Set $vInt2 to be the shortest numeric string to reduce array size later
        $vTemp = $vInt1
        $vInt1 = $vInt2
        $vInt2 = $vTemp

    Local $iBound = StringLen($vInt2), $sZeros = "0" ; Zero padding for the method of long multiplication

    While StringLen($sZeros) < $iBound
        $sZeros &= $sZeros ; Tens, hundreds, thousands etc...
    $sZeros = StringRight($sZeros, $iBound -1) ; Highest order of magnitude needed

    Local $aPartProduct[$iBound], $iCarryForward ; Placeholder for tens of units, hundreds, thousands etc...

    For $i = 1 To $iBound ; Starting with the most significant digit in $vInt2
        $aPartProduct[$i -1] = ""
        $iCarryForward = 0 ; Maximum possible value in this code section is 8 ==> 89 = 9*9+8

        For $j = StringLen($vInt1) To 1 Step -1
            ; Multiply every digit in $vInt2 by every digit in $vInt1
            $vTemp = StringMid($vInt2, $i, 1) * StringMid($vInt1, $j, 1)

            ; Add the tens of units left over from the previous calculation
            $vTemp += $iCarryForward
            $iCarryForward = Number(StringTrimRight($vTemp, 1)) ; This will be added on the next run

            ; Append the units
            $aPartProduct[$i -1] &= StringRight($vTemp, 1) ; Digits appear in reverse sequence
        $aPartProduct[$i -1] &= $iCarryForward

        ; Pad with the correct number of zeros
        $aPartProduct[$i -1] = StringRight($sZeros, $iBound - $i) & $aPartProduct[$i -1] & StringTrimRight($sZeros, $iBound - $i)
    $iCarryForward = 0 ; Reset this variable for the following process

    ; Take the sum of all the part products in the array
    Local $sProduct = ""
    For $i = 1 To StringLen($aPartProduct[0]) ; Starting with the least significant digit
        $vTemp = 0 ; Sum value
        For $j = 0 To UBound($aPartProduct) -1
            ; Add the digits from the units, tens of units, hundreds, thousands etc...
            $vTemp += StringMid($aPartProduct[$j], $i, 1)

        ; Add the value left over from the previous calculation
        $vTemp += $iCarryForward
        $iCarryForward = Number(StringTrimRight($vTemp, 1)) ; This will be added on the next run

        $sProduct = StringRight($vTemp, 1) & $sProduct ; Returns the digits in the correct order
    If $iCarryForward Then $sProduct = $iCarryForward & $sProduct

    If StringLeft($sProduct, 1) = "0" Then $sProduct = StringTrimLeft($sProduct, 1)
    Return $sProduct
EndFunc ;==> _LongProduct

The point of world view

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...