AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Least Common Multiple and Greatest Common Factor

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
maximo3491



Joined: 10 Feb 2007
Posts: 65

PostPosted: Fri Jan 04, 2008 5:43 am    Post subject: Least Common Multiple and Greatest Common Factor Reply with quote

Just 2 functions to replace the grueling task most of us were placed through in algebra.

If you want to use the Least Common Multiple function, you will need to include then the Greatest Common Factor as well.

Code:
LCM(number_1, number_2) ; Least Common Multiple
{
if %number_1% > %number_2% ; This will always make number_2 be greater than number_1
{
   temp_num := number_2
   number_2 := number_1
   number_1 := temp_num
}

if mod(number_1, number_2) = 0 ; If number_2 is divisible by number_1, it will return number_2
   return %number_1%
GCF := GCF(number_1, number_2) ; Finds the GCF

result := round(number_1 * number_2 / GCF) ; Divide out the GCF from one number
return %result%
}


Code:
GCF(number_1, number_2) ; Greatest Common Factor
{
if %number_1% > %number_2% ; This will always make number_2 be greater than number_1
{
   temp_num := number_2
   number_2 := number_1
   number_1 := temp_num
}
if mod(number_1, number_2) = 0 ; If number_2 is divisible by number_1, it will return number_1
   return %number_2%
loopcount := floor(number_2 / 2) - 1 ; The greatest factor can only be half of itself, excluding itself
loop %loopcount%         ; Subtracted 1 to make a_index in the loop 1 greater, so starting it at 2
{
divisor := a_index + 1 ; Starts at 2 since 1 is always a factor
if mod(number_1, divisor) = 0
   if mod(number_2, divisor) = 0
      GCM = %divisor% ; Will retrieve the last common factor of number_1 and number_2
}
return %GCM%
}


Examples

Code:
LCM(4,10)
will result in 20

Code:
GCF(4,10)
will result in 2
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6772
Location: Pacific Northwest, US

PostPosted: Fri Jan 04, 2008 5:58 am    Post subject: Reply with quote

instead of
Code:

if %number_1% > %number_2%

you should use
Code:

if (number_1 > number_2)

or
Code:

if number_1 > %number_2%

_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
JDN



Joined: 24 Mar 2004
Posts: 126

PostPosted: Fri Jan 04, 2008 7:56 am    Post subject: Reply with quote

It certainly appears more professional the way that engunneer has suggested - which is using expressions.

But can anyone explain the reasons for preferring expressions?

Is it an issue of machine efficiency? Or is it merely a question of superior style?
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4016
Location: Pittsburgh

PostPosted: Fri Jan 04, 2008 5:10 pm    Post subject: Reply with quote

If you use recursive functions the code can be shorter. In the Popup Calculator II script I used the following (Greatest Common Divisor):
Code:
GCD(a,b) {      ; Euclidean GCD
   Return b=0 ? Abs(a) : GCD(b, mod(a,b))
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4016
Location: Pittsburgh

PostPosted: Fri Jan 04, 2008 5:20 pm    Post subject: Reply with quote

...and you only need one function, because LCM(a,b) = a*b//GCD(a,b), as you did in your LCM code, but there is no need for checking, which parameter is larger.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6772
Location: Pacific Northwest, US

PostPosted: Fri Jan 04, 2008 6:19 pm    Post subject: Reply with quote

It's not about style, it's about correctness.

instead of
Code:

if %number_1% > %number_2%

(Which is incorrect syntax -> see item 3 of When exactly are variable names enclosed in percent signs?)

you should use
Code:

if (number_1 > number_2)

(Expression format style)

or
Code:

if number_1 > %number_2%

(non-expression format style with correct syntax)
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM


Last edited by engunneer on Fri Jan 04, 2008 6:24 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
JDN



Joined: 24 Mar 2004
Posts: 126

PostPosted: Fri Jan 04, 2008 6:24 pm    Post subject: Reply with quote

Yes. Quite right, engunneer.

Thank you for your reply.
Back to top
View user's profile Send private message
maximo3491



Joined: 10 Feb 2007
Posts: 65

PostPosted: Fri Jan 04, 2008 7:35 pm    Post subject: Reply with quote

I checked which one is larger so i could make sure number_2 is greater than number_1.

Then I checked whether or not the answer to the function is infact one of the numbers, if i wouldnt have made number_2 greater than number_1 EVERY time, then i would need to check mod(1,2) and mod(2,1).



Also, would anyone be willing to show me the documentation for these shorter methods. For example
Code:
Return b=0 ? Abs(a) : GCD(b, mod(a,b))


I understand what the functions mean and everything, but what does the ? and : mean. I've been able to complete the tasks i needed without using those, and I have been unsuccessful in fully understanding the meaning. Any who can link me to the location where I can read about this, please do so.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6772
Location: Pacific Northwest, US

PostPosted: Fri Jan 04, 2008 8:05 pm    Post subject: Reply with quote

? : is called the ternary operator. It's shorthand for if/else.

http://www.autohotkey.com/forum/viewtopic.php?p=136472#136472
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
emoyasha



Joined: 12 Nov 2007
Posts: 61

PostPosted: Wed Jan 09, 2008 7:28 am    Post subject: Reply with quote

how exactly do i use this>
_________________
online .ini reader with encryption

[url=http://www.autohotkey.com/forum/viewtopic.php?p=164102]
advanced tetris game many features [/url]
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4016
Location: Pittsburgh

PostPosted: Wed Jan 09, 2008 3:40 pm    Post subject: Reply with quote

Code:
InputBox Nums, For GCD type two numbers: x`,y,,,400,130
StringSplit a, Nums, `,
MsgBox % "GCD(" a1 "," a2 ")= " GCD(a1,a2)

GCD(a,b) {      ; Euclidean GCD
   Return b=0 ? Abs(a) : GCD(b, mod(a,b))
}
When prompted, type two numbers separated by comma and hit enter.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group