math.extras: adding the Möbius function.

db4
John Benediktsson 2014-12-23 20:08:23 -08:00
parent 56b95b09a1
commit 592764ded4
2 changed files with 19 additions and 4 deletions

View File

@ -148,3 +148,12 @@ IN: math.extras.test
{ 36100 } [ 20 iota sum-cubes ] unit-test { 36100 } [ 20 iota sum-cubes ] unit-test
{ 36100 } [ 20 iota >array sum-cubes ] unit-test { 36100 } [ 20 iota >array sum-cubes ] unit-test
{
{
1 -1 -1 0 -1 1 -1 0 0 1 -1 0 -1 1 1
0 -1 0 -1 0 1 1 -1 0 0 1 0 0 -1 -1
}
} [
30 [1,b] [ mobius ] map
] unit-test

View File

@ -4,10 +4,10 @@
USING: accessors arrays assocs assocs.extras byte-arrays USING: accessors arrays assocs assocs.extras byte-arrays
combinators combinators.short-circuit compression.zlib fry combinators combinators.short-circuit compression.zlib fry
grouping kernel locals math math.combinatorics math.constants grouping kernel locals math math.combinatorics math.constants
math.functions math.order math.primes math.ranges math.functions math.order math.primes math.primes.factors
math.ranges.private math.statistics math.vectors memoize parser math.ranges math.ranges.private math.statistics math.vectors
random sequences sequences.extras sequences.private sets sorting memoize parser random sequences sequences.extras
sorting.extras ; sequences.private sets sorting sorting.extras ;
IN: math.extras IN: math.extras
@ -316,3 +316,9 @@ M: iota-tuple sum-squares
GENERIC: sum-cubes ( seq -- n ) GENERIC: sum-cubes ( seq -- n )
M: object sum-cubes [ 3 ^ ] map-sum ; M: object sum-cubes [ 3 ^ ] map-sum ;
M: iota-tuple sum-cubes sum sq ; M: iota-tuple sum-cubes sum sq ;
: mobius ( n -- x )
group-factors values [ 1 ] [
dup [ 1 > ] any?
[ drop 0 ] [ length even? 1 -1 ? ] if
] if-empty ;