AutoHotkey Community

It is currently May 26th, 2012, 2:24 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: January 4th, 2008, 5:43 am 
Offline

Joined: February 10th, 2007, 5:18 am
Posts: 92
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2008, 5:58 am 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8666
Location: Salem, MA
instead of
Code:
if %number_1% > %number_2%

you should use
Code:
if (number_1 > number_2)

or
Code:
if number_1 > %number_2%

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2008, 7:56 am 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2008, 5:10 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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))
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2008, 5:20 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
...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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2008, 6:19 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8666
Location: Salem, MA
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)

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Last edited by engunneer on January 4th, 2008, 6:24 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2008, 6:24 pm 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
Yes. Quite right, engunneer.

Thank you for your reply.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2008, 7:35 pm 
Offline

Joined: February 10th, 2007, 5:18 am
Posts: 92
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 4th, 2008, 8:05 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8666
Location: Salem, MA
? : is called the ternary operator. It's shorthand for if/else.

http://www.autohotkey.com/forum/viewtop ... 472#136472

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 9th, 2008, 7:28 am 
Offline

Joined: November 12th, 2007, 4:46 am
Posts: 67
how exactly do i use this>

_________________
online .ini reader with encryption

advanced tetris game many features


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 9th, 2008, 3:40 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Stigg and 16 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group