$prime(N)

By NIGathan on Mar 06, 2009

I havent posted anything in a while, and I was thinking about prime numbers for some odd reason... So I decided to port the C/C++ primity function to mIRC.

You can find the original code here: http://docs.google.com/Doc?id=dgrmft6x_0htx3n3fh&hl=en

It should be 99.99% accurate.

Returns $true if N ($1) is a prime number, and $false if it is not.

alias prime {
  if ($1 !isnum 2-) return $false
  var %x 2, %y $floor($sqrt($1))
  while (%x <= %y) {
    if (!$calc($1 % %x)) && (%x != $1) break
    inc %x
  }
  if (%y == $calc(%x -1)) return $true
  return $false
}

Comments

Sign in to comment.
Jonesy44   -  Mar 07, 2009

np. forgot to rate. 7/10

 Respond  
NIGathan   -  Mar 06, 2009

Thanks :D

 Respond  
Jonesy44   -  Mar 06, 2009

done! Lol, it's a neat script. and for the small numbers i checked, it worked well and pretty much an instant reply even though it was running 2 while loops

 Respond  
NIGathan   -  Mar 06, 2009

lol, I just updated it with a REAL working version, only to see your post, make sure you use it instead.. The old one isnt 100% accurate, it cant find prime numbers if the only factor is a prime number (above 10) squared.

However, the updated one is as close to 100% as you can get.

 Respond  
Jonesy44   -  Mar 06, 2009
alias prime {
  if ($1 !isnum 2- && !$prop) { return 2* $!prime: error; $prime(N)[.up|down] }
  if ($istok(up down,$prop,32)) {
    var %x = $1 
    while (%x) {
      if ($prime.test(%x)) { return %x }
      else { $replace($prop,up,inc,down,dec) %x }
    }
  }
  else { return $prime.test($1) }
}
alias prime.test {
  if ($1 !isnum 2-) return $false
  var %x 2, %y $floor($sqrt($1))
  while (%x <= %y) {
    if (!$calc($1 % %x)) && (%x != $1) break
    inc %x
  }
  if (%y == $calc(%x -1)) return $true
  return $false
}

Got bored to i added a bit on..
$prime(N)[.up|down] - searches up or down for the next prime num. or just checks with no $prop

 Respond  
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.