Prime numbers

By Corne on Jul 04, 2005

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 ! }


Sign in to comment.
Corne   -  Jul 28, 2005

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)

QuickStep   -  Jul 06, 2005

Looking better now, still few things to mention:

  • with my previous post saying if divide by \'2\' doesnt match then all even numbers dont match, thats something you can keep in consideration. Check if it is dividable by 2 and if it isnt, only check UNeven numbers afterwards \'inc %prime 2\'. This will make the loop 2x faster.
  • You dont need the if (!%pr) bit because the script should be already halted if a match was found, so you dont need to set the %pr=1 too
  • 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
    %calc = $calc($1 / %prime)
    if (%calc = $int(%calc))

  • Now you can avoid this, but too be extremely picky, in a single if-statement you dont have to use the { } brackets, and it speeds up processing leaving them away, so it would be:
    else inc %prime
    instead of
    else { inc %prime }
aeros   -  Jul 05, 2005

weeeeeeee ;O

Corne   -  Jul 05, 2005

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)

Corne   -  Jul 05, 2005

I know that think with \'2\' and \'3\' and thanks for \'while (%prime <= $sqrt($1))\'. I know there are many ways to check if a number is integer and I just picked . .

QuickStep   -  Jul 05, 2005

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

QuickStep   -  Jul 05, 2005

also when a \'no prime\' is detected, halt the script, unnecesarry lag afterwards

QuickStep   -  Jul 05, 2005

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

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

xDaeMoN   -  Jul 05, 2005

Add a check if $1 is a number.

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.