Double Prime Words

Sep 09 2020

พิจารณาคำ / สตริงที่มีความยาว\$n\$รวมเฉพาะตัวอักษร AZ, az คำ / สตริงเป็นคำที่มีไพรม์สองเท่าถ้า n เป็นไพรม์และผลรวมของตัวอักษร s จะเป็นไพรม์ด้วยโดยใช้ตำแหน่งตัวเลขในตัวอักษร ( a=1, B=2, c=3ฯลฯ )

การป้อนข้อมูลสามารถรวมกันของตัวอักษรบนหรือกรณีที่ต่ำกว่าใด ๆ ตามที่ไม่มีความแตกต่างระหว่างตัวเลขหรือaA

ผลลัพธ์คือรูปแบบตรรกะที่เหมาะสมที่เกี่ยวข้องกับภาษาของคุณ เช่น True หรือ False, T หรือ F, 1 หรือ 0 เป็นต้นการระบุรูปแบบที่ผลลัพธ์ของคุณจะปรากฏเป็นที่นิยมอย่างมาก แต่ไม่จำเป็นต้องใช้ (ผลลัพธ์ไม่จำเป็นต้องรวม n, s แต่ฉันรวมไว้ด้านล่างเพื่อสาธิตและตัวอย่าง)

เงื่อนไขการชนะคือรหัสที่สั้นที่สุดในหน่วยไบต์ที่สามารถตรวจจับได้ว่าสตริงเป็นไพรม์คู่หรือไม่โดยปรับเงื่อนไขทั้งสองให้ n และ s เป็นไพรม์ (ตอนนี้ฉันได้รวมเคสจากทั้ง 4 สถานการณ์ที่เป็นไปได้ของ n, s)

ตัวอย่าง

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 , 68 71 ไบต์

+3 ไบต์เพื่อแก้ไขข้อผิดพลาดที่ Dominic van Essen ชี้ให้เห็น

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

ลองออนไลน์!

สังเกตว่าในการแปลงตัวอักษรตัวพิมพ์ใหญ่และตัวพิมพ์เล็กเป็นจำนวนเต็ม 1 ... 26 เราสามารถใช้ ASCII codepoint modulo 32 sum(!x%%1:x)ได้เป็นวิธีที่ดีในการนับจำนวนตัวหารxซึ่งจะเท่ากับ 2 iff xเป็นจำนวนเฉพาะ

ไม่พอใจ:

`?` = 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?}

+33 ไบต์หลังจากแก้ไขโซลูชันด้วย DrQuarius

ลองออนไลน์! หรือตรวจสอบกรณีทดสอบทั้งหมด

4 Abigail Sep 09 2020 at 06:48

perl -Mfeature = พูด -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ในการทดสอบสำหรับ primality ของความยาวสตริงแทนโมดูโลและไม่มีตัวละครจำเป็นต้องเสียในการประกาศตัวแปรในการจัดเก็บ1:nchar(s)nchar(s)

การทดสอบเบื้องต้นทั้งสองแบบsum(!t%%1:t)และsum(!nchar(s)%%1:t)ต้องเท่ากับ 2 หากทั้งผลรวมของตัวอักษรดัชนีและความยาวสตริงเป็นไพรม์
เราสามารถตรวจสอบได้ว่าทั้งคู่เป็น 2 หรือไม่ แต่ต้องใช้==2สองครั้ง (บวก a &หรือเทียบเท่า) ซึ่งดูเหมือนสิ้นเปลือง ตรวจสอบได้หรือไม่ว่าผลรวมเป็น 4 edge-case ที่เราต้องกังวลคือถ้าหนึ่งในนั้นเท่ากับ 1 และอีก 3: สิ่งนี้เกิดขึ้นกับสตริง "D" (length = 1 และ character-index = 4 ที่มีตัวหาร 1,2 และ 4) มันไม่โอเค เราสามารถคูณมันได้หรือไม่? ไม่เช่นกันเพราะ 1 และ 4 จะให้ 4 อีกครั้ง (ลองนึกถึงสตริง "F")
แต่ - เนื่องจากเรารู้ว่าความยาวสตริงต้องน้อยกว่าหรือเท่ากับผลรวมของอักขระดัชนีเราจึงสามารถใช้เลขชี้กำลัง: วิธีเดียวที่จะได้ 4 คือ 4 ^ 1 หรือ 2 ^ 2 และตั้งแต่ ผลรวมของอักขระดัชนีต้องไม่เป็น 1 ถ้าความยาวสตริงคือ 4, 2 ^ 2 เป็นไปได้เพียงอย่างเดียว

ดังนั้นขั้นสุดท้ายการตรวจสอบแบบรวมสำหรับ double-primality คือsum(!nchar(s)%%1:t)^sum(!t%%1:t)==4การประหยัดอักขระ 3 ตัวเมื่อเทียบกับการทดสอบแยกกัน

4 Shaggy Sep 11 2020 at 23:47

Rockstar , 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 ไบต์

บันทึก 8 ไบต์ขอบคุณOVs !!!
เพิ่ม 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^#)&

ลองออนไลน์!

-22 ไบต์จาก@att

2 Shaggy Sep 09 2020 at 06:37

Japt , 16 ไบต์

Êj ©Uu ¬mc xaI j

ลองมัน

2 Jonah Sep 10 2020 at 11:24

J , 27 22 18 ไบต์

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

ลองออนไลน์!

-5 ไบต์ขอบคุณ xash

-4 ไบต์ขอบคุณ Dominic van Essen

  • 32|3&u: เปลี่ยนตัวอักษรแต่ละตัวให้เป็นดัชนีโดยการแปลงเป็นหมายเลข ascii ก่อนโดยการปรับค่าด้วย 32
  • 1#. ผลรวม
  • #, นำหน้าความยาวรายการ
  • 1...p: แต่ละจำนวนสองจำนวนนั้นเป็นจำนวนเฉพาะหรือไม่
  • */@ คูณเข้าด้วยกัน - พวกเขาทั้งหมดเป็นไพรม์หรือไม่?
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);}

ลองออนไลน์

ก่อนหน้านี้

ขอบคุณมากสำหรับ @DominicvanEssen และ @ceilingcat สำหรับการประหยัด 20 ไบต์! - และโดยเฉพาะอย่างยิ่งสำหรับ Dominic สำหรับการแก้ไขข้อผิดพลาดใน 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;}

ในความเป็นจริงสามารถบันทึก 3 ไบต์โดยใช้while(c%--b)ในรูทีนที่สอง แต่จะล้มเหลวในกรณีของ p (1) เช่น 'a' หรืออักขระเดี่ยวอื่น ๆ

ลองออนไลน์

2 user Sep 09 2020 at 09:00

Scala , 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 -pl , 52 ไบต์

ใช้นิพจน์ทั่วไป regex จากคำตอบของ @ Abigail

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

ลองออนไลน์!

1 DrQuarius Sep 13 2020 at 12:39

ทับทิม , 50 55 50 ไบต์

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

ลองออนไลน์!

+5 ไบต์เนื่องจากความเข้าใจผิดว่าอาร์เรย์ถือได้ว่าเป็นจริงหรือไม่

-5 ไบต์ขอบคุณ Razetime โดยใช้เคล็ดลับที่ดีในการใส่ "&: prime?" ในตอนท้ายแทนที่จะทำ ".map (&: prime?)" ก่อนหน้า ".all?"

โพสต์แยกกันเนื่องจากโซลูชันของ Razetime ไม่ได้รวมดัชนีตัวอักษร แต่เป็นเพียงลำดับ ascii มันล้มเหลวสำหรับคำสำคัญคู่ "โต๊ะรับประทานอาหาร" และ "วัด"

1 LegionMammal978 Oct 28 2020 at 01:05

แกลบ 12 ไบต์

&ṗL¹ṗṁȯ-64ca

ลองออนไลน์! แสดงจำนวนจริงถ้าคำนั้นเป็นคำเฉพาะคู่และเป็น 0