Jump to content

Category talk:Wren-math: Difference between revisions

→‎Source code: Improved algorithm for Int.divisors2.
(Added efficient divisorSum, divisorCount and reverse methods to Int class.)
(→‎Source code: Improved algorithm for Int.divisors2.)
Line 632:
 
// As 'divisors' method but uses a different algorithm.
// Better for largelarger numbers with a small number of prime factors.
static divisors2(n) {
ifvar (npf <= 0Int.primeFactors(n) return []
varif factors(pf.count == Int.primeFactors0) return (n == 1) ? [1] : pf
var divsarr = [1]
forif (ppf.count in== factors1) {
for (i in 0...divs.count) divsarr.add(divs[ipf[0], 1]*p)
} else {
var prevPrime = pf[0]
var count = 1
for (i in 1...pf.count) {
if (pf[i] == prevPrime) {
count = count + 1
} else {
arr.add([prevPrime, count])
prevPrime = pf[i]
count = 1
}
}
arr.add([prevPrime, count])
}
divs.sort()var divisors = []
var c = divs.countgenerateDivs
ifgenerateDivs (c= > 1)Fn.new { |currIndex, currDivisor|
forif (icurrIndex in== c-1arr..1count) {
if (divs[i-1] == divs[i]) divsdivisors.removeAtadd(icurrDivisor)
return
}
for (i in 0..arr[currIndex][1]) {
generateDivs.call(currIndex+1, currDivisor)
currDivisor = currDivisor * arr[currIndex][0]
}
}
returngenerateDivs.call(0, divs1)
return divisors.sort()
}
 
9,482

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.