Welcome

User: Pass:   register


Determines the Euclids totient function for the given number. If you don't know what that is, you don't need the code :)

Usage: /totient N where N is an integer

alias totient {
  if (!$1) || ($2) { echo -a error: Please provide a single integer argument | halt }
  if ($int($1) != $1) { echo -a error: The given number must be an integer. | halt }
  if (!$hget(primes)) prime_sieve $1
  var %input = $1
  var %y = 1
  while (%y <= $hget(primes,0).item) {
    var %div = $hget(primes,%y).item
    var %fact = $calc(1 - 1/%div)
    var %tmp = $round($calc(%input * %fact),4)
    if ($int(%tmp) == %tmp) { %input = %tmp }
    inc %y
  }
  echo -a Euclid's totient function of $1 is %result
}
 
alias prime_sieve {
  if ($hget(primes)) {
    hfree primes
  }
  hmake primes $1
  var %i = 2
  hadd primes %i 1
  inc %i
  while (%i <= $1) {
    hadd primes %i 1
    inc %i 2
  }
  var %i = 3, %t
  while ($calc(%i ^2) <= $1) {
    var %t = $calc(%i *2)
    while (%t <= $1) {
      hdel primes %t
      inc %t %i
    }
    inc %i 2
    while (!$hget(primes,%i)) {
      inc %i 2
    }
  }
}


PS: I don't take credit for the prime_sieve function to load up a bunch of prime numbers. It's an edited version of code I found somewhere.






© 1996-2010 hdesk.org and dalnethelpdesk.com