RosettaCodeData/Task/AKS-test-for-primes/Sidef/aks-test-for-primes.sidef
2016-12-05 23:44:36 +01:00

23 lines
526 B
Text
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

func binprime(p) {
p >= 2 || return false
for i in (1 .. p>>1) {
(binomial(p, i) % p) && return false
}
return true
}
func coef(n, e) {
(e == 0) && return "#{n}"
(n == 1) && (n = "")
(e == 1) ? "#{n}x" : "#{n}x^#{e}"
}
func binpoly(p) {
join(" ", coef(1, p), ^p -> map {|i|
join(" ", %w(+ -)[(p-i)&1], coef(binomial(p, i), i))
}.reverse...)
}
say "expansions of (x-1)^p:"
for i in ^10 { say binpoly(i) }
say "Primes to 80: [#{2..80 -> grep { binprime(_) }.join(' ')}]"