Jump to content

Erdös-Selfridge categorization of primes: Difference between revisions

→‎{{header|Wren}}: Doh, need more sleep!
(→‎{{header|Wren}}: Doh, need more sleep!)
Line 315:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
Runs in about 23.5 seconds.
Second part takes a while, about 12 minutes on my machine.
 
If anyone's wondering why I'm not removing duplicates from the list of prime factors, the reason is that this worsens overall time (adds about 2 minutes) even if I inline the necessary code rather than call a library function.
<lang ecmascript>import "./math" for Int
import "./fmt" for Fmt
Line 323 ⟶ 321:
var limit = (1e6.log * 1e6 * 1.2).floor // should be more than enough
var primes = Int.primeSieve(limit)
 
var prevCats = {}
 
var cat // recursive
cat = Fn.new { |p|
if (prevCats.containsKey(p)) return prevCats[p]
var pf = Int.primeFactors(p+1)
if (pf.all { |f| f == 2 || f == 3 }) return 1
if (p > 2) {
for (i in pf.count-1..1) {
if (pf[i-1] == pf[i]) pf.removeAt(i)
}
}
for (c in 2..11) {
if (pf.all { |f| cat.call(f) < c }) return c{
prevCats[p] = c
return c
}
}
return 12
9,486

edits

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