给定正整数n,计算Mertens函数 M(n)的值,其中
和μ(ķ)是莫比乌斯函数,其中μ(ķ)= 1,如果ķ具有不同的素因子偶数,-1,如果ķ具有奇数个的不同的素因子,和0,如果首要因素是不明显。
测试用例
n M(n)
1 1
2 0
3 -1
4 -1
5 -2
6 -1
7 -2
8 -2
9 -2
10 -1
117 -5
5525 5
7044 -25
8888 4
10000 -23
给定正整数n,计算Mertens函数 M(n)的值,其中
和μ(ķ)是莫比乌斯函数,其中μ(ķ)= 1,如果ķ具有不同的素因子偶数,-1,如果ķ具有奇数个的不同的素因子,和0,如果首要因素是不明显。
n M(n)
1 1
2 0
3 -1
4 -1
5 -2
6 -1
7 -2
8 -2
9 -2
10 -1
117 -5
5525 5
7044 -25
8888 4
10000 -23
Answers:
:Ḋ߀SC
这使用属性
从A002321开始,将导致以下递归公式。
:Ḋ߀SC Main link. Argument: n
Ḋ Dequeue; yield [2, ..., n].
: Perform the integer division of n by each k in [2, ..., n].
߀ Recursively call the main link on each result.
S Sum; add the results from the recursive calls.
C Complement; map the sum r to 1 - r.
感谢@miles节省了2个字节。
Tr@*MoebiusMu@*Range
Range
生成一个从1输入的列表。
MoebiusMu
查找MoebiusMu
每个号码
Tr
对结果求和。
f=lambda n,k=2:n<k or f(n,k+1)-f(n/k)
这使用属性
从A002321开始,将导致以下递归公式。
我们不仅使用递归来计算商的M,而且还计算这些图像的总和。与下面的简单实现相比,这节省了8个字节。
M=lambda n:1-sum(M(n/k)for k in range(2,n+1))
当使用单个参数n调用f时,可选参数k默认为2。
如果n = 1,则n<k
得出True,而f返回该值。这是我们的基本情况。
如果n> 1,则n<k
最初返回False,然后or
执行以下代码。f(n/k)
递归计算总和的一项,并从的返回值中减去f(n,k+1)
。后者递增k并递归调用f,从而迭代k的可能值。一旦n <k + 1或n = 1,f(n,k+1)
将返回1,从而结束递归。
yb:1a+
$p#dl:_1r^|,0
yb The list [1, 2, …, Input]
:1a Apply predicate 1 (second line) to each element
+ Sum the resulting list
$p#d All elements of the list of prime factors of the Input are distinct
l:_1r^ Output = (-1)^(<length of the list of prime factors>)
| Or
,0 Output = 0
RÆFỊNP€FS
RÆFỊNP€FS Main link. Argument: n
R Range; yield [1, ..., n].
ÆF Factor; decompose each integer in that range into prime-exponent pairs.
Ị Insignificant; yield 1 for argument 1, 0 for all others.
N Negative; map n to -n.
This maps primes to 0, exponent 1 to -1, and all other exponents to 0.
P€ Reduce the columns of the resulting 2D arrays by multiplication.
The product of the prime values will always be 0; the product of the
exponent values is 0 if any exponent is greater than, 1 if there is an
even number of them, -1 is there is an odd number of them.
FS Flatten and sum, computing the sum of µ(k) for k in [1, ..., n].
Ị*%ðþÆḊ
效率不高;决定因素很难。
这使用A002321中的公式:
M(n)的是布尔矩阵的行列式甲N×N ,其中一个I,J是1,如果J = 1或I | j,否则为0。
Ị*%ðþÆḊ Main link. Argument: n
ð Combine the preceding atoms into a chain (unknown arity).
Begin a new, dyadic chain with arguments a and b.
Ị Insignificant; return 1 iff a = 1.
% Compute a % b.
* Compute (a == 1) ** (a % b).
This yields 1 if a = 1, or if a ≠ 1 and a % b = 0; otherwise, it yields 0.
þ Table; construct the matrix A by calling the defined chain for every pair
of integers in [1, ..., n].
ÆḊ Compute the determinant of the resulting matrix.
for(;$i=$argv[1]--;){for($n=$j=1;$j++<$i;)if(!($i%$j)){$i/=$j;$n++;if(!($i%$j))continue 2;}$a+=$n%2?1:-1;}echo$a;
据我所知,php缺少像质数功能之类的东西,所以这有点痛苦。有可能做得更好。
用途像:
php -r "for(;$i=$argv[1]--;){for($n=$j=1;$j++<$i;)if(!($i%$j)){$i/=$j;$n++;if(!($i%$j))continue 2;}$a+=$n%2?1:-1;}echo$a;" 10000
qiM{_,:)(@@f/{j-}/}j
使用OEIS中的公式
sum(k = 1..n, a([n/k])) = 1
。-David W.Wilson,2012年2月27日
和CJam的备忘运算符j
。
qi e# Read stdin as an integer
M{ e# Memoise with no base cases
e# Memoised function: stack contains n
_,:)( e# Basic manipulations to give n [2 .. n] 1
@@f/ e# More basic manipulations to give 1 [n/2 ... n/n]
{j-}/ e# For each element of the array, make a memoised recursive call and subtract
}j
def?(n:Int,k:Int=2):Int=if(n<k)1 else?(n,k+1)- ?(n/k)
丹尼斯(Dennis)提出的答案。
我称之为method ?
,它是一个不粘字母的令牌。
欢迎打高尔夫球。在线尝试!
R`;y;l0~ⁿ)π=*`MΣ
开球
Implicit input n.
R Push the range [1..n].
`...`M Map the following function over the range. Variable k.
; Duplicate k.
y Push the distinct prime factors of k. Call it dpf.
; Duplicate dpf.
l Push len(dpf).
0~ Push -1.
ⁿ Push (-1)**len(dpf).
) Move (-1)**len(dpf) to BOS. Stack: dpf, k, (-1)**len(dpf)
π Push product(dpf).
= Check if this product is equal to k.
If so, then k is squarefree.
* Multiply (k is squarefree) * (-1)**(length).
If k is NOT squarefree, then 0.
Else if length is odd, then -1.
Else if length is even, then 1.
This function is equivalent to the Möbius function.
Σ Sum the results of the map.
Implicit return.
n->sum(x=1,n,moebius(x))
1#.1*/@:-@~:@q:@+i.
n
使用范围内的Möbius函数之和计算Mertens函数[1, n]
。
f =: 1#.1*/@:-@~:@q:@+i.
(,.f"0) 1 2 3 4 5 6 7 8 9 10 117 5525 7044 8888 10000
1 1
2 0
3 _1
4 _1
5 _2
6 _1
7 _2
8 _2
9 _2
10 _1
117 _5
5525 5
7044 _25
8888 4
10000 _23
1#.1*/@:-@~:@q:@+i. Input: integer n
i. Range [0, 1, ..., n-1]
1 + Add 1 to each
q:@ Get the prime factors of each
~:@ Sieve mask of each, 1s at the first occurrence
of a value and 0 elsewhere
-@ Negate
*/@: Reduce each using multiplication to get the product
1#. Convert that to decimal from a list of base-1 digits
Equivalent to getting the sum