Category talk:Wren-big: Difference between revisions
Content added Content deleted
m (→Source code: Added quotes to 'lang' attribute.) |
(→Source code: Added divisors and properDivisors methods to BigInt.) |
||
Line 1,478: | Line 1,478: | ||
if (m is Num) m = BigInt.new(m) |
if (m is Num) m = BigInt.new(m) |
||
return primeFactors_(m, true) |
return primeFactors_(m, true) |
||
} |
|||
// Returns all the divisors of 'n' including 1 and 'n' itself. |
|||
static divisors(n) { |
|||
if (n <= BigInt.zero) return [] |
|||
var factors = BigInt.primeFactors(n) |
|||
var divs = [BigInt.one] |
|||
for (p in factors) { |
|||
for (i in 0...divs.count) divs.add(divs[i]*p) |
|||
} |
|||
divs.sort() |
|||
var c = divs.count |
|||
if (c > 1) { |
|||
for (i in c-1..1) { |
|||
if (divs[i-1] == divs[i]) divs.removeAt(i) |
|||
} |
|||
} |
|||
return divs |
|||
} |
|||
// Returns all the divisors of 'n' excluding 'n'. |
|||
static properDivisors(n) { |
|||
var d = divisors(n) |
|||
var c = d.count |
|||
return (c <= 1) ? [] : d[0..-2] |
|||
} |
} |
||
} |
} |