# 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.

Factorial.au3

Factorial_Test.au3

Writing AutoIt scripts since

`_DateAdd("d", -2, _NowCalcDate())`
##### 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.
##### 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())`
##### 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.

##### 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

##### 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: 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

Â

##### 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

##### Share on other sites
• Moderators

Hi,

I think he got the message!

M23

Â 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:

Spoiler

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

Â

##### 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))
Next

;===========================
func _LongFactorial (\$vInt)
If \$vInt == "0" then
Return "1"
else
Return _LongProduct(\$vInt,_LongFactorial ( String (Number(\$vInt)-1) ))
endif
endfunc

; #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
EndIf

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

While StringLen(\$sZeros) < \$iBound
\$sZeros &= \$sZeros ; Tens, hundreds, thousands etc...
WEnd
\$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
Next
\$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)
Next
\$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)
Next

; 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
Next
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

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...