Category talk:Wren-math: Difference between revisions
Content added Content deleted
(Added Nums.plot method.) |
(→Source code: Added maxSafePrime and prevPrime methods and outlawed 'unsafe' arguments to several existing methods of the Int class.) |
||
Line 334: | Line 334: | ||
// Returns the binomial coefficent of n over k. |
// Returns the binomial coefficent of n over k. |
||
static binomial(n, k) { multinomial(n, [k, n-k]) } |
static binomial(n, k) { multinomial(n, [k, n-k]) } |
||
// The highest prime less than 2^53. |
|||
static maxSafePrime { 9007199254740881 } |
|||
// Private helper method for 'isPrime' method. |
// Private helper method for 'isPrime' method. |
||
Line 339: | Line 342: | ||
// Miller-Rabin test which should be faster for 'n' >= 2 ^ 32. |
// Miller-Rabin test which should be faster for 'n' >= 2 ^ 32. |
||
static isPrimeMR_(n) { |
static isPrimeMR_(n) { |
||
if (n > Num.maxSafeInteger) Fiber.abort("Argument must be a 'safe' integer.") |
|||
if (n%2 == 0) return n == 2 |
if (n%2 == 0) return n == 2 |
||
if (n%3 == 0) return n == 3 |
if (n%3 == 0) return n == 3 |
||
Line 420: | Line 424: | ||
// Returns the next prime number greater than 'n'. |
// Returns the next prime number greater than 'n'. |
||
static nextPrime(n) { |
static nextPrime(n) { |
||
if (n >= maxSafePrime) Fiber.abort("Argument is larger than maximum safe prime.") |
|||
if (n < 2) return 2 |
if (n < 2) return 2 |
||
n = (n%2 == 0) ? n + 1 : n + 2 |
n = (n%2 == 0) ? n + 1 : n + 2 |
||
Line 425: | Line 430: | ||
if (Int.isPrime(n)) return n |
if (Int.isPrime(n)) return n |
||
n = n + 2 |
n = n + 2 |
||
⚫ | |||
} |
|||
// Returns the previous prime number less than 'n' or null if there isn't one. |
|||
static prevPrime(n) { |
|||
if (n > Num.maxSafeInteger) Fiber.abort("Argument must be a 'safe' integer.") |
|||
if (n < 3) return null |
|||
if (n == 3) return 2 |
|||
n = (n%2 == 0) ? n - 1 : n - 2 |
|||
while (true) { |
|||
if (Int.isPrime(n)) return n |
|||
n = n - 2 |
|||
} |
} |
||
} |
} |
||
Line 602: | Line 619: | ||
static primeFactors(n) { |
static primeFactors(n) { |
||
if (!n.isInteger || n < 2) return [] |
if (!n.isInteger || n < 2) return [] |
||
if (n > Num.maxSafeInteger) Fiber.abort("Argument must be a 'safe' integer.") |
|||
var inc = [4, 2, 4, 2, 4, 6, 2, 6] |
var inc = [4, 2, 4, 2, 4, 6, 2, 6] |
||
var factors = [] |
var factors = [] |
||
Line 634: | Line 652: | ||
static divisors(n) { |
static divisors(n) { |
||
if (!n.isInteger || n < 1) return [] |
if (!n.isInteger || n < 1) return [] |
||
if (n > Num.maxSafeInteger) Fiber.abort("Argument must be a 'safe' integer.") |
|||
var divisors = [] |
var divisors = [] |
||
var divisors2 = [] |
var divisors2 = [] |
||
Line 705: | Line 724: | ||
static divisorSum(n) { |
static divisorSum(n) { |
||
if (!n.isInteger || n < 1) return 0 |
if (!n.isInteger || n < 1) return 0 |
||
if (n > Num.maxSafeInteger) Fiber.abort("Argument must be a 'safe' integer.") |
|||
var total = 1 |
var total = 1 |
||
var power = 2 |
var power = 2 |
||
Line 731: | Line 751: | ||
static divisorCount(n) { |
static divisorCount(n) { |
||
if (!n.isInteger || n < 1) return 0 |
if (!n.isInteger || n < 1) return 0 |
||
if (n > Num.maxSafeInteger) Fiber.abort("Argument must be a 'safe' integer.") |
|||
var count = 0 |
var count = 0 |
||
var prod = 1 |
var prod = 1 |
||
Line 754: | Line 775: | ||
// Private helper method which checks a number and base for validity. |
// Private helper method which checks a number and base for validity. |
||
static check_(n, b) { |
static check_(n, b) { |
||
if (!(n is Num && n.isInteger && n >= 0)) { |
if (!(n is Num && n.isInteger && n >= 0 && n <= Num.maxSafeInteger)) { |
||
Fiber.abort("Number must be a non-negative integer.") |
Fiber.abort("Number must be a non-negative 'safe' integer.") |
||
} |
} |
||
if (!(b is Num && b.isInteger && b >= 2 && b < 64)) { |
if (!(b is Num && b.isInteger && b >= 2 && b < 64)) { |
||
Line 796: | Line 817: | ||
return [n, ap] |
return [n, ap] |
||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// Returns a new integer formed by reversing the base 10 digits of 'n'. |
// Returns a new integer formed by reversing the base 10 digits of 'n'. |
||
// Works with positive, negative and zero integers. |
// Works with positive, negative and zero integers. |
||
static reverse(n) { |
static reverse(n, check) { |
||
if (check) check_(n, b) |
|||
var m = (n >= 0) ? n : -n |
var m = (n >= 0) ? n : -n |
||
var r = 0 |
var r = 0 |
||
Line 817: | Line 830: | ||
return r * n.sign |
return r * n.sign |
||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
static reverse(n) { reverse(n, false) } |
|||
// Returns the unique non-negative integer that is associated with a pair |
// Returns the unique non-negative integer that is associated with a pair |