\ $n\$-完全数

Sep 14 2020

正の整数\$x\$は\$n\$- \の場合の完全数$\sigma(x) = nx\$、ここで\$\sigma(x)\$除数和関数です。たとえば、\$120\$は\$3\$-除数の合計が\になるため、完全数$360\$:

$$360 = 3\times120 = 1+2+3+4+5+6+8+10+12+15+20+24+30+40+60+120$$

そして

$$926073336514623897600 = 6\times154345556085770649600 = 1+2+3+4+5+6+7+8+9+10+11+12+\dots+51448518695256883200+77172778042885324800+154345556085770649600$$

だから\$154345556085770649600\$は\$6\$-完全数。

あなたは整数を取ることになっています\$x\$入力および出力として値\$n\$、そのような\$x\$は\$n\$-完全数。そのような\がない場合$n\$が存在する場合、正の整数ではない任意の一貫した値を出力できます。言語の範囲外の入力を受け取ることはありませんが、アルゴリズムは任意の大きさの\に対して機能する必要があります。$x\$。

これはコードゴルフなので、バイト単位の最短コードが優先されます。

ミニチャレンジ:ゼリーで5バイトビート

テストケース

    x -> n
    1 -> 1
    2 -> 0
    3 -> 0
    4 -> 0
    5 -> 0
    6 -> 2
   28 -> 2
  120 -> 3
  496 -> 2
  500 -> 0
  672 -> 3
30240 -> 4
154345556085770649600 -> 6

回答

9 Zgarb Sep 14 2020 at 11:28

殻、4バイト

¦¹ΣḊ

オンラインでお試しください!

最後のテストケースはタイムアウトします。

説明

¦¹ΣḊ   Input is a number x.
   Ḋ   List of divisors.
  Σ    Sum.
¦      Division if divisible, 0 if not
 ¹     by x.

¦ 通常は単なる分割可能性テストですが、ここではその戻り値が役立ちます。

8 Sisyphus Sep 14 2020 at 10:59

ゼリー、5バイト

R×iÆs

オンラインでお試しください!

説明:

      - Explanation (sample for input 6)
R     - Range ([1, 2, 3, 4, 5, 6])
 ×    - Multiply by input ([6, 12, 18, 24, 30, 36])
   Æs - Divisor sum (12)
  i   - Index of divisor sum in list, else 0 (2)
7 xash Sep 14 2020 at 05:24

Brachylog、6バイト

f+;?/ℕ

オンラインでお試しください!

使い方

f+;?/ℕ
f+     the sum of the factors
  ;?/ℕ divided by the input
     ℕ is a natural number

代替バージョン、私はよりクールだと思いますが、より長いです:

f+~×[?,.]∧
f+          the sum of the factors
  ~×        unifies with the multiplication of
    [?,.]   the input and the output
         ∧  return the output
6 Bismarck71 Sep 14 2020 at 14:36

05AB1E、 7つの 6バイト

Ý*IÑOk

オンラインでお試しください!

説明:

Ý*IÑOk>
Ý        0-Index inclusive range of input (6 -> [1, 2, 3, 4, 5, 6])
 *       Multiply by input ([6, 12, 18, 24, 30, 36])
  IÑO    Get input -> divisors -> sum (6 -> [1, 2, 3, 6] -> 12)
     k   0-Index of divisor-sum in array or -1 if not found. ([6, >12<, 18, 24, 30, 36] -> 1)

私はちょうどSisyphusの方法を使用しました。これはおそらくゴルフダウンするか、さらに効率的にすることができますが、そうするための05AB1Eの知識が不足しています。時間を過ごすためにそれを試してみようと思っただけです。

-ovsのおかげで1バイト

5 AZTECCO Sep 14 2020 at 07:30

C(gcc)、47バイト

s,i;f(x){for(i=s=x;--i;)s+=x%i?0:i;s/=s%x*s+x;}

オンラインでお試しください!

nまたはを返します0。

5 JoKing Sep 14 2020 at 11:23

APL(Dyalogユニコード)、16の15 13バイト

指摘してくれたBubblerのおかげで、出力形式を変更して数バイト節約できます

⍸×∘⍳⍨=1⊥∘∪⊢∨⍳

オンラインでお試しください!

存在するn場合はシングルトンリストを出力し、n存在しない場合は空の配列を出力します。⍸(1⊥)の除数(∪⊢∨⍳)の合計が()=入力(×∘⍳⍨)の倍数に等しい()のインデックスを検索します。リストの長さではなく、要素が存在しない場合に空のリストを返すため、インデックスを見つけるためだけに⍸とを使用します。=⍳

4 KevinCruijssen Sep 14 2020 at 16:17

空白、153バイト

[S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][S N
S _Duplicate_input][S N
S _Duplicate_input][N
S S N
_Create_Label_LOOP][S S S T N
_Push_1][T  S S T   _Subtract][S N
S _Duplicate][N
T   S S N
_If_0_Jump_to_Label_REACHED_ZERO][S T   S S T   S N
_Copy_0-based_2nd_input][S T    S S T   N
_Copy_0-based_1st_integer][T    S T T   _Modulo][N
T   S T N
_If_0_Jump_to_Label_ADD_TO_SUM][N
S N
N
_Jump_to_Label_LOOP][N
S S T   N
_Create_Label_ADD_TO_SUM][S N
T   _Swap_top_two][S T  S S T   N
_Copy_0-based_1st_integer][T    S S S _Add_top_two][S N
T   _Swap_top_two][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_REACHED_ZERO][S N
N
_Discard_top][S N
S _Duplicate_top][S T   S S T   S N
_Copy_0-based_2nd_input][T  S T T   _Modulo][N
T   S S S N
_If_0_Jump_to_Label_DIVISIBLE][S S S N
_Push_0][N
S N
S T 
_Jump_to_Label_OUTPUT][N
S S S S N
_Create_Label_DIVISIBLE][S N
T   _Swap_top_two][T    S T S _Integer_divide_top_two][N
S S S T N
_Create_Label_OUTPUT][T N
S T _Output_as_integer]

文字S(スペース)、T(タブ)、およびN(改行)が強調表示のみとして追加されました。
[..._some_action]説明のみとして追加。

オンラインでお試しください(生のスペース、タブ、新しい行のみ)。

擬似コードでの説明:

Integer input = STDIN as input
Integer sum = input
Integer i = input
Start LOOP:
  i = i - 1
  If(i == 0):
    Jump to Label REACHED_ZERO
  If(input % i == 0):
    sum = sum + i
  Go to next iteration of LOOP

Label REACHED_ZERO:
  Integer output
  If(sum % input == 0):
    output = sum integer-divided by input
  Else:
    output = 0

  Print output as integer to STDOUT

実行例: input = 6

Command    Explanation                       Stack         Heap   STDIN  STDOUT  STDERR

SSSN       Push 0                            [0]
SNS        Duplicate top (0)                 [0,0]
TNTT       Read STDIN as integer             [0]           {0:6}  6
TTT        Retrieve at address (0)           [6]           {0:6}
SNS        Duplicate top (6)                 [6,6]         {0:6}
SNS        Duplicate top (6)                 [6,6,6]       {0:6}
NSSN       Create Label LOOP                 [6,6,6]       {0:6}
 SSSTN     Push 1                            [6,6,6,1]     {0:6}
 TSST      Subtract top two (6-1)            [6,6,5]       {0:6}
 SNS       Duplicate top (5)                 [6,6,5,5]     {0:6}
 NTSSN     If 0: Jump to Label REACHED_ZERO  [6,6,5]       {0:6}
 STSSTSN   Copy 0-based 2nd (6)              [6,6,5,6]     {0:6}
 STSSTN    Copy 0-based 1st (5)              [6,6,5,6,5]   {0:6}
 TSTT      Modulo top two (6%5)              [6,6,5,1]     {0:6}
 NTSTN     If 0: Jump to Label ADD_TO_SUM    [6,6,5]       {0:6}
 NSNN      Jump to Label LOOP                [6,6,5]       {0:6}

 SSSTN     Push 1                            [6,6,5,1]     {0:6}
 TSST      Subtract top two (5-1)            [6,6,4]       {0:6}
 SNS       Duplicate top (4)                 [6,6,4,4]     {0:6}
 NTSSN     If 0: Jump to Label REACHED_ZERO  [6,6,4]       {0:6}
 STSSTSN   Copy 0-based 2nd (6)              [6,6,4,6]     {0:6}
 STSSTN    Copy 0-based 1st (4)              [6,6,4,6,4]   {0:6}
 TSTT      Modulo top two (6%4)              [6,6,4,2]     {0:6}
 NTSTN     If 0: Jump to Label ADD_TO_SUM    [6,6,4]       {0:6}
 NSNN      Jump to Label LOOP                [6,6,4]       {0:6}

 SSSTN     Push 1                            [6,6,4,1]     {0:6}
 TSST      Subtract top two (4-1)            [6,6,3]       {0:6}
 SNS       Duplicate top (3)                 [6,6,3,3]     {0:6}
 NTSSN     If 0: Jump to Label REACHED_ZERO  [6,6,3]       {0:6}
 STSSTSN   Copy 0-based 2nd (6)              [6,6,3,6]     {0:6}
 STSSTN    Copy 0-based 1st (3)              [6,6,3,6,3]   {0:6}
 TSTT      Modulo top two (6%3)              [6,6,3,0]     {0:6}
 NTSTN     If 0: Jump to Label ADD_TO_SUM    [6,6,3]       {0:6}
 NSSTN     Create Label ADD_TO_SUM           [6,6,3]       {0:6}
  SNT      Swap top two                      [6,3,6]       {0:6}
  STSSTN   Copy 0-based 1st (3)              [6,3,6,3]     {0:6}
  TSSS     Add top two (6+3)                 [6,3,9]       {0:6}
  SNT      Swap top two                      [6,9,3]       {0:6}
  NSNN     Jump to Label LOOP                [6,9,3]       {0:6}

 SSSTN     Push 1                            [6,9,3,1]     {0:6}
 TSST      Subtract top two (3-1)            [6,9,2]       {0:6}
 SNS       Duplicate top (2)                 [6,9,2,2]     {0:6}
 NTSSN     If 0: Jump to Label REACHED_ZERO  [6,9,2]       {0:6}
 STSSTSN   Copy 0-based 2nd (6)              [6,9,2,6]     {0:6}
 STSSTN    Copy 0-based 1st (5)              [6,9,2,6,2]   {0:6}
 TSTT      Modulo top two (6%5)              [6,9,2,0]     {0:6}
 NTSTN     If 0: Jump to Label ADD_TO_SUM    [6,9,2]       {0:6}
  SNT      Swap top two                      [6,2,9]       {0:6}
  STSSTN   Copy 0-based 1st (2)              [6,2,9,2]     {0:6}
  TSSS     Add top two (9+2)                 [6,2,11]      {0:6}
  SNT      Swap top two                      [6,11,2]      {0:6}
  NSNN     Jump to Label LOOP                [6,11,2]      {0:6}

 SSSTN     Push 1                            [6,11,2,1]    {0:6}
 TSST      Subtract top two (2-1)            [6,11,1]      {0:6}
 SNS       Duplicate top (1)                 [6,11,1,1]    {0:6}
 NTSSN     If 0: Jump to Label REACHED_ZERO  [6,11,1]      {0:6}
 STSSTSN   Copy 0-based 2nd (6)              [6,11,1,6]    {0:6}
 STSSTN    Copy 0-based 1st (1)              [6,11,1,6,1]  {0:6}
 TSTT      Modulo top two (6%1)              [6,11,1,0]    {0:6}
 NTSTN     If 0: Jump to Label ADD_TO_SUM    [6,11,1]      {0:6}
  SNT      Swap top two                      [6,1,11]      {0:6}
  STSSTN   Copy 0-based 1st (1)              [6,1,11,1]    {0:6}
  TSSS     Add top two (11+1)                [6,1,12]      {0:6}
  SNT      Swap top two                      [6,12,1]      {0:6}
  NSNN     Jump to Label LOOP                [6,12,1]      {0:6}

 SSSTN     Push 1                            [6,12,1,1]    {0:6}
 TSST      Subtract top two (1-1)            [6,12,0]      {0:6}
 SNS       Duplicate top (1)                 [6,12,0,0]    {0:6}
 NTSSN     If 0: Jump to Label REACHED_ZERO  [6,12,0]      {0:6}
 NSSSN     Create Label REACHED_ZERO         [6,12,0]      {0:6}
  SNN      Discard top (0)                   [6,12]        {0:6}
  SNS      Duplicate top (12)                [6,12,12]     {0:6}
  STSSTSN  Copy 0-based 2nd (6)              [6,12,12,6]   {0:6}
  TSTT     Modulo top two (12%6)             [6,12,0]      {0:6}
  NTSSSN   If 0: Jump to Label DIVISIBLE     [6,12]        {0:6}
  NSSSSN   Create Label DIVISIBLE            [6,12]        {0:6}
   SNT     Swap top two                      [12,6]        {0:6}
   TSTS    Integer-divide top two (12/6)     [2]           {0:6}
   NSSSTN  Create Label OUTPUT               [2]           {0:6}
    TNST   Output top as integer (2)         []            {0:6}         2
                                                                                 error

出口が定義されていないため、結果の出力後にエラーで停止します。

4 Mukundan314 Sep 14 2020 at 09:38

Pyth、15バイト

&!%Jsf!%QTSQQ/J

オンラインでお試しください!

説明

&!%Jsf!%QTSQQ/J
   J              # set J to
    s             # sum of
     f    SQ      # filtering the range [1, input] with
      !%QT        # lambda T: not (input % T)    (divisibility test)
                  # implicit print the
&                 # short-circuiting and of
 !%J        Q     # not (J % input)
             /J   # and J / input
3 Arnauld Sep 14 2020 at 04:59

JavaScript(ES6)、41バイト

解決策がない場合は0を返します。

x=>(g=k=>x=k&&k*!(x%k)/x+g(k-1))(x)%1?0:x

オンラインでお試しください!

3 JonathanAllan Sep 14 2020 at 05:22

ゼリー、6バイト

Æs0:%?

非負の整数を生成する正の整数を受け入れるモナドリンク。

オンラインでお試しください!または、テストスイートを参照してください。

どうやって?

Æs0:%? - Link: x
Æs     - divisor sum
     ? - if...
    %  - ...condition: has a remainder when divided
  0    - ...then: zero
    :  - ...else: integeger divide
3 Razetime Sep 14 2020 at 11:22

APL(Dyalogユニコード)、19の18バイト

⊢(÷⍨×0=|)1⊥∘⍸0=⍳|⊢

オンラインでお試しください!

Jo Kingによるトレーニングへの変換(-3バイト)

-チェック条件を変更した後、JoKingからさらに1バイト。

古い回答、22バイト

{(⊢×⌊=⊢)⍵÷⍨+/⍸0=⍵|⍨⍳⍵}

説明

{(⊢×⌊=⊢)⍵÷⍨+/⍸0=⍵|⍨⍳⍵} ⍵ → input
                   ⍳⍵  range 1-⍵
                ⍵|⍨    mod ⍵
              0=       check which ones are divisors
             ⍸         get the indices (factors)
           +/          sum the factors
        ⍵÷⍨            divide by ⍵
 (⊢×⌊=⊢)               Inner tacit fn:
    ⌊=⊢                Floor equals right? (integer test, returns 0 or 1)
  ⊢×                   times right 
3 ovs Sep 14 2020 at 06:15

ハスケル、51の46バイト

a!b=0^mod a b*div a b
f n=sum(map(n!)[1..n])!n

オンラインでお試しください!

3 J42161217 Sep 14 2020 at 06:24

Wolfram言語(Mathematica)、30バイト

Tr@Divisors@#/#/._Rational->0&

オンラインでお試しください!

-@ attから6バイト

3 Neil Sep 14 2020 at 05:55

木炭、23の、20バイト

NθI⌕E⊕θ×θιΣΦ⊕θ∧ι¬﹪θι

オンラインでお試しください!リンクは、コードの詳細バージョンへのリンクです。@Sisyphusのアルゴリズムの移植ですが、@ ovsのコメントを使用して0インデックスを処理します。-1存在しない場合の出力。説明:

Nθ                      Input `x` as a number
             θ          `x`
            ⊕           Incremented
           Φ            Filter over implicit range
               ι        Current index
              ∧         Logical AND
                  θ     `x`
                 ﹪      Modulo
                   ι    Current index
                ¬       Logical NOT
          Σ             Take the sum
      θ                 `x`
     ⊕                  Incremented
    E                   Map over implicit range
        θ               `x`
       ×                Multiplied by
         ι              Current index
   ⌕                    Find the index
  I                     Cast to string
                        Implicitly print

残念ながら、Charcoalの場合、の合計[]はゼロではありません。つまり、の2つの増分を削除し、x代わりに結果を増分することによってバイトを保存することはできません。

以前の23バイトのソリューション:

Nθ≔ΣΦ⊕θ∧ι¬﹪θιη¿¬﹪ηθI÷ηθ

オンラインでお試しください!リンクは、コードの詳細バージョンへのリンクです。説明:

Nθ

入力x。

≔ΣΦ⊕θ∧ι¬﹪θιη

からリストを作成し、1..x除算しない数値を除外してx、合計を取ります。

¿¬﹪ηθI÷ηθ

x合計を除算する場合は、商を出力します。

3 DominicvanEssen Sep 14 2020 at 14:55

R、42 4139バイト

編集:ロビンライダーのおかげで-1バイト(そして、これに触発されて、さらに-2バイト)

function(x)(d=sum(1:x*!x%%1:x))/x*!d%%x

オンラインでお試しください!

コメント:

perfect_n=
function(x)
 (d=            # d is the divisor sum, calculated as...
  sum(          # sum of...
   1:x*         # the values of 1..x that have...
    !           # zero values for...
     x%%1:x)    # x MOD 1..x
  )
 )/x            # output d/x...
    *!d%%x      # but only if it's an integer 
                # (so d MOD x == 0)
2 user Sep 14 2020 at 05:38

スカラ座、54の、53バイト

x=>{val s=1 to x filter(x%_<1)sum;s/x*(1-(s%x).sign)}

Scastieでお試しください

x1からxまでのすべての除数を合計します。その合計がxで割り切れる場合は、xで割った値を返します。それ以外の場合は、0を返します。

2 Neil Sep 14 2020 at 06:05

網膜、51バイト

.+
*
|""Lw`^(.+)(?=\1*$) ^ $-1;
L$`^(.+);(\1)+$
$#2

オンラインでお試しください!リンクには、それほど低速ではないテストケースが含まれています。説明:

.+
*

入力を1進数に変換します。

|""Lw`^(.+)(?=\1*$)

それらを区切ることなくすべての要因をリストし、したがってそれらを合計します。

^
$-1;

元の単項値を取得します。

L$`^(.+);(\1)+$ $#2

合計を除算する回数を数えます。(または、出力されない場合は何も出力しません。)

2 LuisMendo Sep 14 2020 at 06:33

オクターブ、36の34バイト

@(x)~mod(s=~mod(x,r=1:x)*r',x)*s/x

浮動小数点数または整数を入力として受け取る匿名関数。最後のテストケースは、メモリの制限のために失敗します。

オンラインでお試しください!または、テストケースを確認します。

説明

@(x)~mod(s=~mod(x,r=1:x)*r',x)*s/x

@(x)                                 % anonymous function with input x
                    1:x              % row vector [1 2 ... x]
                  r=                 % call that r
            mod(x,     )             % x modulo [1 2 ... x]. Gives a row vector
           ~                         % negate each element. Gives 1 for divisors
                         r'          % column vector [1; 2; ... ; x]
                        *            % matrix-multiply. Gives the sum of divisors
         s=                          % call that s
     mod(                  ,x)       % sum of divisors modulo x
    ~                                % negate. Gives 1 if x divides sum of divisors
                               s/x   % sum of divisors divided by x
                              *      % multiply
2 Mukundan314 Sep 14 2020 at 12:10

Python 3.8(プレリリース)、62バイト

lambda x:(a:=sum(x/i*(x%i<1)for i in range(1,x+1)))%x<1and a/x

オンラインでお試しください!

2 KevinCruijssen Sep 14 2020 at 17:29

MathGolf、7バイト

─Σk‼÷/*

オンラインでお試しください。

説明:

─       # Get the divisors of the (implicit) input-integer
 Σ      # Sum those divisors
  k     # Push the input-integer again
   ‼    # Apply the following two commands separately to the stack:
    ÷   #  Check if the divisor-sum is divisible by the input (1 if truthy; 0 if falsey)
    /   #  Integer-divide the divisor-sum by the input
     *  # Multiply the two together
        # (after which the entire stack joined together is output implicitly as result)
2 Shaggy Sep 14 2020 at 18:22

ロックスター、141の 135 131バイト

n存在しない場合は何も出力しません。

listen to N
X's0
T's0
while N-X
let X be+1
let D be N/X
turn up D
let T be+D is N/X and X

let D be T/N
turn up D
if D is T/N
say D

ここで試してください(コードを貼り付ける必要があります)

1 GalenIvanov Sep 14 2020 at 13:51

アイコン、67バイト

procedure f(n)
s:=0
n%(i:=1to n)=0&s+:=i&\z
return(0=s%n&s/n)|0
end

オンラインでお試しください!

1 Shaggy Sep 14 2020 at 14:00

Japt -æ、7バイト

見つからundefinedない場合に出力しnます。

*N¶Îâ x

それを試してみてください

1 GalenIvanov Sep 14 2020 at 14:17

因数分解、84バイト

: f ( n -- n ) dup [1,b] [ dupd mod 0 = ] filter sum swap /mod 0 > [ drop 0 ] when ;

オンラインでお試しください!

1 user Sep 15 2020 at 02:19

Prolog、117バイト

s(X,D,S):-D<1,!,S is 0;E is D-1,(0 is X mod D,!,s(X,E,T),S is T+D;s(X,E,S)).
f(X,N):-s(X,X,S),0 is S mod X,N is S//X.

オンラインでお試しください!(直接変更しないでください。バージョンも変更されます)


この短いバージョン(96バイト)が機能しない理由を誰かが理解できれば、私は本当に感謝しています。

s(X,D,S):-D<1,!,S is 0;E is D-1,(0 is X mod D,!,s(X,E,T),S is T+D;s(X,E,S)).
f(X,N):-s(X,X,N*X).

印刷デバッグ付きバージョン

1 2014MELO03 Sep 17 2020 at 02:48

GolfScript、22バイト

~:x),{.x\%!*+}*.x%!*x/

オンラインでお試しください!

~:x                     # Store the input in x
   ),                   # Make an array from 0 to x
     {       }*         # For each number in the array, execute this block
      .                 # Copy current number
       x\%!             # The copy becomes 1 if it is a divisor of x and 0 if it isn't
           *+           # Multiply and add
               .        # Copy the sum of the divisors
                x%!     # The copy becomes 1 if it is a divisor of x and 0 if it isn't
                   *    # Multiply
                    x/  # Divide by x