除数の豊富な番号と不良な番号


18

前書き

整数の奇妙な世界では、除数は資産に似ており、逆数よりも約数が多い数を「豊富」と呼び、逆数よりも約数が少ない数を「不良」と呼びます。

例えば、番号2401 5つの除数有する:17493432401、その反転、つつ1042:、4つだけ有している125211042
したがって、2401豊かな数と呼ばれ、1042不十分数と呼ばれます。

この定義が与えられると、次の2つの整数列を作成できます。

(here we list the first 25 elements of the sequences)

 Index | Poor | Rich
-------|------|-------
     1 |   19 |   10
     2 |   21 |   12
     3 |   23 |   14
     4 |   25 |   16
     5 |   27 |   18
     6 |   29 |   20
     7 |   41 |   28
     8 |   43 |   30
     9 |   45 |   32
    10 |   46 |   34
    11 |   47 |   35
    12 |   48 |   36
    13 |   49 |   38
    14 |   53 |   40
    15 |   57 |   50
    16 |   59 |   52
    17 |   61 |   54
    18 |   63 |   56
    19 |   65 |   60
    20 |   67 |   64
    21 |   69 |   68
    22 |   81 |   70
    23 |   82 |   72
    24 |   83 |   74
    25 |   86 |   75
   ... |  ... |  ...

ノート :

  • 数値の「反転」とは、デジタル反転、つまり10進数の桁が反転していることを意味します。一つ以上のゼロで終わる番号が「短い」逆転を持っていることを、この手段は例えばの逆転が1900ある0091ので、91
  • 逆転と同じ数の除数を持つ整数、つまりOEIS:A062895に属する整数を意図的に除外します

チャレンジ

上記で定義した2つのシーケンスを考慮して、整数n(0または1のインデックスを選択できます)が与えられると、n番目の貧しい数とn番目の豊かな数を返すプログラムまたは関数を作成します。

入力

  • 整数(>= 00インデックスまたは>= 11インデックスの場合)

出力

  • 2つの整数、1つは貧弱なシーケンス用、もう1つは豊富なシーケンス用で、一貫している限り、好きな順序で

例:

INPUT          |   OUTPUT
----------------------------------
n (1-indexed)  |   poor    rich
----------------------------------
1              |   19      10
18             |   63      56
44             |   213     112
95             |   298     208
4542           |   16803   10282
11866          |   36923   25272
17128          |   48453   36466
22867          |   61431   51794
35842          |   99998   81888

一般的なルール:

  • これはであるため、バイト単位の最短回答が優先されます。
    コードゴルフ言語では、非コードゴルフ言語で回答を投稿しないようにしてください。「任意の」プログラミング言語の可能な限り短い答えを考えてみてください。
  • 標準のルールデフォルトのI / Oルールを使用した回答に適用されるため、STDIN / STDOUT、適切なパラメーターと戻り値型、完全なプログラムを備えた関数/メソッドを使用できます。あなたの電話。
  • デフォルトの抜け穴は禁止されています。
  • 可能であれば、コードのテストへのリンク(TIOなど)を追加してください。
  • また、回答の説明を追加することを強くお勧めします。

2
推測:番目の貧しい数は常にn番目の豊かな数よりも大きい。誰かがこれを証明できれば、おそらく多くの答えからバイトを削るでしょう。nn
ロビンライダー

@RobinRyder:それは本当だと思うが、それがまったく異なる物語であることを証明している:)
digEmAll

@RobinRyder先行ゼロのために、複数の数字が同じ逆の数字にマップできることを考慮してください(例:51、510、5100はすべて15にマップされます)。あらゆる数の、末尾ゼロで豊か対応する逆数値の無限数が存在するであろう(の余分な因子と10 100 1000年等)、悪い逆数値の一方のみ有限量。私はこれがそれを完全に証明するとは思わない(おそらくどこかに貧しい人々の幸運な連鎖があるだろう)が、少なくとも貧しい人々よりはるかに豊かな人々がいることを明示している。n101001000
ジョーキング

2
@JoKing「...貧乏人よりもはるかに豊かな数」この声明を明確にしたいかもしれません。書かれているように、豊かな数字のセットは貧しい数字のセットよりも大きなカーディナリティを持っていると言っていると解釈できます。しかし、もちろん、両方の集合は数え切れないほど無限です(どちらのシーケンスも終了しません):最初の桁がである素数が無限にたくさんあることを証明すれば十分2です。これについては、m-hikari.com / ijcmsn19, 199, 1999, ...
password

回答:


9

05AB1E、16バイト

∞.¡ÂÑgsÑg.S}¦ζsè

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


0インデックス付き[リッチ、プア]:

∞                # Push infinite list.
 .¡        }     # Split into three lists by...
   ÂÑgsÑg.S      # 1 if rich, 0 if nothing, -1 if poor.
            ¦ζ   # Remove list of nothings, and zip rich/poor together.
              sè # Grab nth element.

たぶん、このバージョンが終了しないように見える理由を誰かが説明できますが、TIOで「実行をキャンセル」をクリックすると、正しい答えで終了するか、60秒待つと正しい答えが得られます。「正しく」終了するバージョンでは、次を使用できます:T+nL.¡ÂÑgsÑg.S}¦ζsè+3バイト


Split-byは、無限リストではあまりうまく機能しないようです。
エミグナ

@Emigna個人的には、無限のリストがどのように可能であるかについては全くわかりません。
魔法のタコUr

遅延評価。不要な数を計算しないでください。したがって∞n5è、最初の6つの数値のみを計算します。これらのタイプのループ/グループ化/分割構造が再生されると、レイジー評価は失敗し、戻る前にすべてのアイテムを計算しようとします。
エミグナ

1
私はまだ€g。のために1バイトのビルトインがあるべきだと思います。私はそれを頻繁に使用しました。ここでバイトを(現在は同じバイトの)代替として保存していました‚рgÆ.±。いい答えだ!の素晴らしい使い方
ケビンクルーッセン

@KevinCruijssenもう2バイトはδg笑です。
マジックタコUr

6

JavaScript(ES6)、 121 115 113  111バイト

入力は1から始まります。として出力し[poor, rich]ます。

x=>[(s=h=(n,i=x)=>i?h(++n,i-=(g=(n,k=n)=>k&&!(n%k)*~-s+g(n,k-1))(n)>g([...n+''].reverse().join``)):n)``,h(s=2)]

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

コメント済み

ヘルパー機能

g = (n,                   // g is a helper function taking n and returning either the
        k = n) =>         // number of divisors or its opposite; starting with k = n
  k &&                    // if k is not equal to 0:
    !(n % k)              //   add either 1 or -1 to the result if k is a divisor of n
    * ~-s                 //   use -1 if s = 0, or 1 if s = 2
    + g(n, k - 1)         //   add the result of a recursive call with k - 1

メイン

x => [                    // x = input
  ( s =                   // initialize s to a non-numeric value (coerced to 0)
    h = (n,               // h is a recursive function taking n
            i = x) =>     // and using i as a counter, initialized to x
      i ?                 // if i is not equal to 0:
        h(                //   do a recursive call ...
          ++n,            //     ... with n + 1
          i -=            //     subtract 1 from i if:
            g(n)          //       the number of divisors of n (multiplied by ~-s within g)
            >             //       is greater than
            g(            //       the number of divisors of the reversal of n obtained ...
              [...n + ''] //         ... by splitting the digits
              .reverse()  //             reversing them
              .join``     //             and joining back
            )             //       (also multiplied by ~-s within g)
        )                 //   end of recursive call
      :                   // else:
        n                 //   we have reached the requested term: return n
  )``,                    // first call to h for the poor one, with n = s = 0 (coerced)
  h(s = 2)                // second call to h for the rich one, with n = s = 2
]                         // (it's OK to start with any n in [0..9] because these values
                          // are neither poor nor rich and ignored anyway)

4

ゼリー、22バイト

ṚḌ;⁸Æd
Ç>/$Ɠ©#żÇ</$®#Ṫ

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

1インデックス付きのを取る完全なプログラムnstdinで、その順序でn番目の貧弱整数のリストを返す。

説明

ṚḌ;⁸Æd | Helper link: take an integer and return the count of divisors fof its reverse and the original number in that order

Ṛ      | Reverse
 Ḍ     | Convert back from decimal digits to integer
  ;⁸   | Concatenate to left argument
    Æd | Count divisors


Ç>/$Ɠ©#żÇ</$®#Ṫ | Main link

    Ɠ©          | Read and evaluate a line from stdin and copy to register
   $  #         | Find this many integers that meet the following criteria, starting at 0 and counting up
Ç               | Helper link
 >/             | Reduce using greater than (i.e. poor numbers)
       ż        | zip with
           $®#  | Find the same number of integers meeting the following criteria
        Ç       | Helper link
         </     | Reduce using less than (i.e. rich numbers)
              Ṫ | Finally take the last pair of poor and rich numbers

4

Wolfram言語(Mathematica)、152バイト

(a=b=k=1;m=n={};p=AppendTo;While[a<=#||b<=#,#==#2&@@(s=0~DivisorSigma~#&/@{k,IntegerReverse@k++})||If[#<#2&@@s,m~p~k;a++,n~p~k;b++]];{m[[#]],n[[#]]}-1)&

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

推測が正しい場合、この140バイトのソリューションも機能します。

(a=k=1;m=n={};p=AppendTo;While[a<=#,#==#2&@@(s=0~DivisorSigma~#&/@{k,IntegerReverse@k++})||If[#<#2&@@s,m~p~k;a++,n~p~k]];{m[[#]],n[[#]]}-1)&   

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

貧乏対豊かなプロット はこちら

ここに画像の説明を入力してください


彼らが本当に近くに来るポイントは何ですか?
ジョーキング

1
@JoKing私はそれが信じていますa(27635)= {70003, 65892}
J42161217

1
すごい!ところで、これはおそらくTIOでn = 35842に到達できる数少ないソリューションの1つ(たぶん唯一のソリューション)です:)
digEmAll

3

Perl 6、81バイト

{(*>*,* <*).map(->&c {grep
{[[&c]] map {grep $_%%*,1..$_},$_,.flip},1..*})»[$_]}

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

  • * > *は、最初の引数が2番目の引数より大きい場合にtrueを返す匿名関数です。同様に* < *。前者はリッチシーケンスに属する番号を選択し、後者は貧弱なシーケンスに属する番号を選択します。
  • (* > *, * < *).map(-> &c { ... }) 一対の無限シーケンスを生成します。それぞれは、コンパレーター関数の1つに基づいています:リッチシーケンスと貧弱なシーケンス、この順序で。
  • »[$_]を使用してこれらのシーケンスの両方にインデックスを付け$_、トップレベル関数への引数を使用して、$_リッチシーケンスの$_thメンバーと不良シーケンスのthメンバーを含む2要素リストを返します。
  • grep $_ %% *, 1..$_の約数のリストを生成します$_
  • map { grep $_ %% *, 1..$_ }, $_, .flipの約数$_との約数の2要素リストを生成します$_その桁は、(「反転」)逆。
  • [[&c]]コンパレーター関数&c(より大かより小)でその2要素リストを縮小し、この数が貧弱なシーケンスの豊かなシーケンスに属するかどうかを示すブール値を生成します。

1..$_することができます^$_[$_]マップ関数内でを移動することもできます。78バイト
ジョーキング

3

パイソン2142の 141バイト

f=lambda i,n=1,a=[[],[]]:zip(*a)[i:]or f(i,n+1,[a[j]+[n]*(cmp(*[sum(x%y<1for y in range(1,x))for x in int(`n`[::-1]),n])==1|-j)for j in 0,1])

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



非再帰的な代替(他のPythonの回答に非常に似ています)

Python 2、143バイト

i=input()
a=[[],[]];n=1
while~i+len(zip(*a)):([[]]+a)[cmp(*[sum(x%i<1for i in range(1,x))for x in int(`n`[::-1]),n])]+=n,;n+=1
print zip(*a)[i]

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



2

ルビー、128バイト

入力はゼロインデックスです。[poor、rich]として出力します。

->n,*a{b=[];i=0;d=->z{(1..z).count{|e|z%e<1}};(x=d[i+=1];y=d[i.digits.join.to_i];[[],b,a][x<=>y]<<i)until a[n]&b[n];[a[n],b[n]]}

説明

->n,*a{                             # Anonymous function, initialize poor array
       b=[];i=0;                    # Initialize rich array and counter variable
    d=->z{(1..z).count{|e|z%e<1}};  # Helper function to count number of factors
    (                               # Start block for while loop
     x=d[i+=1];                     # Get factors for next number
     y=d[i.digits.join.to_i];       # Factors for its reverse
                                    # (digits returns the ones digit first, so no reversing)
     [[],b,a][x<=>y]                # Fetch the appropriate array based on
                                    #  which number has more factors
                    <<i             # Insert the current number to that array
    )until a[n]&b[n];               # End loop, terminate when a[n] and b[n] are defined
    [a[n],b[n]]                     # Array with both poor and rich number (implicit return)
}                                   # End function

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


2

Perl 6、76バイト

{classify({+(.&h <=>h .flip)},^($_*3+99)){-1,1}[*;$_]}
my&h={grep $_%%*,^$_}

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

SeanのPerl 6の答えは見ませんでしたが、これは別の方法で機能します。上限をとしてハードコーディングしていることに注意してくださいn*3+99。これはおそらく厳密には正しくありません。ただし、余分なバイトなし*3³を置き換えることができます。これにより、より正確であれば、プログラムの効率が大幅に低下します。


2

Python 2、152バイト

def f(n):
 a,b=[],[];i=1
 while not(a[n:]and b[n:]):[[],b,a][cmp(*[sum(m%-~i<1for i in range(m))for m in i,int(`i`[::-1])])]+=[i];i+=1
 return a[n],b[n]

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

ロッドの答えにかなり似ていることになります。ゼロインデックスのn貧しい、豊かなタプルを返します。




2

APL(Dyalog Unicode)、34バイト

{⍵⌷⍉1↓⊢⌸{×-/{≢∪⍵∨⍳⍵}¨⍵,⍎⌽⍕⍵}¨⍳1e3}

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

この怪物のゴルフを手伝ってくれたAdámとngnに感謝します。

TIOはより大きいインデックス(⍳1e5またはを必要とする⍳1e6)に対してタイムアウトしますが、十分な時間とメモリがあれば、関数は正しく終了します。


2

R152 137バイト

ジュゼッペのおかげで-12バイトdigEmAllのおかげで-3バイト

n=scan()
F=i=!1:2
`?`=sum
while(?n>i)if(n==?(i[s]=i[s<-sign((?!(r=?rev((T=T+1)%/%(e=10^(0:log10(T)))%%10)*e)%%1:r)-?!T%%1:T)]+1))F[s]=T
F

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

T現在試行されている整数です。最新の貧しい人々と豊かな人々がベクターに保存されますFます。

整数を逆にする最も簡単な方法は、モジュラー演算で10進数に変換し、10のべき乗で逆変換することでした。

説明(以前の類似バージョンの):

n=scan() # input
i=0*1:3  # number of poor, middle class, and rich numbers so far
S=sum
while(S(n>i)){ # continue as long as at least one of the classes has less than n numbers
  if((i[s]=i[
    s<-2+sign(S(!(           # s will be 1 for poor, 2 for middle class, 3 for rich
      r=S((T<-T+1)%/%10^(0:( # reverse integer T with modular arithmetic
        b=log10(T)%/%1       # b is number of digits
        ))%%10*10^(b:0)) 
      )%%1:r)-               # compute number of divisors of r
      S(!T%%1:T))            # computer number of divisors of T
    ]+1)<=n){                # check we haven't already found n of that class
    F[s]=T
  }
}
F[-2] # print nth poor and rich numbers

146バイト ; digEmAllの応答が何であるかわかりません
ジュゼッペ

@ジュゼッペありがとう!の使用が大好きですnchar
ロビンライダー

142バイト。以前はオペレーターの優先順位に問題がありましたが、困惑しました。
ジュゼッペ

2
よくやった!140逆戦略の変更
digEmAll

2
@digEmAll 138バイトに戻って行きますlog10
ジュゼッペ

1

JavaScript(Node.js)190 180バイト

として出力し[poor, rich]ます。

n=>{let p,r,f=h=i=0;while(f<n){k=d(i),e=d(+(i+"").split``.reverse().join``);if(k<e){p=i;f++}if(k>e&&h<n){r=i;h++}i++}return[p,r]}
d=n=>{c=0;for(j=1;j<=n;j++)if(n%j==0)c++;return c}

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

説明

d(n) 関数

このヘルパーは、数値が持つ要因の数を見つけます。

d=n=>{              // Equivalent to `function d(n) {
  c=0;              // Counter
  for(j=1;j<=n;j++) // Check every integer from 1 to n
    if(n%j==0)c++;  // If the number is a factor, add 1 to the counter
  return c
};

メイン機能

n=>{ 
  let p,r,f=h=i=0; // p -> the poor number, r -> the rich number, f -> the number of poor numbers found, h -> the number of rich numbers found, i -> the current number being checked
  while(f<n){ // While it's found less than n poor numbers (assumes that it will always find the rich number first)
    k=d(i),        // k -> number of factors of i
    e=d(+((i+"").split``.reverse().join``)); // e -> number of factors of reversed i
    if(k<e){p=i;f++}  // If it hasn't found enough poor numbers and i is poor, save it and count it
    if(k>e&&h<n){r=i;h++}  // If it hasn't found enough rich numbers and i is rich, save it and count it
    i++
  };
  return[p,r]
}

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.