# Least Common Multiple

### By ProIcons on Apr 22, 2011

Well i made a small SLOW snippet that you can calculate the least common multiple

Before the Comma are the numbers that you want to find the least common multiple
After the comma is the number that stands for the multiples that the script will search

//echo -a \$ekp(4 6 8 12 16 48,20)
6 Numbers to Compare
in 20 Multiples Each.

Result: Least Common Multiple: 48

Changes:
Version 2: Code rewritten. Optimized. x100 Fastest
Version 1: Warning if the number after comma is bigger than 20, and there is no common multiple your mIRC will freeze for some seconds...

``````;;;;VERSION 2 Optimized!
;;;;VERSION 2 Optimized!
;;;;VERSION 2 Optimized!

alias ekp {
unset %ekp.*
var %i = 1
while (%i <= \$numtok(\$1,32) ) {
var %z = 1
while (%z <= \$2 ) {
set %ekp. \$+ \$gettok(\$1,%i,32) %ekp. [ \$+ [ \$gettok(\$1,%i,32) ] ] \$calc(\$gettok(\$1,%i,32) * %z)
inc %z
}
inc %i
}
var %k = 1
var %z
while (%k <= \$2) {
var %p = 0
var %i = 1
while (%i <= \$numtok(\$1,32) ) {
if (\$istokcs(%ekp. [ \$+ [ \$gettok(\$1,%i,32) ] ],\$gettok(%ekp. [ \$+ [ \$gettok(\$1,1,32) ] ],%k,32),32)) {
inc %p
var %z = \$gettok(%ekp. [ \$+ [ \$gettok(\$1,1,32) ] ],%k,32)
}
inc %i
}
if (%p == \$numtok(\$1,32)) { return %z }
inc %k
}
}

;;;;VERSION 1
;;;;VERSION 1
;;;;VERSION 1
;;;;VERSION 1    Do Not Use, Only for the Records...
;;;;VERSION 1    Do Not Use, Only for the Records...
;;;;VERSION 1    Do Not Use, Only for the Records...
;;;;VERSION 1
;;;;VERSION 1
;;;;VERSION 1
alias ekp {
unset %ekp.*
var %i = 1
while (%i <= \$numtok(\$1,32) ) {
var %z = 1
while (%z <= \$2 ) {
set %ekp. \$+ \$gettok(\$1,%i,32) %ekp. [ \$+ [ \$gettok(\$1,%i,32) ] ] \$calc(\$gettok(\$1,%i,32) * %z)
inc %z
}
inc %i
}
var %i = 1
while (%i <= \$numtok(\$1,32) ) {
var %k = 1
while (%k <= \$2) {
var %z = 1
var %p = 0
while (%z <= \$2 ) {
var %a = 1
while (%a <= \$numtok(\$1,32) ) {
if (%a != %i) {
if ( \$gettok(%ekp. [ \$+ [ \$gettok(\$1,%i,32) ] ],%k,32) == \$gettok(%ekp. [ \$+ [ \$gettok(\$1,%a,32) ] ],%z,32) ) { inc %p }
}
inc %a
}
if (%p == \$calc(\$numtok(\$1,32) - 1)) { return Least Multiple Common: \$gettok(%ekp. [ \$+ [ \$gettok(\$1,%i,32) ] ],%k,32) | halt }
inc %z
}
inc %k
}
inc %i
}
}
``````

ProIcons   -  Aug 17, 2011

well i didn't even knew that there is exists calculation identified by the symbol % :P

Imk0tter   -  May 28, 2011

to enhance jaytea's post:

each work with a dynamic number of parameters

``````alias gcd {
while \$2 {
while \$2 {
tokenize 32 \$2 \$calc(\$1 % \$2) \$3-
}
tokenize 32 \$1 \$3-
}
return \$1
}
``````
``````alias lcm {
while \$2 {
tokenize 32 \$calc(\$1 * \$2 / \$gcd(\$1, \$2)) \$3-
}
return \$1
}
``````
ProIcons   -  Apr 23, 2011

the value of \$2 is for how many multiple it wants to search...

you can set it by default to 20 or 40 or 100 or whatever

jaytea   -  Apr 22, 2011

not quite optimized since you're still calling \$gettok() and \$numtok() many more times than necessary - set them to local variables if you're sure their values won't change across multiple calls. not to mention the method is slow in and of itself :P

a quick method for calculating the LCM of a list of integers is to note that LCM(a, b, c, ...) = LCM(LCM(a, b), c, ...), and that LCM(a, b) = a * b / GCD(a, b) where GCD() is the greatest common divisor and can be found using a very simple algorithm:

``````alias gcd {
while (\$2) tokenize 32 \$2 \$calc(\$1 % \$2)
return \$1
}
``````

then \$lcm() can be defined as:

``````alias lcm {
var %i = 2, %lcm = \$gettok(\$1, 1, 32)
while (\$gettok(\$1, %i, 32) != \$null) {
%lcm = \$lcm_(\$v1, %lcm)
inc %i
}
return %lcm
}

alias lcm_ {
return \$calc(\$1 * \$2 / \$gcd(\$1, \$2))
}
``````

where \$lcm_() calculates the LCM of a pair of integers.

this is much more optimal and even lets you do away with requiring a value for \$2, which shouldn't be necessary.

ProIcons   -  Apr 22, 2011

Edite: Script Optimized - No Problems Now...

ProIcons   -  Apr 22, 2011

@Jethro_ i did for one of m y codes and i posted here for all of you... Also i want to add that i have fever on 39C¤ so i can't write well... i guess... if you have a better way you could post it :) anywayz i am going to bed i am not well^^

Sorasyn   -  Apr 22, 2011

I had heard of a Whilefix.dll awhile back that was used on what seemed to be an mp3 snippet. Might be worth taking a look at; although I don't know that a small snippet such as this would be suffice motivation to download it. Apparently it prevents mIRC from freezing or 'hanging' in large while loops, and judging from the mass of while functions I'd say it could be of use.

EDIT: Found it. Check the description for the link. http://www.hawkee.com/snippet/8521/

Jethro   -  Apr 22, 2011

Warning if the number after comma is bigger than 20, and there is no common multiple your mIRC will free for some seconds... You meant the word freeze

Usually a code that results in a hang or frozen state is not considered efficiently written; worse yet, it may give the users a bug-like experience. There has to be a better way to write a script like this.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.