คำนวณฟังก์ชัน Mertens


18

รับจำนวนเต็มบวกnคำนวณค่าของฟังก์ชัน Mertens M ( n ) โดยที่

Mertens

และμ ( k ) คือฟังก์ชั่นMöbiusโดยที่μ ( k ) = 1 ถ้าkมีจำนวนเฉพาะของปัจจัยสำคัญที่แตกต่างกัน -1 ถ้าkมีจำนวนเฉพาะของปัจจัยสำคัญที่แตกต่างกันและ 0 ถ้าปัจจัยสำคัญไม่ชัดเจน

  • นี่คือดังนั้นสร้างรหัสที่สั้นที่สุดสำหรับฟังก์ชันหรือโปรแกรมที่คำนวณฟังก์ชัน Mertens สำหรับจำนวนเต็มอินพุตn > 0
  • นี่คือลำดับ OEIS A002321

กรณีทดสอบ

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


เราสามารถคืนค่าTrueแทน1 ได้หรือไม่? การอภิปรายเมตาที่เกี่ยวข้อง: ควรอนุญาตให้ใช้บูลีนเมื่อจำเป็นต้องใช้ตัวเลขหรือไม่
เดนนิส

@Dennis แน่นอนว่าภาษาของคุณแปลความจริงเป็น 1
ไมล์

คำตอบ:


6

เยลลี่ 6 ไบต์

:Ḋ߀SC

ลองออนไลน์! หรือตรวจสอบกรณีทดสอบที่มีขนาดเล็ก (ใช้เวลาสักครู่)

พื้นหลัง

สิ่งนี้ใช้คุณสมบัติ

property by David W. Wilson

จากA002321ซึ่งนำไปสู่สูตรแบบเรียกซ้ำต่อไปนี้

recursive formula

มันทำงานอย่างไร

:Ḋ߀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.

11

Mathematica, 22 20 ไบต์

ขอบคุณ @miles สำหรับการบันทึก 2 ไบต์

Tr@*MoebiusMu@*Range

คำอธิบาย

Range

สร้างรายการจาก 1 ถึงอินพุต

MoebiusMu

ค้นหาMoebiusMuของแต่ละหมายเลข

Tr

สรุปผล


2
ฉันชอบที่ Mathematica มี builtin สำหรับทุกสิ่ง แต่โดยปกติแล้วจะยาวกว่าภาษากอล์ฟอยู่แล้ว = D
DJMcMayhem

5
การเรียก mthmca อีกครั้งเวอร์ชัน Mathematica ที่เหมาะสมที่สุดของคำสั่งชื่อ
Michael Stern

11

Python 2, 45 37 bytes

f=lambda n,k=2:n<k or f(n,k+1)-f(n/k)

ทดสอบบนIdeone

พื้นหลัง

สิ่งนี้ใช้คุณสมบัติ

property by David W. Wilson

จากA002321ซึ่งนำไปสู่สูตรแบบเรียกซ้ำต่อไปนี้

recursive formula

มันทำงานอย่างไร

เราใช้การเรียกซ้ำไม่เพียงเพื่อคำนวณMสำหรับค่าอ้างอิง แต่เพื่อคำนวณผลรวมของภาพเหล่านั้นเช่นกัน สิ่งนี้จะช่วยประหยัด 8 ไบต์ในการติดตั้งต่อไปนี้ที่ไม่ซับซ้อน

M=lambda n:1-sum(M(n/k)for k in range(2,n+1))

เมื่อเรียกว่ามีอาร์กิวเมนต์เดียวn , ตัวเลือกอาร์กิวเมนต์kเริ่มต้นที่2

ถ้าn = 1ให้n<kผลตอบแทนจริงและfคืนค่านี้ นี่คือกรณีฐานของเรา

ถ้าn> 1 , n<kแรกผลตอบแทนที่เป็นเท็จและต่อไปนี้รหัสorจะถูกดำเนินการ ซ้ำคำนวณระยะหนึ่งของทุนซึ่งจะหักออกจากค่าตอบแทนของf(n/k) f(n,k+1)เพิ่มขึ้นหลังkและซ้ำเรียกจึงวนมากกว่าค่าความเป็นไปได้ของk เมื่อn <k + 1หรือn = 1 , f(n,k+1)จะกลับ1สิ้นสุดเรียกซ้ำ


ว้าวนั่นสั้นกว่าการนำไปใช้ของ Mobius codegolf.stackexchange.com/a/70024/34718
mbomb007

สั้นกว่ามาก :) เอาล่ะ
เดนนิส


7

Brachylog , 22 20 ไบต์

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

5

เยลลี่ขนาด 9 ไบต์

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].


3

เยลลี่ขนาด 7 ไบต์

Ị*%ðþÆḊ

ไม่ค่อยมีประสิทธิภาพ ดีเทอร์มิแนนต์นั้นยาก

ลองออนไลน์! หรือตรวจสอบกรณีทดสอบที่เล็กกว่าตรวจสอบกรณีทดสอบที่มีขนาดเล็ก(ใช้เวลาสักครู่)

พื้นหลัง

สิ่งนี้ใช้สูตรจากA002321 :

M (n)เป็นปัจจัยของเมทริกซ์บูลn × nที่ฉัน jคือ1ถ้าJ = 1หรือฉัน | เจและ 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.

3

PHP, 113 ไบต์

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

2

แร็กเก็ต 103 ไบต์

(λ(N)(for/sum((n(range 1 N)))(define c(length(factorize n)))(cond[(= 0 c)0][(even? c)1][(odd? c)-1])))

Ungolfed:

(define f
  (λ(N)
    (for/sum ((n (range 1 N)))
      (define c (length (factorize n)))
      (cond
        [(= 0 c) 0]
        [(even? c) 1]
        [(odd? c) -1]))))

2

CJam (20 ไบต์)

qiM{_,:)(@@f/{j-}/}j

การสาธิตออนไลน์

ใช้สูตรจาก OEIS

sum(k = 1..n, a([n/k])) = 1. - David W. Wilson, 27 ก.พ. 2012

และผู้ให้บริการบันทึกความจำของ 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

2

JavaScript (ES6), 50 ไบต์

n=>[1,...Array(n-1)].reduce((r,_,i)=>r-f(n/++i|0))

คำตอบ Python ของพอร์ตออฟ @ เดนนิส


2

Julia, 26 25 ไบต์

!n=1-sum(map(!,n÷(2:n)))

ลองออนไลน์!

พื้นหลัง

สิ่งนี้ใช้คุณสมบัติ

คุณสมบัติโดย David W. Wilson

จากA002321ซึ่งนำไปสู่สูตรแบบเรียกซ้ำต่อไปนี้

สูตรเรียกซ้ำ

มันทำงานอย่างไร

เรากำหนดผู้ประกอบการเอก! สำหรับวัตถุประสงค์ของเรา

n÷(2:n)คำนวณผลคูณที่จำเป็นทั้งหมด, นิยามใหม่ของเรา! เป็นแมปเหนือพวกเขาและในที่สุดผลรวมของการโทร recursive ทั้งหมดจะถูกหักออกจาก1

น่าเสียดาย,

!n=1-sum(!,n÷(2:n))

ไม่ทำงานเนื่องจากผลรวม dyadic จะทำให้หายใจไม่ออกบนคอลเลกชันที่ว่างเปล่า

!n=n<2||1-sum(!,n÷(2:n))

การแก้ไขนี้ แต่มันไม่ได้บันทึกไบต์ใด ๆ และผลตอบแทนที่แท้จริงสำหรับการป้อนข้อมูล1



1

สกาลา, 53 ไบต์

def?(n:Int,k:Int=2):Int=if(n<k)1 else?(n,k+1)- ?(n/k)

พอร์ตของคำตอบของไพเพนในเดนนิส

ฉันเรียกวิธีการ?นี้ว่าเป็นโทเค็นที่ไม่ยึดติดกับตัวอักษร



1

ที่จริงแล้ว18 17 16 ไบต์

ยินดีต้อนรับคำแนะนำการเล่นกอล์ฟ ลองออนไลน์!

R`;y;l0~ⁿ)π=*`MΣ

Ungolfing

         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.


0

J, 19 ไบต์

1#.1*/@:-@~:@q:@+i.

คำนวณฟังก์ชัน Mertens nโดยใช้ผลรวมของฟังก์ชันMöbiusในช่วง[1, n]การใช้ผลรวมของฟังก์ชั่นMöbiusกว่าช่วง

การใช้

   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
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.