| View previous topic :: View next topic |
| Author |
Message |
maximo3491
Joined: 10 Feb 2007 Posts: 65
|
Posted: Fri Jan 04, 2008 5:43 am Post subject: Least Common Multiple and Greatest Common Factor |
|
|
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
will result in 20
will result in 2 |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6772 Location: Pacific Northwest, US
|
Posted: Fri Jan 04, 2008 5:58 am Post subject: |
|
|
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 |
|
 |
JDN
Joined: 24 Mar 2004 Posts: 126
|
Posted: Fri Jan 04, 2008 7:56 am Post subject: |
|
|
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 |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Fri Jan 04, 2008 5:10 pm Post subject: |
|
|
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 |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Fri Jan 04, 2008 5:20 pm Post subject: |
|
|
| ...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 |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6772 Location: Pacific Northwest, US
|
Posted: Fri Jan 04, 2008 6:19 pm Post subject: |
|
|
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 |
|
 |
JDN
Joined: 24 Mar 2004 Posts: 126
|
Posted: Fri Jan 04, 2008 6:24 pm Post subject: |
|
|
Yes. Quite right, engunneer.
Thank you for your reply. |
|
| Back to top |
|
 |
maximo3491
Joined: 10 Feb 2007 Posts: 65
|
Posted: Fri Jan 04, 2008 7:35 pm Post subject: |
|
|
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 |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6772 Location: Pacific Northwest, US
|
|
| Back to top |
|
 |
emoyasha
Joined: 12 Nov 2007 Posts: 61
|
Posted: Wed Jan 09, 2008 7:28 am Post subject: |
|
|
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 |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Wed Jan 09, 2008 3:40 pm Post subject: |
|
|
| 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 |
|
 |
|