以前の合成番号


16

シーケンス定義

a(n)次のように正の整数のシーケンスを作成します。

  1. a(0) = 4
  2. a(n)最初の項以外の各項は、次を満たす最小の数値です。a
    a(n)は合成数、
    b)a(n) > a(n-1)および
    c)a(n) + a(k) + 1はそれぞれの合成数です0 <= k < n

だから私たちはで始まりa(0) = 4ます。次のエントリはでa(1)なければなりません9。それらは合成できない57、合成できないため、合成できない6か合成でない8ため、合成できないか、合成できないから6+4+1=11です8+4+1=13。最後に、、9+4+1=14これは複合a(1) = 9です。

次のエントリはであるa(2)必要があります。これは、両方のコンポジット10よりも大きい最小の番号だからです。910+9+1=2010+4+1=15

次のエントリについては、それらは複合ではないため両方1113も出ています。複合ではない12ため12+4+1=17です。複合ではない14ため14+4+1=19です。したがって、15のでシーケンスの次の用語である15複合材であり15+4+1=2015+9+1=25及び15+10+1=26従って、全て各複合ありますa(3) = 15

このシーケンスの最初の30の用語は次のとおりです。

4, 9, 10, 15, 16, 22, 28, 34, 35, 39, 40, 46, 52, 58, 64, 70, 75, 76, 82, 88, 94, 100, 106, 112, 118, 119, 124, 125, 130, 136

これはOEIS A133764です。

チャレンジ

入力整数を指定するとnnこのシーケンスのth番目の項を出力します。

ルール

  • 0ベースまたは1ベースのインデックス作成を選択できます。提出する際にどちらを明記してください。
  • 入力と出力は、言語のネイティブ整数型に適合すると仮定できます。
  • 入力と出力は、任意の便利な方法で指定できます。
  • 完全なプログラムまたは機能のいずれかが受け入れられます。関数の場合、出力する代わりに出力を返すことができます。
  • 標準的な抜け穴は禁止されています。
  • これはので、通常のゴルフルールがすべて適用され、最短のコード(バイト単位)が勝ちます。

3
タイトル:以前はコンポジットと呼ばれていた番号。
魔法のタコ

@MagicOctopusUrnこれがアートや音楽と関係があるのなら、それでいいと思います。しかし、私は現在持っているタイトルに固執します。
AdmBorkBork

もっと冗談だった;)。
魔法のタコ

回答:


5

、11バイト

!üȯṗ→+fotpN

1インデックス付き。 オンラインでお試しください!

説明

!üȯṗ→+fotpN  Implicit input, a number n.
          N  The list of positive integers [1,2,3,4,..
      f      Keep those
         p   whose list of prime factors
       ot    has a nonempty tail: [4,6,8,9,10,12,..
 ü           De-duplicate wrt this equality predicate:
     +       sum
    →        plus 1
  ȯṗ         is a prime number.
             Result is [4,9,10,15,16,..
!            Get n'th element.

2

Perl 6、70バイト

{(4,->+_{first {none($^a X+0,|(_ X+1)).is-prime},_.tail^..*}...*)[$_]}

0インデックスで試してください

拡張:

{  # bare block lambda with implicit parameter $_

  (  # generate the sequence

    4, # seed the sequence

    -> +_ { # pointy block that has a slurpy list parameter _ (all previous values)

      first

      {  # bare block with placeholder parameter $a

        none(                 # none junction
            $^a               # placeholder parameter for this inner block
          X+                
            0,                # make sure $a isn't prime
            |( _ X+ 1 )       # check all a(k)+1
        ).is-prime            # make sure none are prime
      },

      _.tail ^.. *            # start looking after the previous value
    }

    ...                       # keep generating values until

    *                         # never stop

  )[$_]                       # index into the sequence
}


2

JavaScript(ES6)、83バイト

1インデックス付き

f=(n,a=[-1,p=4])=>a[n]||f(n,a.some(x=>(P=n=>n%--x?P(n):x<2)(x-=~p),p++)?a:[...a,p])

デモ

コメント済み

ヘルパー関数P()nが素数の場合はtrue、そうでない場合はfalseを返します

P = n => n % --x ? P(n) : x < 2

NB:x = nで呼び出す必要があります。

メイン関数f()

f = (               // given:
  n,                //   n = target index
  a = [-1, p = 4]   //   a = computed sequence with an extra -1 at the beginning
) =>                //   p = last appended value
  a[n] ||           // if a[n] exists, stop recursion and return it
  f(                // otherwise, do a recursive call to f() with:
    n,              //   n unchanged
    a.some(x =>     //   for each value x in a[]:
      P(x -= ~p),   //     rule c: check whether x + p + 1 is prime
                    //     rule a: because a[0] = -1, this will first compute P(p)
      p++           //     rule b: increment p before the some() loop starts
    ) ?             //   end of some(); if truthy:
      a             //     p is invalid: use a[] unchanged
    :               //   else:
      [...a, p]     //     p is valid: append it to a[]
  )                 // end of recursive call



0

Javaの8、186の 173バイト

n->{int a[]=new int[n+1],r=a[n]=4;a:for(;n>0;)if(c(++r)<2){for(int x:a)if(x>0&c(r-~x)>1)continue a;a[--n]=r;}return r;}int c(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}

0インデックス。
残念ながら、プライムチェック(またはこの場合はアンチプライム/コンポジットチェック)は、Javaではそれほど安くはありません。

説明:

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

n->{                     // Method with integer as both parameter and return-type
  int a[]=new int[n+1],  //  Integer-array of size `n+1`
      r=a[n]=4;          //  Start the result and last item at 4
  a:for(;n>0;)           //  Loop as long as `n` is larger than 0
    if(c(++r)<2){        //   Raise `r` by 1, and if it's a composite:
      for(int x:a)       //    Inner loop over the array
        if(x>0           //     If the item in the array is filled in (non-zero),
           &c(r-~x)>1)   //     and if `r+x+1` is a prime (not a composite number):
          continue a;}   //      Continue the outer loop
      a[--n]=r;}         //    Decrease `n` by 1, and put `r` in the array
  return r;}             //  Return the result

// Separated method to check if a given number is a composite number
// (It's a composite number if 0 or 1 is returned, otherwise it's a prime.)
int c(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}

0

Ruby + -rprime85 75バイト

->n{*a=x=4
n.times{x+=1;!x.prime?&&a.none?{|k|(x+k+1).prime?}?a<<x:redo}
x}

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

0から始まるn番目の要素を返すラムダ。

-10バイト:... と条件チェーンのredo代わりにandと三項演算子を使用しますloopbreak

ゴルフをしていない:

->n{
  *a=x=4                         # x is the most recent value: 4
                                 # a is the list of values so far: [4]
  n.times{                       # Repeat n times:
    x += 1                       # Increment x
    !x.prime? &&                 # If x is composite, and
      a.none?{|k|(x+k+1).prime?} #   for all k, a(n)+x+1 is composite,
      ? a<<x                     # Add x to a
      : redo                     # Else, restart the block (go to x+=1)
  }
  x                              # Return the most recent value
}


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