Just a simple snippet which shows you if a number if prime or not. Type /prime to check a number.
alias prime {
if ($1 > 1) && ($1 isnum) {
var %prime = 2
while (%prime <= $sqrt($1)) {
if ($int($calc($1 / %prime)) == $calc($1 / %prime)) { echo $color(info) -at 7,0 $1 2,0is not a prime number ! | var %pr = 1 | halt }
else { inc %prime }
}
if (!%pr) { echo $color(info) -at 7,0 $1 2,0is a prime number ! }
}
}
I understood the first 3 parts with \'2\', %pr and $int (thanks)
I know that { } are not necessary there and I\'ll try to get rid of them if you say that it slows down the script. I wont update the snippet because it would suck (3 updates...) and a script never gets perfect no matter how good it is:) You should post a snippet with prime numbers to see how would yours look like (I\'m not trying to be rude)
Looking better now, still few things to mention:
By comparing it like this \'if $calc(xxx) == $int($calc(xxx)) you are actually doing 2 calculations, which slows the script. This script obviously depends on speed, so set a variable, BUT dont use VAR in the loop itself, do it like this:
var %prime, %calc
;restofcode
%calc = $calc($1 / %prime)
if (%calc = $int(%calc))
etc.
I was thinking to what you said about \'2\' and \'3\' and I thought I can get rid of loop and I will divide $1 to 2 and 3 and if the result is an integer number then the number is not prime, else is prime but what would happen if the reuslt of $1 / 2 or /3 is not an integer number and $1 is not a prime number (for example 25). If the reuslt of $1 / 2 is not an integer number then obviously $1 / 4 is not an integer number either and if I wont do that check I\'ll have to check if the divider is a multiple of 2/3 so I\'ll still have to do a check and its the same work. (hopefully I\'m not wrong :) )
(updated the code)
Anyway, if you every think about improving the script, also remember this:
if \'2\' doesnt match, all even number wont match
if \'3\'doesnt match, all number contained in the table of 3 wont work (3,6,9,12)
if \'4\'doesnt match, all numbers containted int he table of 4 wont work (4,8,12)
this is something to make not of, especially with very large numbers, the way you have it now it might lock up your mirc
also read the suggestions below
2 things to note, which involves some understanding and math:
Note 1:
You let the loop go while $calc($1 / 2) is larger than prime, you do this to avoid \'mirrage\' or the calculation (42=8, 24=8), but this is NOT the right check, especially when checking large numbers. When a number mirrages it means that n*n=answer, meaning you should check the square root, thats the point where the calculation turns around. So: \'while (%prime <= $sqrt($1))\' OR \'while (%prime <= $calc($1 ^ 0.5))\'
This can save some lag especially during large numbers
Note2:
To check if a number is a decimal number, you check if a dot is in the number. While this works and is correct in mIRC, it is not the way to go especially when doing the same script in other languages, because you are converting a float to string. To check if a number is a decimal number, i always use \'if ($1 = $int($1))\'
anyway sorry to be picky