バイナリプライムチャンク


19

シーケンスを検索しています

自然数を取る
1,2,3,4,5,6,7,8,9,10,11,12,13,14...

2底に変換
1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110...

上記の数字を連結します
110111001011101111000100110101011110011011110...

パーティションにこの番号をプライムチャンク
(桁の素数を含むチャンク)
素数が順序をascedingに取り込まれます2,3,5,7,11,13,17...

[11][011][10010][1110111][10001001101][0101111001101][1110...]

各チャンクの数字の合計を見つける

Primes 2 3 5 7 11 13 17
Chunks [11][011][10010][1110111][10001001101][0101111001101][1110...]
SumOfDigits 2 2 2 6 5 8

シーケンス

2、2、2、6、5、8、9、10、14、22、11、18、25、27、32、21、28、32、40、40、49、49、32、41、49、 53、63、55、63、70、87、73、51、63、71、78、78、90、107、86、96、108、115、128、138、92、83、95、102、110、 130、106、122、141、149、163、130、140、151、165、181、165、204、200、234、100、130、138、167、149、169、180、209、166、189、 194、222、205、234、260、216、206、217、241、240、267、289、242、274、308、286、329、338、155、189、225、197、240、272、217、 254、282、287、317、281、256、299、286、331、337、316、350、354、391、367、282、327、313、364、358、348、397、406、466 ...

チャレンジ

nth上記のシーケンスの項を 見つける

入力

整数 n>0

テストケース

1->2   
3->2    
6->8    
36->78 
60->165    
160->581     
260->1099    
350->1345

これはです。バイト単位の最短回答が勝ちです!


2
関連(最初の3つのステップは同じ)
ライコニ

4
これは、多くの課題が一緒にマッシュアップされたように感じるため、ダウン投票されました。
エソランジングフルーツ

回答:


14

、8バイト

Σ!CİpṁḋN

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

説明

Σ!CİpṁḋN
       N   Start with the infinite list of natural numbers.
     ṁḋ    Convert each to its binary representation and join them all together. (A)
   İp      Get the infinite list of primes. (B)
  C        Split (A) into chunks of lengths (B).
 !         Retrieve the nth chunk (where n is the input).
Σ          Sum the bits in this chunk.

6

ゼリー、12バイト

RÆNµSRBFṁRṪS

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

使い方

RÆNµSRBFṁRṪS  Main link. Argument: n

R             Range; yield [1, ..., n].
 ÆN           N-th prime; yield P := [p(1), ..., p(n)].
   µ          Begin a new, monadic chain with argument P.
    S         Take the sum of P, yielding s := p(1) + ... + p(n).
     R        Range; yield [1, ..., s].
      B       Binary; convert all integers from 1 to s to base 2.
       F      Flatten the resulting array.
         R    Range; yield [[1, ..., p(1)], ..., [1, ..., p(n)]].
        ṁ     Mold; reshape the result to the left like the result to the right.
          Ṫ   Tail; take the last chunk.
           S  Take the sum, counting the set digits.

5

05AB1E、12バイト

コード

数が多いとかなり遅くなります:

ÅpDOLbJs£`SO

05AB1Eエンコードを使用します。オンラインでお試しください!

説明

Åp              # Get a list of the first <input> primes
  DO            # Duplicate and sum the primes
    L           # Create the list [1, .., <sum>]
     bJ         # Convert to binary and join into a single string
       s£       # Get the slices [a[0:2], a[2:2+3], a[2+3:2+3+5], a[2+3+5:2+3+5+7], ...] 
                  corresponding to the list of primes
         `SO    # Get the last one and sum up it's digits



2

ゼリー、16バイト

RBFṁ
RÆNSÇṫÆNC$S

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

説明

RBFṁ  Helper link. Input: integer k
R     Range, [1, 2, ..., k]
 B    Convert each to a list of its binary digits
  F   Flatten
   ṁ  Shape it to length k

RÆNSÇṫÆNC$S  Main link. Input: integer n
R            Range, [1, 2, ..., n]
 ÆN          Get i'th prime for each
   S         Sum
    Ç        Call helper link
         $   Monadic chain
      ÆN       Get n'th prime
        C      Complement, 1 - n'th prime
     ṫ       Tail, take the last n'th prime digits
          S  Sum

2

R206 200バイト

function(n){a=p=j=y=2
for(i in 2:n-1){while(sum(y)<4*a){x=as.double(rev(intToBits(j)))
y=c(y,x[cumsum(x)>0])
j=j+1}
b=1:a
y=y[-b]
z=outer(k<-b+a,p,'%%')
p=c(a<-k[!apply(z<1,1,sum)][1],p)}
sum(y[1:a])}

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

アルゴリズムは、素数を循環するときにビットを繰り返し削除することで、スペースを「節約」しようとします。小数からビットへの変換はおそらく短くなると思いますが、他の選択肢を見つけることができませんでした。

Jonathan Frenchのおかげで6バイト節約されました。


1
Rは連鎖代入をサポートすると思います。p=j=2は、より2バイト短いですp=2;j=2
ジョナサンフレッチ

...おそらくのためa=pに行うことができ、さらに2バイトを節約します。
ジョナサンフレッチ

1
...そして-理由がわからない-で動作しy=1、置き換えy=2られ、200バイトになります
ジョナサンフレッチ

ありがとうございました。y = 2は、数字1のビットを置き換えます。n> 1の場合、最初の反復で除去され、n = 1の場合、forループが逆方向にループするため、n = 3の答えが得られます。まだ2です(運悪くありません)。
NofP

2

JavaScript(ES6)、144バイト

n=>eval("s=o=j=0;for(i=p=1;n;d>p&&(n--,s+=p))for(p++,d=2;p%d++;);while(b=Math.log2(++j)+1|0,i<=s)for(x=0;x++<b&i<=s;)o+=i++>s-p&&j<<x&1<<b?1:0")

非ゴルフ

n=>{
    s=o=j=0;
    for(i=p=1;n;d>p&&(n--,s+=p))
        for(p++,d=2;p%d++;);
    while(b=Math.log2(++j)+1|0,i<=s)
        for(x=0;x++<b&i<=s;)
            o+=i++>s-p&&j<<x&1<<b?1:0
    return o
}

テストケース



2

JavaScriptの(ES6)、138の 132 123バイト

N=>(n=k=1,g=s=>N?g((P=n=>n%--x?P(n):x<2)(x=++n)?s[n]?s.slice(--N&&n,n/!N):s+(n--,k++).toString(2):s):s.split`1`.length-1)``

テストケース

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

デモ

注意:ここには「安全な」テストケースのみが含まれています(Chrome、Firefox、およびEdgeでの動作が保証されています)。他のエンジンを渡すには、エンジンの呼び出しスタックサイズを増やす必要がある場合があります。

書式設定およびコメント化

N => (                            // given N = index of the expected term
  n = k = 1,                      // n = current prime, k = current natural number
  g = s =>                        // g = recursive function taking s = binary string
    N ?                           //   if we haven't reached the correct chunk yet:
      g(                          //     do a recursive call to g():
        (P = n =>                 //       P() returns: true for prime
          n % --x ? P(n) : x < 2) //                    false for composite
        (x = ++n) ?               //       increment n; if n is prime:
          s[n] ?                  //         if s is long enough:
            s.slice(--N && n,     //           either remove this chunk (if N > 0)
                    n / !N)       //           or truncate it to the correct size (if N = 0)
          :                       //         else:
            s + (n--, k++)        //           append the next natural number to s
                .toString(2)      //           in binary format
        :                         //       else:
          s                       //         just look for the next prime
      )                           //     end of recursive call
    :                             //   else:
      s.split`1`.length - 1       //     return the number of 1's in the last chunk
)``                               // initial call to g() with an empty string

1

Perl 6、67バイト

{(1..*).map(|*.base(2).comb).rotor(grep *.is-prime,2..*)[$_-1].sum}

試して

拡張:

{  # bare block lambda with implicit parameter 「$_」

  (

    1 .. *                # Range of all numbers starting with 1

  ).map(

    # WhateverCode lambda
    |                     # Slip each of these values into the outer list individually
      *                   # this is the parameter
      .base(2)            # convert base
      .comb               # split into digits


  ).rotor(                # split into chunks

    grep *.is-prime, 2..* # the sequence of prime numbers


  )[ $_ - 1]              # index into it using 1 based indexing

  .sum                    # find the sum
}

1

パイソン2143の 139 133バイト

@ErikTheOutgolferのおかげで-4バイト

s='1';i=x=1
exec"s=s[i:];i+=1\nwhile~-all(i%x for x in range(2,i)):i+=1\nexec's+=bin(x)[2:];x+=1;'*i;"*input()
print s[:i].count('1')

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


互換性のないテストハーネスを削除して-2バイト。いくつかのものを再配置することによる別の-2
エリックアウトゴルファー

@EriktheOutgolferどうもありがとう。私はまだ古いテストを元に戻すことができました。
ovs

1

J、48バイト

([:+/-@{:{.+/{.[:}:[:(#:@[,])/1+[:i.1++/)@:p:@i.

説明した

(                                                         )@:p:@i.  the first n primes, passed to...
       -@{: {.                    ...                               take "nth prime" elements from the tail of...
               +/                                                   sum the first n primes and...
                  {.                                                take that number of elements from...
                     [: }:                                          all but the last element of...   <----------------<
                                          1 + [: i. 1 + +/          sum first n primes, add 1 (so we have enough      |
                                                                    for case n=1) -- make that many natural numbers   |
                           [: (#:@[ , ])/                           reduce them by turning into lists of binary       |
                                                                    digits and catting, however the rightmost number  |
                                                                    won't get reduced, hence the need for ------------^
([: +/                                                              and sum those digits

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


キーを使用して30バイト(/.):_1({]+//.$$&;<@#:@#\)[:#~p:@i.
マイル

とても賢い。ありがとうマイル。
ジョナ

0

JavaScript 1+ + substr、135バイト

for(n=prompt(s=P=0),i=n*n*n*8;--i;)s=i.toString(2)+s;for(p=1;n;e=j?s:--n?P+=p:s.substr(P,p))for(j=p++;p%j--;);eval([].join.call(e,'+'))

「4」とはどういう意味ですか?バージョンがわかりませんか?あなたが体で意味することを拡張することは、この投稿をより良くするのに役立ちます。
FryAmTheEggman

JS5が来なかったときに実行されることは知っていますが、正確にはいつ
–l4m2
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.