ค้นหาหมายเลข Rocco


12

ฉันถูกถามคำถามนี้ในการสัมภาษณ์ แต่ฉันไม่สามารถหาคำตอบได้ ฉันไม่รู้ว่าคำถามนั้นถูกหรือไม่ ฉันพยายามมาก แต่ก็ไม่สามารถหาทางออกได้ พูดอย่างสุจริตไม่มีอะไรมาอยู่ในใจของฉัน

หมายเลข Rocco

จำนวนเต็มบวกคือจำนวน Rocco หากสามารถแสดงเป็นหรือโดยที่คือจำนวนเฉพาะnn=p(p+14)n=p(p14)p

10 หมายเลขแรกของ Rocco คือ:

32,51,95,147,207,275,351,435,527,627

งาน

รหัสของคุณต้องยอมรับจำนวนเต็มบวกเป็นอินพุตและพิจารณาว่าเป็นหมายเลข Rocco หรือไม่

คะแนนบราวนี่

  • เขียนฟังก์ชั่นที่คำนวณและพิมพ์จำนวน Rocco น้อยกว่าหรือเท่ากับ 1 ล้าน
  • เขียนฟังก์ชั่นที่คำนวณและพิมพ์จำนวน Rocco จากคำถามโบนัส (ด้านบนหนึ่งคำถาม) ที่เป็นค่าเฉพาะ

5
สวัสดีและยินดีต้อนรับสู่ PPCG เราจัดการกับความท้าทาย (รูปลักษณ์ของคุณน่าสนใจจริงๆ) ที่มีเกณฑ์การให้คะแนนและเกณฑ์การชนะ ลองแก้ไขโพสต์ของคุณเพื่อรวมไว้ ฉันแนะนำcode-golfเป็นเป้าหมายเนื่องจากเป็นวิธีที่ง่ายที่สุดในการทำให้ถูกต้อง นอกจากนี้คุณต้องการหลีกเลี่ยงโบนัสเหล่านั้น เพียงแค่มุ่งเน้นไปที่งานที่ชัดเจนอย่างใดอย่างหนึ่ง
อดัม

3
เอาต์พุตจะเป็นจำนวนเต็ม : คุณไม่ได้หมายถึงบูลีนว่าอินพุตเป็นหมายเลข Rocco หรือไม่?
อดัม

5
โบนัสที่ print 02: หมายเลข Rocco ทั้งหมดประกอบ(n*..)กันดังนั้นจึงไม่มีช่วงเวลาใด ๆ
TFeld

4
"คะแนนโบนัส" สามารถเป็นค่าฮาร์ดโค้ดได้และไม่เป็นประโยชน์ต่อความท้าทายเลย ฉันแนะนำให้ลบทิ้ง
Erik the Outgolfer

5
ฉันแก้ไขคำถามและแท็กแล้ว อย่าลังเลที่จะย้อนกลับหรือแก้ไขเพิ่มเติมหากคุณไม่เห็นด้วย ดังที่ @EriktheOutgolfer กล่าวว่าฉันคิดว่าควรลบโบนัส
Arnauld

คำตอบ:


10

05AB1E , 8 ไบต์

ส่งคืน1ถ้าnเป็นหมายเลข Rocco หรือ0อย่างอื่น

fDŠ/α14å

ลองออนไลน์!

อย่างไร?

ด้วยจำนวนเต็มบวกnเราจะทดสอบว่ามีตัวประกอบสำคัญpของnเช่นนั้นหรือไม่:

|pnp|=14

แสดงความคิดเห็น

fDŠ/α14å  # expects a positive integer n as input       e.g. 2655
f         # push the list of unique prime factors of n  -->  2655, [ 3, 5, 59 ]
 D        # duplicate it                                -->  2655, [ 3, 5, 59 ], [ 3, 5, 59 ]
  Š       # moves the input n between the two lists     -->  [ 3, 5, 59 ], 2655, [ 3, 5, 59 ]
   /      # divide n by each prime factor               -->  [ 3, 5, 59 ], [ 885, 531, 45 ]
    α     # compute the absolute differences
          # between both remaining lists                -->  [ 882, 526, 14 ]
     14å  # does 14 appear in there?                    -->  1

11

JavaScript (ES7), 55 ไบต์

n=>(g=k=>k>0&&n%--k?g(k):k==1)(n=(49+n)**.5-7)|g(n+=14)

ลองออนไลน์!

อย่างไร?

ป.ร. ให้ไว้เป็นจำนวนเต็มบวกnเรากำลังมองหานายกxเช่นว่าx(x+14)=nหรือx(x14)=n n

ดังนั้นสมการกำลังสองต่อไปนี้:

(1)x2+14xn=0
(2)x214xn=0

ค่าบวกของ(1)คือ:

x0=49+n7

และรากที่เป็นบวกของ(2)คือ:

x1=49+n+7

ดังนั้นปัญหาจึงเทียบเท่ากับการทดสอบว่าx0หรือx1นั้นดีหรือไม่

ในการทำเช่นนั้นเราใช้ฟังก์ชั่นการทดสอบแบบเรียกซ้ำแบบคลาสสิกพร้อมการทดสอบเพิ่มเติมเพื่อให้แน่ใจว่าจะไม่วนซ้ำตลอดไปหากได้รับจำนวนอตรรกยะเป็นอินพุต

g = k =>    // k = explicit input; this is the divisor
            // we assume that the implicit input n is equal to k on the initial call
  k > 0 &&  // abort if k is negative, which may happen if n is irrational
  n % --k ? // decrement k; if k is not a divisor of n:
    g(k)    //   do a recursive call
  :         // else:
    k == 1  //   returns true if k is equal to 1 (n is prime)
            //   or false otherwise (n is either irrational or a composite integer)

ฟังก์ชั่นเสื้อคลุมหลัก:

n => g(n = (49 + n) ** .5 - 7) | g(n += 14)


6

Regex (ECMAScript), 64 62 ไบต์

aa+14n=a(a+14)aa+14

การใช้งานนี้แตกต่างจากในเวลาสั้น ๆ ขั้นตอนวิธีการคูณไว้ในวรรคของฉันตัวเลขมากมาย regex โพสต์ นี้เป็นสปอยเลอร์ ดังนั้นไม่ได้อ่านเพิ่มเติมใด ๆ ถ้าคุณไม่ต้องการบางขั้นสูงมายากล regex เอกมากมายสำหรับคุณ หากคุณต้องการที่จะหาวิธีที่จะใช้เวทมนตร์นี้ด้วยตัวเองฉันขอแนะนำให้เริ่มต้นด้วยการแก้ปัญหาบางอย่างในรายการปัญหาแนะนำที่ติดแท็กสปอยเลอร์อย่างต่อเนื่องในโพสต์ก่อนหน้านี้และพยายามหาข้อมูลเชิงลึกทางคณิตศาสตร์อิสระ

ขั้นตอนวิธีการคูณจะดำเนินการที่แตกต่างกันที่นี่เพราะเรากำลังยืนยันว่าทั้งสองค่าที่รู้จักกันคูณกันเท่ากับค่าอื่นที่รู้จักกัน (ในขณะที่ยังทำในรุ่นทางเลือกของการ regex ไม่ในโพสต์นี้เพื่อทดสอบจำนวนเป็นตารางที่สมบูรณ์แบบ) ในคำตอบ regex อื่น ๆ ที่โพสต์ส่วนใหญ่ของฉันการคูณจะถูกนำมาใช้เป็นการคำนวณ วิธีการทั้งสองทำงานในทั้งสองสถานการณ์ แต่นักกอล์ฟก็ฉลาดกว่าในการทำงานซึ่งกันและกัน

^(?=(x((x{14})(x+)))(?=(\1*)\4\2*$)(\1*$\5))\6\3?(?!(xx+)\7+$)

ลองออนไลน์!


 # For the purposes of these comments, the input number = N.
 ^
 # Find two numbers A and A+14 such that A*(A+14)==N.
 (?=
     (x((x{14})(x+)))   # \1 = A+14; \2 = \1-1; \3 = 14; \4 = A-1; tail -= \1
     (?=                # Assert that \1 * (\4+1) == N.
         (\1*)\4\2*$    # We are asserting that N is the smallest number satisfying
                        # two moduli, thus proving it is the product of A and A+14
                        # via the Chinese Remainder Theorem. The (\1*) has the effect
                        # of testing every value that satisfies the "≡0 mod \1"
                        # modulus, starting with the smallest (zero), against "\4\2*$",
                        # to see if it also satisfies the "≡\4 mod \2" modulus; if any
                        # smaller number satisfied both moduli, (\1*) would capture a
                        # nonzero value in \5. Note that this actually finds the
                        # product of \4*\1, not (\4+1)*\1 which what we actually want,
                        # but this is fine, because we already subtracted \1 and thus
                        # \4*\1 is the value of tail at the start of this lookahead.
                        # This implementation of multiplication is very efficient
                        # golf-wise, but slow, because if the number being tested is
                        # not even divisible by \1, the entire test done inside this
                        # lookahead is invalid, and the "\1*$" test below will only
                        # fail after this useless test has finished.
     )
     (\1*$\5)           # Assert that the above test proved \1*(\4+1)==N, by
                        # asserting that tail is divisible by \1 and that \5==0;
                        # \6 = tool to make tail = \1
 )
 # Assert that either A or A+14 is prime.
 \6                     # tail = \1 == A+14
 \3?                    # optionally make tail = A
 (?!(xx+)\7+$)          # Assert tail is prime. We don't need to exclude treating
                        # 1 as prime, because the potential false positive of N==15
                        # is already excluded by requiring \4 >= 1.
 


3

Brachylog , 13 12 ไบต์

ṗ;14{+|-};?×

ป้อนหมายเลขผู้สมัครเป็นอาร์กิวเมนต์บรรทัดคำสั่ง ขาออกหรือtrue ลองออนไลน์!false

คำอธิบาย

รหัสเป็นภาคแสดงซึ่งมีการป้อนข้อมูลไม่ จำกัด และมีการส่งออกเป็นจำนวนที่เรากำลังทดสอบ

ṗ             Let the input ? be a prime number
 ;14          Pair it with 14, yielding the list [?, 14]
    {+|-}     Either add or subtract, yielding ?+14 or ?-14
         ;?   Pair the result with the input, yielding [?+14, ?] or [?-14, ?]
           ×  Multiply; the result must match the candidate number

(ยินดีต้อนรับเคล็ดลับที่{+|-}ยังคงรู้สึก clunky)


3

Brachylogขนาด 9 ไบต์

วิธีการที่แตกต่างกันแล้วคำตอบของDLosc

Ċ-14&∋ṗ&×

ใช้ N เป็นเอาท์พุตให้ [P, P-14] หรือ [P + 14, P] ย้อนกลับผ่านอินพุต (หมายเลขที่ใหญ่ที่สุดก่อน)

คำอธิบาย

Ċ              # The 'input' is a pair of numbers
 -14           #   where the 2nd is 14 smaller then the first
    &∋ṗ        #   and the pair contains a prime
       &×      #   and the numbers multiplied give the output (N)

ลองออนไลน์!


2

Pyth, 22 20 ไบต์

}Qsm*Ld+Ld_B14fP_TSh

ลองออนไลน์ได้ที่นี่

}Qsm*Ld+Ld_B14fP_TShQ   Implicit: Q=eval(input())
                        Trailing Q inferred
                  ShQ   Range [1-(Q+1)]
              fP_T      Filter the above to keep primes
   m                    Map the elements of the above, as d, using:
          _B14            [14, -14]
       +Ld                Add d to each
    *Ld                   Multiply each by d
  s                     Flatten result of map
}Q                      Is Q in the above? Implicit print

แก้ไข: บันทึก 3 ไบต์เนื่องจากอินพุตจะเป็นค่าบวกเสมอดังนั้นไม่จำเป็นต้องกรองค่าลบออกจากรายการ แก้ไขข้อบกพร่องสำหรับอินพุต1และยัง2คิดต้นทุน 1 ไบต์ รุ่นก่อนหน้า:}Qsm*Ld>#0+Ld_B14fP_TU


2

05AB1E , 16 15 14 ไบต์

บันทึก 1 ไบต์ด้วยการคำนวณ 14 ด้วยแทนžvÍ(ไม่เชื่อว่าฉันไม่ได้คิดถึงสิ่งนี้ตั้งแต่แรก)

บันทึก 1 ไบต์ขอบคุณ Emigna

ÅPε7·D(‚+y*Q}Z

ลองออนไลน์! หรือทดสอบอินพุตทั้งหมด

คำอธิบาย

                 # Implicit input n
ÅP               # Push a list of primes up to n
  ε         }    # For each prime in the list...
   7·            # Push 14 (by doubling 7)
     D(‚         # Push -14 and pair them together to get [14,-14]
        +        # Add [14,-14] to the prime
         y*      # Multiply the prime to compute p(p-14) and p(p+14)
           Q     # Check if the (implicit) input is equal to each element
             Z   # Take the maximum

1
คุณสามารถบันทึกไบต์โดยเปลี่ยน}˜såเป็นการQ}Zใช้อินพุตโดยนัย ชุดทดสอบของคุณจะต้องเปลี่ยนเป็นแบบนี้เพื่อให้มันทำงานได้ นอกจากนี้วิธีการเขียนที่ชัดเจนยิ่งขึ้นžvÍหรือจะเป็น14;)
Emigna

ขอบคุณ! ทำไมต้องทำให้ง่ายขึ้นเมื่อคุณสามารถกด 14 ในวิธีที่ซับซ้อนที่สุด / facepalm :)
38913


2

เรติน่า 0.8.2 , 61 ไบต์

.+
$*
^((1{14})1(1)+)(?<=(?<!^\4+(..+))\2?)(?<-3>\1)+$(?(3)1)

ลองออนไลน์! คำอธิบาย:

.+
$*

แปลงเป็นเอก

^((1{14})1(1)+)

\1จับปัจจัยที่ใหญ่กว่าของทั้งสอง \2จับค่าคงที่ 14 บันทึกไบต์ \3จับขนาดเล็กลงของสองปัจจัยลบ 1 ซึ่งจะช่วยให้มั่นใจว่าทั้งสองอย่างน้อย 2

(?<=(?<!^\4+(..+))\2?)

ตรวจสอบสองปัจจัยเพื่อให้แน่ใจว่าอย่างน้อยหนึ่งในนั้นสำคัญที่สุด ความคิดในการใช้\2?ถูกขโมยลงคอจากคำตอบของ @ Deadcode

(?<-3>\1)+

ทำซ้ำสองปัจจัยที่มีขนาดใหญ่กว่าจำนวนครั้งเท่ากับหนึ่งน้อยกว่าปัจจัยที่น้อยกว่าของทั้งสอง เนื่องจากเราได้จับปัจจัยที่มีขนาดใหญ่ขึ้นเมื่อสิ่งนี้จบลงด้วยการจับผลิตภัณฑ์ของทั้งสองปัจจัย

$(?(3)1)

ตรวจสอบให้แน่ใจว่าผลิตภัณฑ์เท่ากับจำนวนที่กำหนด

แปลโดยตรงกับ Retina 1 โดยการแทนที่$*ด้วย*1จะมีการนับไบต์เดียวกัน แต่ไบต์จะถูกบันทึกไว้โดยการแทนที่ทั้งหมด1s พร้อมด้วย_และจากนั้น*1จะถูกแทนที่ด้วยมากกว่า* *_Retina 1 ก่อนหน้านี้คำตอบสำหรับ 68 ไบต์:

.+
*
Lw$`^(__+)(?=(\1)+$)
$1 _$#2*
Am` (__+)\1+$
(_+) \1

0m`^_{14}$

ลองออนไลน์! คำอธิบาย:

.+
*

แปลงเป็นเอก

Lw$`^(__+)(?=(\1)+$)
$1 _$#2*

ค้นหาคู่ของปัจจัยทั้งหมด

Am` (__+)\1+$

ตรวจสอบให้แน่ใจว่าเป็นหนึ่งเดียว

(_+) \1

รับความแตกต่างที่แน่นอน

0m`^_{14}$

ตรวจสอบว่ามี 14 หรือไม่


1

JavaScript (โหนดบาเบล) 69 ไบต์

ประณามฉันแม้ว่าฉันจะเอาชนะคำตอบของ Arnaulds แต่ไม่ใช่ ..... : c

x=>[...Array(x)].some((a,b)=>x/(a=(p=n=>--b-1?n%b&&p(n):n)(b))-a==14)

ลองออนไลน์!

ฉันต้องการกำจัดx=>[...Array(x)].some(ส่วนที่ใช้การเรียกซ้ำเพื่อให้สั้นลงเมื่อเวลาผ่านไป

คำอธิบาย

x=>[...Array(x)]                                                              Creates a range from 0 to x-1 and map:

                .some((a,b)=>                                                 Returns True if any of the following values is true
                             x/                                              Input number divided by
                                (a=(p=n=>--b-1?n%b&&p(n):n)(b))               recursive helper function. Receives a number (mapped value) as parameters and returns 
                                                                              the same number if it is prime, otherwise returns 1. Take this value
                                                                              and assign to variable a
                                                               -a            Subtract a from the result  
                                                                     ==14    Compare result equal to 14

มันใช้สูตร

n/pp==14




1

APL (NARS) 16 ตัวอักษร 32 ไบต์

{14=∣r-⍵÷r←↑⌽π⍵}

{π⍵} จะพบการแยกตัวประกอบของการโต้แย้งของมันและเราคิดว่าองค์ประกอบสุดท้ายของผล (รายการของตัวหารของ n) เป็นตัวหารสูงสุดของ n; ที่นี่เราสมมติว่าคำจำกัดความหนึ่งที่เทียบเท่าของหมายเลข Rocco คือ: n เป็นจำนวน Rocco <=> ค่าแฟคเตอร์สูงสุดของ n: r เป็นเช่นนั้นจริง 14 = ∣rn ÷ r [สำหรับ C pseudocode เป็น 14 == abs (rn / r) คำจำกัดความของหมายเลข Rocco นี้ดูเหมือนว่าใช้ได้ในช่วง 1..1000000]; ช่วงของค่า ok จะเป็น 1..maxInt; ทดสอบ:

 f←{14=∣r-⍵÷r←↑⌽π⍵}
 {⍞←{1=f ⍵:' ',⍵⋄⍬}⍵⋄⍬}¨1..10000
32  51  95  147  207  275  351  435  527  627  851  1107  1247  1395  1551  1887  2067  2255  2451  2655  2867  3551  4047  4307  4575  5135  5427  5727  6035  6351  6675  7347  8051  8787  9167  9951   

1

C # (Visual C # Interactive คอมไพเลอร์) , 99 ไบต์

n=>Enumerable.Range(2,n).Any(p=>Enumerable.Range(2,p).All(y=>y>=p|p%y>0)&(n==p*(p+14)|n==p*(p-14)))

ลองออนไลน์!

Enumerable.Range นัดอีกครั้ง :) ด้วยการใช้ธงคอมไพเลอร์บ้าคุณสามารถลดสิ่งต่าง ๆ ลงได้เล็กน้อยแม้ว่าฉันจะเป็นแฟนของโซลูชันวานิลลา

C # (Visual C # Interactive Compiler) + /u:System.Linq.Enumerable, 77 ไบต์

n=>Range(2,n).Any(p=>Range(2,p).All(y=>y>=p|p%y>0)&(n==p*(p+14)|n==p*(p-14)))

ลองออนไลน์!

ด้านล่างเป็นวิธีแก้ปัญหาของ Arnauld ที่ดูเท่มาก ปัจจุบันเป็นสนามที่ยาวที่สุด แต่ก็สามารถตีกอล์ฟได้บ้าง

C # (Visual C # Interactive คอมไพเลอร์) , 101 ไบต์

n=>{bool g(int k)=>--k<2?n>1:n%k>0&g(k);var d=Math.Sqrt(n+49)-7;return(n=(int)d)==d&(g(n)|g(n+=14));}

ลองออนไลน์!


0

APL (NARS) 30 ตัวอักษร 60 ไบต์

{∨/{0=1∣⍵:0π⍵⋄0}¨(7,¯7)+√49+⍵}

0πนี่คือฟังก์ชั่นบอกว่าถ้าจำนวนหนึ่งเป็นจำนวนเฉพาะทดสอบ:

 f←{∨/{0=1∣⍵:0π⍵⋄0}¨(7,¯7)+√49+⍵}
 {⍞←{1=f ⍵:' ',⍵⋄⍬}⍵⋄⍬}¨0..700
32  51  95  147  207  275  351  435  527  627

0

F #, 2 คำตอบ (ไม่ใช่การแข่งขัน)

ฉันชอบคำตอบของ @Arnauld ดังนั้นฉันจึงแปลมัน

123 ไบต์ขึ้นอยู่กับคำตอบของ JavaScript

fun n->let t=int<|sqrt(float n+49.)in Seq.map(fun n->Seq.filter(fun i->n%i=0)[1..n]|>Seq.length=2)[t-7;t+7]|>Seq.reduce(||)

คำอธิบาย:

fun n->let t=int<|sqrt(float n+49.)in Seq.map(fun n->Seq.filter(fun i->n%i=0)[1..n]|>Seq.length=2)[t-7;t+7]|>Seq.reduce(||) //Lambda which takes an integer, n
       let t=int<|sqrt(float n+49.)                                                                                         //let t be n, converted to float, add 49 and get square root, converted back to int (F# type restrictions)
                                   in                                                                                       //in the following...
                                                                                                  [t-7;t+7]                 //Subtract and add 7 to t in a list of 2 results (Lists and Seqs can be interchanged various places)
                                      Seq.map(fun n->Seq.filter(fun i->n%i=0)[1..n]|>Seq.length=2)                          //See if either are prime (here, if either result has 2 and only 2 divisors)
                                                                                                           |>Seq.reduce(||) //And logically OR the resulting sequence

125 ไบต์ขึ้นอยู่กับคำตอบของ 05AB1E

fun n->let l=Seq.filter(fun i->n%i=0)[2..n-1]in let m=Seq.map(fun i->n/i)l in Seq.map2(fun a b->abs(a-b))l m|>Seq.contains 14

คำอธิบาย:

fun n->let l=Seq.filter(fun i->n%i=0)[2..n-1]in let m=Seq.map(fun i->n/i)l in Seq.map2(fun a b->abs(a-b))l m|>Seq.contains 14  //Lambda which takes an integer, n
       let l=Seq.filter(fun i->n%i=0)[2..n-1]                                                                                  //let l be the list of n's primes 
                                             in                                                                                //in...
                                                let m=Seq.map(fun i->n/i)l                                                     //m, which is n divided by each of l's contents
                                                                           in                                                  //and then...
                                                                              Seq.map2(fun a b->abs(a-b))l m                   //take the absolute difference between each pair of items in the two sequences to make a new sequence
                                                                                                            |>Seq.contains 14  //and does the resulting sequence contain the number 14?

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.