与えられたサイズのアーベル群を数える


14

バックグラウンド

前回は、特定のサイズのグループカウントしましたが、これは重要な問題です。

今回は、アーベルのグループ、つまり可換演算を持つグループのみをカウントします。正式に、基(G、*)がアーベルである場合、X * Y = Y * XすべてのためのためのX、YにおけるG

この方法で問題ははるかに単純になるため、効率的にカウントします。

仕事

入力として負でない整数nを受け入れ、次数nの非同型アーベル群の数を出力または返すプログラムまたは関数を作成します。

グループの数を計算する1つの方法(A(n)で示しますは、次のことを確認することです。

  • A(0)= 0

  • 場合、pは素数であり、A(Pのkはの整数パーティションの数に等しいK。(cfr。OEIS A000041

  • もしN = MK、及びMkが互いに素であり、A(N)= A(M)A(K)

これまたはA(n)を計算する他の方法を使用できます。

テストケース

Input               Output
0                   0
1                   1
2                   1
3                   1
4                   2
5                   1
6                   1
7                   1
8                   3
9                   2
10                  1
11                  1
12                  2
13                  1
14                  1
15                  1
16                  5
17                  1
18                  2
19                  1
20                  2
4611686018427387904 1300156
5587736968198167552 155232
9223371994482243049 2

OEIS A000688から取得

追加のルール

  • 十分な時間、RAM、および入力を保持できるレジスタサイズがあれば、コードは(理論的には)任意の大きな整数で動作するはずです。

  • あなたのコードは、すべての整数間のために働かなければなら02 63 1 -私のマシンで10分(インテルi7-3770、16ジブRAM、Fedoraの21)の下で、終了。

    回答を送信する前に、最後の3つのテストケースのコードのタイミングを確認してください。

  • Mathematicaのような、このタスクを単純化するビルトインFiniteAbelianGroupCountは許可されていません。

  • 整数のパーティションまたはリストのパーティションを返すまたはカウントするビルトインは許可されていません。

  • 標準の規則が適用されます。


Pythの素因数分解システムはこの課題に対して遅すぎるため、修正する必要があります。
isaacg

回答:


3

CJam(39 38バイト)

qimF{~M\{_ee{~\)<1b}%1+a\+}*0=1be&}%:*

オンラインデモ

これは、素因数分解(mF)を見つけてから、各べきのパーティションを計算し、それらの積を取得するという推奨された行に従います。

以下のための2つの特殊なケースがありますmF:それは要因0として0^11として1^1。後者は特別な処理を必要としません。サイズ1のAbelianグループが1つ、パーティションが1の1つです。ただし、ゼロには特別なケースは必要ありません。

パーティションカウントでは、の繰り返しを使用して、A008284(n, k)に分割するパーティションの数を指定nkます。OEISでは

T(n, k) = Sum_{i=1..k} T(n-k, i), for 1<=k<=n-1; T(n, n) = 1 for n >= 1.

私はそれが至るまでのように合計を考えるために、より便利だと思う1min(k, n-k)

解剖

q~              e# Parse input into an integer
mF              e# Factorise it
{               e# For each factor p^a
  ~             e#   Split the array [p a]
                e#   The following lines count partitions of a
                e#   (Note: they would be buggy if a were ever 0, but it isn't)
  M\{           e#   Starting with a table of zero rows, repeat a times
    _ee         e#     Copy table and pair each row with its index
    {~\)<1b}%   e#     Extract that prepended index and use it to sum for each j
                e#     the first jth items of row j
    1+          e#     Append a 1 for P(i, i)
    a\+         e#     Prepend the new row to the table (which is stored in reverse)
  }*
  0=1b          e#   Sum the elements in the latest (first) row

  e&            e#   If p was 0 then replace with 0
}%
:*              e# Take the product

5

CJam、50 49 47 43バイト

ri_mF{1=_L{1$0>{,f{):X-Xj}:+}{;!}?}2j}%:*e&

CJamの組み込みのmF因数分解と、このPythonパーティション番号関数のメモされたポートを使用します。

p=lambda n,x:n==0 or n>0and sum(p(n+~a,a+1)for a in range(x))

または無料:

def p(n, x): # Call like p(n, n). n is number remaining, x is max part size
  if n > 0:
    return sum(p(n-a-1,a+1)for a in range(x))
  else:
    return (n==0)

@RetoKoradiの答えのように、最後のケースはオフラインインタープリターで約17秒かかります。これは、CJamが数値を因数分解するのにかかる時間だからです。したがって、このオンラインテストスイートから除外しました。

完全な説明

[Main body]
ri                                Read input and convert to int
  _          e&                   Logical AND input with final result to special case 0 
   mF                             Factorise input into [base, exponent] pairs
     {...}%                       Map, converting each pair to a partition number
           :*                     Take product

[Pair -> partition]
1=_                               Get exponent and copy (n,x in above Python)
   L                              Initialise empty cache
    {                       }2j   Memoise with 2 arguments
     1$0>                         Check if n > 0
         {            }{  }?      Execute first block if yes, else second block
                        ;!        Return (n == 0)
          ,f{      }              For each a in range(x) ...
             ):X-Xj               Call p(n-a-1,a+1) recursively
                    :+            Sum the results

4

Mathematica、96 94 88バイト

f=1##&@@#&;f[SeriesCoefficient[1/f[1-x^Range@#],{x,0,#}]&/@Last/@FactorInteger@#]Sign@#&

私はMathematicaにそれほど精通していませんが、試してみたいと思いました。-6バイトの@MartinBüttnerに感謝します。

これは、整数パーティションの生成関数式を使用します。


3

CJam、58バイト

li_mF{1=_L{_1>{_2$<{\;_j}{\,f{)_@\-j}:+}?}{;;1}?}2j}%:*\g*

オンラインで試す

最後のテスト例は、オンラインインタープリターで永遠に(または少なくとも私が待っていたよりも長く)かかりますが、ラップトップでCJamのオフラインバージョンを使用すると17秒で終了します。他のすべてのテスト例はほとんど瞬時です。

これは、mF指数を含む素因数分解を行うCJam 演算子を使用します。結果は、各指数のパーティションカウントの積になります。

コードの主要な部分は、パーティションカウントの計算です。Wikipediaページでjメモ化を伴う再帰をサポートする演算子を使用して、再帰アルゴリズムを実装しました。

説明:

li    Get input and convert to int.
_     Make a copy to handle 0 special case at the end.
mF    Factorization with exponents.
{     Loop over factors.
  1=    Take exponent from [factor exponent] pair.
  _     Repeat it, recursive calls are initiated with p(n, n).
  L     Empty list as start point of memoization state.
  {     Start recursive block. Argument order is (m, n), opposite of Wikipedia.
    _1>   Check for n > 1.
    {     Start n > 1 case.
      _2$   Copy both m and n.
      <     Check for n < m.
      {     n < m case.
        \;    Pop m.
        _     Copy n.
        j     Make the p(n, n) recursive call.
      }     End n < m case.
      {     Main part of algorithm that makes recursive calls in loop.
        \,    Generate [0 1 ... m-1] range for k.
        f{    Start loop over k.
          )     Increment, since k goes from 1 to m.
          _     Copy k.
          @\    Rotate n to top, and swap. Now have k n k at top of stack.
          -     Subtract, now have k n-k at top of stack.
          j     Make the p(n-k, k) recursive call.
        }     End loop over k.
        :+    Sum up all the values.
      }?    Ternaray operator for n < m condition.
    }     End n > 1 case.
    {     n <= 1 case.
      ;;1   Pop m, n values, and produce 1 as result.
    }?    Ternary operator for n > 1 condition.
  }2j   Recursive call with memoization, using 2 values.
}%    End loop over factors.
:*    Multiply all values.
\     Swap original input to top.
g     Signum.
*     Multiply to get 0 output for 0 input.
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.