ダブルプライムワード

Sep 09 2020

長さの単語/文字列を考えてみましょう\$n\$、文字AZ、azのみを含みます。単語/文字列は、nが素数であり、文字の合計sも素数であり、アルファベットの数値位置(a=1, B=2, c=3など)を使用する場合に限り、二重素数の単語です。

aまたはの間に数値の違いがないため、入力は大文字または小文字のアルファベット文字の任意の組み合わせにすることができますA。

出力は、言語に関連する適切な論理形式です。つまり、TrueまたはFalse、TまたはF、1または0などです。出力を表示する形式を指定することは非常に重要ですが、必須ではありません。(出力にn、sを含める必要はありませんが、デモンストレーションと例として以下に含めます)

勝者条件は、文字列が二重素数であるかどうかを検出できるバイト単位の最短コードであり、nとsの両方の条件が素数になるように適合します。(n、sの4つの可能な状況すべてからのケースを含めました。)

例

Input -> Output (n, s)

Prime -> True (5, 61)
han -> True (3, 23)
ASK -> True (3, 31)
pOpCoRn -> True (7, 97)
DiningTable -> True (11, 97)
METER -> True (5, 61)

Hello -> False (5, 52)
SMILE -> False (5, 58)
frown -> False (5, 76)

HelpMe -> False (6, 59)
John -> False (4, 47)
TwEnTy -> False (6, 107)

HelloWorld -> False (10, 124)
Donald -> False (6, 50)
telePHONES -> False (10, 119)

A -> False (1, 1) 
C -> False (1, 3) {1 is not prime}
d -> False (1, 4)

回答

5 cairdcoinheringaahing Sep 09 2020 at 05:03

ゼリー、12バイト

ŒuO_64µL,SẒP

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

使い方

ŒuO_64µL,SẒP - Main link, takes string s as argument e.g. s = "Prime"
Œu           - Convert to upper case                          "PRIME"
  O          - Convert to ordinals                            [80, 82, 73, 77, 69]
   _64       - Subtract 65 (call this L)                      [16, 18, 9, 13, 5]
      µ      - Start a new link with L as the left argument
       L     - Take the length                                5
         S   - Take the sum                                   61
        ,    - Pair the two values                            [5, 61]
          Ẓ  - Take primality of each                         [1, 1]
           P - Take product                                   1
5 RobinRyder Sep 09 2020 at 20:34

R、6871バイト

Dominic vanEssenが指摘したバグを修正するための+3バイト

`?`=sum;s=?b<-utf8ToInt(scan(,""))%%32;l=?b^0;l-1&5>?c(!s%%1:s,!l%%1:l)

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

大文字と小文字の両方を整数1 ... 26に変換するにsum(!x%%1:x)は、32を法とするASCIIコードポイントを使用できることに注意してください。これは、の約数の数を数えるゴルフの方法です。これは、素数のx場合xは2に等しくなります。

ゴルフなし:

`?` = sum                       # shorthand for sum
b = utf8ToInt(scan(, "")) %% 32 # take input and convert to ASCII, then take mod 32
s = sum(b)
l = sum(b^0)                    # l = length(b)
5 > sum(c(!s%%1:s,!l%%1:l))    # sum the number of divisors of s and l, and check whether you get <5.
       & l!=1                   # and that l is not 1
5 Razetime Sep 09 2020 at 14:37

ルビー、27の59バイト

->a{[a.size,a.upcase.bytes.map{|i|i-64}.sum].all? &:prime?}

DrQuariusのおかげで、ソリューションを修正した後、+ 33バイト。

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

4 Abigail Sep 09 2020 at 06:48

perl -Mfeature = say -MList :: Util = sum -pl、95バイト

s/[^a-z]//gi;$m=sum map-64+ord,split//,uc;$_=(1 x y===c)!~/^(11+)\1+$|^1$/&&(1x$m)!~/^(11+)\1$/

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

それはどのように機能しますか?

s/[^a-z]//gi;   # Clean the input, remove anything which isn't an ASCII letter.

                          uc;     # Upper case the string
                  split//,        # Split it into individual characters
          -64+ord                 # Calculate its value: 
                                  #           subtract 64 from its ASCII value
       map                        # Do this for each character, return a list
$m=sum                            # Sum the values, and store it in $m

     y===c                        # Returns the length of the input string
(1 x y===c)                       # Length of the input string in unary

/^(11+)\1+$|^1$/                  # Match a string consisting of a composite
                                  # number of 1's, or a single 1
!~                                # Negates the match, so
(1 x y===c)1~/^(11+)\1+$|^1$/     # this is true of the input string (after
                                  # cleaning) has prime length

(1x$m)!~/^(11+)\1+$/              # Similar for the sum of the values --
                                  # note that the value is at least 2, so
                                  # no check for 1.

これを組み合わせると、プログラムは条件に一致する行に1を出力し、一致しない行に空の行を出力します。

4 KevinCruijssen Sep 09 2020 at 13:41

05AB1E、10バイト

gAIlk>O‚pP

文字のリストとして入力します。

オンラインそれを試してみたり、すべてのテストケースを確認してください。

説明:

g           # Get the length of the (implicit) input-list
 A          # Push the lowercase alphabet
  I         # Push the input-list of characters
   l        # Convert the input to lowercase
    k       # Get the (0-based) index of each character in the alphabet-string
     >      # Increase each by 1 to make them 1-based indices
      O     # Take the sum of that
       ‚    # Pair the length together with this sum
        p   # Check for both whether they're a prime (1 if it's a prime; 0 if not)
         P  # And check if both are truthy by taking the product of the pair
            # (after which the result is output implicitly)
4 DominicvanEssen Sep 10 2020 at 04:51

R、70バイト

function(s,S=sum,t=S(utf8ToInt(s)%%32))S(!nchar(s)%%1:t)^S(!t%%1:t)==4

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

私はこれを撃つ前にロビンライダーの答えを覗き見しないように強制しました、そして(満足のいくように)私たちはいくつかのかなり異なるゴルフトリックを使用したことがわかりました。

tすべての文字インデックスの合計です。これは、以上であることが確実ですnchar(s)(文字列sが「A」または「a」の場合にのみ等しくなります)。したがって1:t、モジュロの代わりにモジュロを使用して文字列の長さの素数性をテストでき1:nchar(s)、を格納するために変数宣言で文字を無駄にする必要はありませんnchar(s)。

両方の素数性テストsum(!t%%1:t)でsum(!nchar(s)%%1:t)あり、文字の合計インデックスと文字列の長さの両方が素数である場合は2に等しくなければなりません。
両方が2であるかどうかを確認できますが、これには==22回(プラス1&または同等)が必要であり、無駄に思えます。合計が4であることを確認しても大丈夫ですか?心配する必要があるエッジケースは、一方が1に等しく、もう一方が3に等しい場合です。これは、文字列 "D"(length = 1およびcharacter-index = 4、除数1、2、および4)で発生します。だからそれは大丈夫ではありません。それらを掛けることはできますか?また、いいえ、1と4は再び4を与えるので(文字列「F」について考えてください)。
ただし、文字列の長さは文字インデックスの合計以下でなければならないことがわかっているので、べき乗を使用できます。4を取得する唯一の方法は4 ^ 1または2 ^ 2であり、文字列の長さが4の場合、文字の合計インデックスを1にすることはできません。2^ 2が唯一の可能性です。

したがって、二重素数性の最終的な組み合わせチェックはsum(!nchar(s)%%1:t)^sum(!t%%1:t)==4、個別にテストする場合と比較して3文字節約できます。

4 Shaggy Sep 11 2020 at 23:47

ロックスター、327の 321 319バイト

素数をテストするための組み込みはありません!
ケース変換なし!
キャラクターのコードポイントを取得する方法はありません!

なぜ私は自分自身にこれらのことをするのですか?!いまいましいものを機能させるためだけに長い間費やしたので、それが最適なゴルフにはほど遠いと確信していますが、今のところはうまくいくでしょう。

F takes N
let D be N
let P be N aint 1
while P and D-2
let D be-1
let M be N/D
turn up M
let P be N/D aint M

return P

G takes I
Y's0
N's27
while N
cast N+I into C
if C is S at X
return N

let N be-1

return G taking 64

listen to S
X's0
T's0
while S at X
let T be+G taking 96
let X be+1

say F taking T and F taking X

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

3 Neil Sep 09 2020 at 05:21

Retina 0.8.2、77バイト

\W|\d|_

$
¶$`
\G.
1
T`L`l
[t-z]
55$&
[j-z]
55$&
T`_l`ddd
.
$*
A`^(..+)\1+$

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

\W|\d|_

文字以外のものはすべて削除してください。

$
¶$`

文字を複製します。

\G.
1

最初の行の文字を1sに置き換えて、長さを単項にします。

T`L`l

残りの文字を小文字に変換します。

[t-z]
55$&
[j-z]
55$&
T`_l`ddd

それらを合計して数値位置になる数字に変換します。

.
$*

数字を単項に変換し、合計を取ります。

A`^(..+)\1+$

複合値を削除します。

両方の値がまだ存在することを確認してください。

3 Noodle9 Sep 09 2020 at 07:15

Pythonの3、86の 78 87バイト

ovsのおかげで8バイト節約できました!!! ロビンライダーが
親切に指摘したバグを修正するために9バイトを追加しました。

lambda s:~-len(s)*all(n%i for n in(len(s),sum(ord(c)&31for c in s))for i in range(2,n))

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

真または偽の値を返します。

3 xash Sep 10 2020 at 05:18

Brachylog、11バイト

ḷạ-₉₆ᵐ+ṗ&lṗ

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

使い方

ḷạ-₉₆ᵐ+ṗ&lṗ (is the implicit input)
ḷ           to lowercase
 ạ          to list of char codes
  -₉₆ᵐ      minus 96 (so 'a' -> 1)
      +     summed
       ṗ    prime?
        &l  and is the input's length
          ṗ prime?
3 J42161217 Sep 09 2020 at 04:59

Wolfram言語(Mathematica)、34バイト

PrimeQ@*Tr/@(LetterNumber@#&&1^#)&

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

- @ attから22バイト

2 Shaggy Sep 09 2020 at 06:37
2 Jonah Sep 10 2020 at 11:24

J、27の 22 18バイト

1*/@p:#,1#.32|3&u:

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

-xashのおかげで5バイト

-Dominic vanEssenのおかげで4バイト

  • 32|3&u: 最初にASCII番号に変換し、32で変更して、各文字をインデックスに変換します。
  • 1#. 和。
  • #, リストの長さを付加します。
  • 1...p: これらの2つの数字はそれぞれ素数ですか?
  • */@ それらを一緒に乗算します-それらはすべて素数ですか?
2 tom Sep 10 2020 at 01:53

C- 119108 99 98バイト(gcc)

@ceilingcatは別のバイトを保存しました!

b,t,e;p(c){for(;--e&&c%e;);c=e==1;}a(char*a){t=0;for(e=b=strlen(a);b;)t+=a[--b]%32;t=p(e)*p(e=t);}

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

以前

20バイトを節約してくれた@DominicvanEssenと@ceilingcatに感謝します!-特にn = 1(非プライム)のエラーを修正するためのドミニクに

b,t,e;p(c){for(b=c;--b&&c%b;);c=b==1;}a(char*a){t=0;for(e=b=strlen(a);b;)t+=a[--b]%32;t=p(e)*p(t);}

119バイト未満の最初の試行

a(char*a){int t=0,d=strlen(a),e=d;while(d)t+=a[--d]%32;return p(e)*p(t);}
p(int c){int b=c;while(--b&&c%b);return b<2;}

実際while(c%--b)、2番目のルーチンで使用することで3バイトを節約できますが、これはp(1)の場合(例: 'a')では失敗します。または他の単一文字。

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

2 user Sep 09 2020 at 09:00

スカラ座、75の 74 69バイト

| =>p(|size)&p(|map(_&95-64)sum)
def p(n:Int)=(2 to n/2)forall(n%_>0)

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

1 GalenIvanov Sep 09 2020 at 14:08

因数分解、78バイト

: d ( s -- ? ) dup [ length ] dip >lower [ 96 - ] map sum [ prime? ] bi@ and ;

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

1 Lyxal Sep 09 2020 at 06:18

05AB1E、11バイト

uÇ64-Op¹gp&

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

入力制限がないためにバイトが削除されました

1 Arnauld Sep 09 2020 at 05:55

JavaScript(Node.js)、88バイト

0または1を返します。

s=>(g=k=>n%--k?g(k):k==1)(Buffer(s).map(c=>x+=n<(n+=c>64&(c&=31)<27&&c),x=n=0)|n)&g(n=x)

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

コメント

ヘルパー機能

g = k =>                   // g is a helper function testing if n is prime
  n % --k ?                //   decrement k; if it does not divide n:
    g(k)                   //     do recursive calls until it does
  :                        //   else:
    k == 1                 //     test whether k = 1

主な機能

s =>                       // s = input string
  g(                       // test if the 'sum of the letters' is prime
    Buffer(s).map(c =>     //   for each ASCII code c in s:
      x +=                 //     increment x if ...
        n < (              //       ... n is less than ...
          n +=             //         ... the new value of n:
            c > 64 &       //           if c is greater than 64
            (c &= 31) < 27 //           and c mod 32 is less than 27:
            && c           //             add c mod 32 to n
        ),                 //
      x = n = 0            //     start with x = n = 0
    ) | n                  //   end of map(); yield n
  )                        // end of the first call to g
  & g(n = x)               // 2nd call to g with the 'length' x
1 Xcali Sep 11 2020 at 10:36

Perl 5、52 -plバイト

@Abigailの回答からのプライム識別正規表現を使用します

$_.=$".1x s/./1x(31&ord$&)/ge;$_=!/\b((11+)\2+|1)\b/

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

1 DrQuarius Sep 13 2020 at 12:39

Ruby、50 5550バイト

->s{[s.size,s.upcase.sum-64*s.size].all? &:prime?}

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

配列が真実であると見なすことができるかどうかの誤解による+5バイト。

-「&:prime?」を配置するという素晴らしいトリックを使用して、Razetimeのおかげで5バイト 「.all?」の前に「.map(&:prime?)」を実行する代わりに、最後に。

Razetimeのソリューションは実際にはアルファベットのインデックスを合計せず、単にASCIIの序数を合計したため、個別に投稿されました。ダブルプライムワード「DiningTable」と「METER」では失敗します。

1 LegionMammal978 Oct 28 2020 at 01:05

ハスク、12バイト

&ṗL¹ṗṁȯ-64ca

オンラインでお試しください!単語がダブルプライムワードの場合は真の数値を出力し、それ以外の場合は0を出力します。