Category talk:Wren-i64: Difference between revisions
Content added Content deleted
(→Source code (Wren): Fixed some minor issues and improved U64.isPrime method.) |
(→Source code (Wren): Added largestPrime and prev{rime) |
||
Line 407: | Line 407: | ||
static maxSafe { from(9007199254740991) } |
static maxSafe { from(9007199254740991) } |
||
static maxU32 { from(4294967295) } |
static maxU32 { from(4294967295) } |
||
// Returns the largest prime less than 2^64. |
|||
static largestPrime { U64.fromStr("18446744073709551557") } |
|||
// Returns a U64 object equal in value to 'x' raised to the power of 'n'. |
// Returns a U64 object equal in value to 'x' raised to the power of 'n'. |
||
Line 688: | Line 691: | ||
static nextPrime(x) { |
static nextPrime(x) { |
||
if (x is Num) x = from(x) |
if (x is Num) x = from(x) |
||
if (x < two) return U64.two |
|||
var n = x.isEven ? x + one : x + two |
var n = x.isEven ? x + one : x + two |
||
while (true) { |
while (true) { |
||
if (isPrime(n)) return n |
if (isPrime(n)) return n |
||
n.add(two) |
n.add(two) |
||
} |
|||
} |
|||
// Returns the previous prime number less than 'x' or null if there isn't one. |
|||
static prevPrime(x) { |
|||
if (x is Num) x = from(x) |
|||
if (x < three) return null |
|||
if (x == three) return U64.two |
|||
var n = x.isEven ? x - one : x - two |
|||
while (true) { |
|||
if (isPrime(n)) return n |
|||
n.sub(two) |
|||
} |
} |
||
} |
} |