Jump to content

_Factor()


Paulie
 Share

Recommended Posts

Returns all the factors of a number greater than 0

The $q parameter can be set to 0 and it will only return how many factors the number has.

Func _Factor($Num,$q=1)
    If $q = 0 Then
        Local $b=0
        For $a = 1 to $Num
            $result = $Num/$a
            If $result = Round($result,0) Then
                $b+=1
            EndIf
        Next
        Return $b
    Else    
        Local $b=0
        For $a = 1 to $Num
            $result = $Num/$a
            If $result = Round($result,0) Then
                $b+=1
            EndIf
        Next;;
        Local $Factors[$b+1][2],$i=0
        For $a = 1 to $Num
            $result = $Num/$a
            If $result = Round($result,0) Then
                $i+=1
                $Factors[$i][0] = $result
                $Factors[$i][1] = $a
            EndIf
        Next
        $Factors[0][0] = $i
        $Factors[0][1] = $i/2
        Return $Factors
    EndIf
EndFuncoÝ÷ Øw«z+&ÉìZ^jëh×6$int=_Factor(1002,1)
If IsArray($int) Then 
    _ArrayDisplay2($int);modded arraydisplay
Else
    MsgBox(0,"",$int)
EndIf
;-----
Func _ArrayDisplay2(Const ByRef $avArray, $sTitle="")
    Local $iCounter = 0, $sMsg = "", $iCounter2=0
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return 0
    EndIf
    If UBound($avArray,0) = 2 Then
        For $iCounter = 0 To UBound($avArray) - 1
            For $iCounter2 = 0 to UBound($avArray,2)-1
                $sMsg = $sMsg & "[" & $iCounter & "]["&$icounter2&"]    = " & StringStripCR($avArray[$iCounter][$icounter2]) & @CR
            Next
        Next
    ElseIf UBound($avArray,0) = 1 Then
        For $iCounter = 0 To UBound($avArray) - 1
            $sMsg = $sMsg & "[" & $iCounter & "]    = " & StringStripCR($avArray[$iCounter]) & @CR
        Next
    EndIf
    MsgBox(4096, $sTitle, $sMsg)
    SetError(0)
    Return 1
EndFunc

Kinda pointless, but can add to the mathematical capabilities of autoit. :shocked:

Edited by Paulie
Link to comment
Share on other sites

:shocked: lol:

__________________________________________________________________

C:\Program Files\AutoIt3\test.au3(1,20) : ERROR: _Factor(): undefined function.

$int=_Factor(1002,1)

~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\test.au3 - 1 error(s), 0 warning(s)

__________________________________________________________________

:( would you like to try that math again?

Link to comment
Share on other sites

:shocked: lol:

__________________________________________________________________

C:\Program Files\AutoIt3\test.au3(1,20) : ERROR: _Factor(): undefined function.

$int=_Factor(1002,1)

~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\test.au3 - 1 error(s), 0 warning(s)

__________________________________________________________________

:( would you like to try that math again?

Umm... you have to put the function in the same script as the example.
Link to comment
Share on other sites

by the way, instead dividing then rounding then comparing, wouldn't it be easier to just use Mod ( ) ?

If Mod ($Num, $a) = 0 Then $b += 1

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

  • 7 months later...

Did not understand @Paulies function at first. So I wiped up my own. It's quite a bit faster. And demonstrates a different approach. So here it is, if someone comes hunting for something like this..:P

func GetFactors($num)
    ; Return an array with all factors of a given number.
    ; NOTE: This function will fail (not return the smallest factors) for 
    ; numbers that is dividable by primes bigger than 997.
    ; To solve that we could increase the prime table or let the function 
    ; calculate the primes needed if it reache a undetermined situation.
    ;SOURCE PRIMES: http://en.wikipedia.org/wiki/List_of_prime_numbers#The_first_500_prime_numbers
    Local $primes = "2 ,3 ,5, 7 ,11 ,13 ,17 ,19 ,23 ,29 ," & _
    "31 ,37 ,41 ,43 ,47 ,53 ,59 ,61 ,67 ,71 ," & _
    "73 ,79 ,83 ,89 ,97 ,101 ,103 ,107 ,109 ,113 ," & _
    "127 ,131 ,137 ,139 ,149 ,151 ,157 ,163 ,167 ,173 ," & _
    "179 ,181 ,191 ,193 ,197 ,199 ,211 ,223 ,227 ,229 ," & _
    "233 ,239 ,241 ,251 ,257 ,263 ,269 ,271 ,277 ,281 ," & _
    "283 ,293 ,307 ,311 ,313 ,317 ,331 ,337 ,347 ,349 ," & _
    "353 ,359 ,367 ,373 ,379 ,383 ,389 ,397 ,401 ,409 ," & _
    "419 ,421 ,431 ,433 ,439 ,443 ,449 ,457 ,461 ,463 ," & _
    "467 ,479 ,487 ,491 ,499 ,503 ,509 ,521 ,523 ,541 ," & _
    "547 ,557 ,563 ,569 ,571 ,577 ,587 ,593 ,599 ,601 ," & _
    "607 ,613 ,617 ,619 ,631 ,641 ,643 ,647 ,653 ,659 ," & _
    "661 ,673 ,677 ,683 ,691 ,701 ,709 ,719 ,727 ,733 ," & _
    "739 ,743 ,751 ,757 ,761 ,769 ,773 ,787 ,797 ,809 ," & _
    "811 ,821 ,823 ,827 ,829 ,839 ,853 ,857 ,859 ,863 ," & _
    "877 ,881 ,883 ,887 ,907 ,911 ,919 ,929 ,937 ,941 ," & _
    "947 ,953 ,967 ,971 ,977 ,983 ,991 ,997"
    $primes = StringSplit($primes, " ,")

    ;Local $primes = StringSplit("2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113",",")
    ; Calculate the factors in $num
    Local $size = 10
    Local $factors[$size]
    $factors[0] = 0
    Local $pos = 1

    While $pos <=$primes[0] AND $num > 1
        If mod($num, $primes[$pos]) = 0 Then 
            $factors[0] += 1
            If $size -1 < $factors[0] Then ReDim $factors[$factors[0] + 10]
            $factors[$factors[0]] = $primes[$pos]
            $num = $num/$primes[$pos]
            ; Do not change $pos as we might want the same prime again!
        Else
            $pos += 1
        EndIf
    WEnd
    If $num > 1 then 
            $factors[0] += 1
            If $size -1 < $factors[0] Then ReDim $factors[$factors[0] + 10]
            $factors[$factors[0]] = $num
    EndIf
    Return $factors
endfunc
func factorsDump(ByRef $a, $head="", $tail= "")
    if $head = "" Then 
        ConsoleWrite("Factors( count:=" & $a[0] & ") : ")
    Else
        ConsoleWrite($head)
    EndIf
    for $i = 1 to $a[0]
        ConsoleWrite($a[$i] & ", ")
    Next
    ConsoleWrite(@CRLF)
EndFunc
;
$a = GetFactors(33765)
factorsDump($a)
Link to comment
Share on other sites

what about this one:

Func _primeFactor($iNum)
    Local $aRet[Ceiling(log($iNum)/log(2))]
    Local $a = 0
    Local $i = 2
    If $iNum = 1 Then 
        $aRet[0] = 1
        Return $aRet
    EndIf
    While $iNum > 1
        If Mod($iNum,$i) = 0 Then
            $aRet[$a] = $i
            $a += 1
            $iNum /= $i
        Else
            $i += 1
        EndIf
    WEnd
    Return $aRet
EndFunc

looks more smartly, huh?

Link to comment
Share on other sites

what about this one:

Func _primeFactor($iNum)
    Local $aRet[Ceiling(log($iNum)/log(2))]
    Local $a = 0
    Local $i = 2
    If $iNum = 1 Then 
        $aRet[0] = 1
        Return $aRet
    EndIf
    While $iNum > 1
        If Mod($iNum,$i) = 0 Then
            $aRet[$a] = $i
            $a += 1
            $iNum /= $i
        Else
            $i += 1
        EndIf
    WEnd
    Return $aRet
EndFunc

looks more smartly, huh?

Yes, but works more dumbly.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 8 months later...

I am trying to use this great function, but I saw 2 times Local $b=0 in here.

One under the If and one under the Else...

Isn't it just as good to put this above the If..Then?

Cause you Local that $b anyway.

Yay, one line less! :P (If i'm correct...)

My active project(s): A-maze-ing generator (generates a maze)

My archived project(s): Pong3 (Multi-pinger)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...