From 592764ded4ad2982e501640b65c539c542410a44 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Dec 2014 20:08:23 -0800 Subject: [PATCH] =?UTF-8?q?math.extras:=20adding=20the=20M=C3=B6bius=20fun?= =?UTF-8?q?ction.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extra/math/extras/extras-tests.factor | 9 +++++++++ extra/math/extras/extras.factor | 14 ++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/extra/math/extras/extras-tests.factor b/extra/math/extras/extras-tests.factor index c5c26cb99f..3f56fe2c2e 100644 --- a/extra/math/extras/extras-tests.factor +++ b/extra/math/extras/extras-tests.factor @@ -148,3 +148,12 @@ IN: math.extras.test { 36100 } [ 20 iota 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 diff --git a/extra/math/extras/extras.factor b/extra/math/extras/extras.factor index 9b2a86a285..90d8d4f19f 100644 --- a/extra/math/extras/extras.factor +++ b/extra/math/extras/extras.factor @@ -4,10 +4,10 @@ USING: accessors arrays assocs assocs.extras byte-arrays combinators combinators.short-circuit compression.zlib fry grouping kernel locals math math.combinatorics math.constants -math.functions math.order math.primes math.ranges -math.ranges.private math.statistics math.vectors memoize parser -random sequences sequences.extras sequences.private sets sorting -sorting.extras ; +math.functions math.order math.primes math.primes.factors +math.ranges math.ranges.private math.statistics math.vectors +memoize parser random sequences sequences.extras +sequences.private sets sorting sorting.extras ; IN: math.extras @@ -316,3 +316,9 @@ M: iota-tuple sum-squares GENERIC: sum-cubes ( seq -- n ) M: object sum-cubes [ 3 ^ ] map-sum ; 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 ;