ปิรามิดรวมของจำนวนเฉพาะ


24

ด้วยหมายเลข N ความท้าทายคือการหาผลรวมของปิรามิดของจำนวนเฉพาะ เพื่อชี้แจงสิ่งต่าง ๆ นี่คือตัวอย่าง:

Input: 4

เราจะแสดง4ช่วงเวลาแรกและคำนวณผลรวมของพวกเขา หลังจากนั้นเราจะคำนวณผลรวมของจำนวนเงินและ:

  2
    > 5
  3     > 13
    > 8      > 33
  5     > 20
    > 12
  7

คุณสามารถเห็นได้ว่าผลสุดท้ายคือ33 นี่เป็นอีกตัวอย่างหนึ่งที่มี N = 8:

   2
     >   5
   3       >  13
     >   8       >  33
   5       >  20       >  83
     >  12       >  50       > 205
   7       >  30       > 122       > 495
     >  18       >  72       > 290       > 1169
  11       >  42       > 168       > 674
     >  24       >  96       > 384
  13       >  54       > 216
     >  30       > 120
  17       >  66
     >  36
  19

คุณสามารถเห็นได้ว่าผลสุดท้ายคือ1169

นี่เป็นอีกตัวอย่างหนึ่งที่มีคี่ N, N = 3:

 2
   > 5
 3     > 13
   > 8
 5

สิ่งนี้ทำให้เรามีผลลัพธ์13อย่าง

งานของคุณคือการเขียนโปรแกรมหรือฟังก์ชั่นที่ใช้จำนวนเต็มมากกว่า 0 และส่งออกผลลัพธ์สุดท้าย

นี่คือผลการทดสอบบางส่วน:

1:  2
2:  5
3:  13
4:  33
5:  83
6:  205
7:  495
8:  1169
9:  2707
10: 6169
11: 13889
12: 30993
13: 68701
14: 151469
15: 332349
16: 725837
17: 1577751
18: 3413221
19: 7349029
20: 15751187
21: 33616925
22: 71475193
23: 151466705
24: 320072415
25: 674721797
26: 1419327223
27: 2979993519
28: 6245693407
29: 13068049163
30: 27297614797
31: 56929779663
32: 118543624847
33: 246475746269
34: 511766428817
35: 1061264813321
36: 2198298700845
37: 4548996804811
38: 9405003164065
39: 19429190057417
40: 40107799133677
41: 82736199371081
42: 170553108953473
43: 351333736092089
44: 723224546040181
45: 1487710742395387
46: 3058157261678325
47: 6282142186547177
48: 12896743408107403
49: 26460652594917673
50: 54262186256186881
51: 111224391050741687
52: 227896496141836195
53: 466805185374509003
54: 955904519939662217
55: 1956988697590280537
56: 4005572366722212927
57: 8196803221276230093
58: 16769645303734608963
59: 34300013739423719561
60: 70136585692535099353
61: 143371352962891226373
62: 292978031452308375001
63: 598482012866917021541
64: 1222083126601616763473
65: 2494459637841415902073
66: 5089478703050176444803
67: 10379794709536133386939
68: 21160351440305258275579
69: 43119914481530819445497
70: 87833066190052490228187
71: 178841897161848754603319
72: 364014682565128163812791
73: 740654046243174781813209
74: 1506496270380756958474835
75: 3063280375436290387756263
76: 6227039507615221644290617
77: 12655020557561801933128885
78: 25712267089927372837530869
79: 52230425385198423845305957
80: 106076955379202815098486497
81: 215397386589448754140867649
82: 437308717912632286770415395
83: 887706233370396897803709611
84: 1801721089699452657985592689
85: 3656329898231436156162865559
86: 7418972676822310377574227797
87: 15051599987013574096449515927
88: 30532404546282900804722616529
89: 61926565462373271494414919017
90: 125582269494835615524470915169
91: 254631689768733901573206365479
92: 516210444730946464864091626473
93: 1046330617753410129672316234861
94: 2120493010460433691014704829565
95: 4296639990460140795780826898943
96: 8704509990931940668688755806845
97: 17631229933967301681217551193565
98: 35706243541395815998303171050377
99: 72298621492552303967009812018997

นี่คือดังนั้นจำนวนไบต์ที่สั้นที่สุดชนะ!


1
เอาท์พุทผลรวมในรายการองค์ประกอบเดียว (เช่น[1169]สำหรับ8) ยอมรับได้หรือไม่?
Mego

@Mego ใช่ตราบใดที่มันเป็นผลลัพธ์สุดท้าย
Adnan

เราจะต้องสนับสนุนกรณีทดสอบทั้งหมดถึง 99 หรือไม่ หลายภาษา (เช่น JavaScript) ไม่สามารถนับได้ว่าสูงโดยไม่สูญเสียความแม่นยำ
ETHproductions

1
@ETHproductions เพียงสูงสุด 27 ซึ่งมีผลสูงสุดต่ำกว่า 2 ^ 32 - 1 (ค่า int สูงสุดที่ไม่ได้ลงนาม)
Adnan

คำตอบ:


12

J, 15 ไบต์

p:@i.+/ .*i.!<:

คำอธิบาย:

โดยทั่วไปเช่นเดียวกับคำตอบของฉัน Mathematica

p:@i.+/ .*i.!<:
          i.!<:    binomial coefficients
p:@i.              first n primes
     +/ .*         dot product


10

Minkolang 0.14 , 17 ไบต์

n[i3M$i1-i6M*+]N.

ลองได้ที่นี่และตรวจสอบกรณีทดสอบทั้งหมดที่นี่

คำอธิบาย

n                    Take number from input (N)
 [                   Open for loop that repeats N times
  i                  Loop counter (n)
   3M                Pop n and push nth prime (where 2 is the 0th prime)
     $i1-            Max iterations - 1 (which is N-1)
         i           Loop counter (n)
          6M         Pop n,k and push kCn (binomial)
            *+       Multiply and add
              ]      Close for loop
               N.    Output as number and stop.

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

คำอธิบายเพิ่มเติม

2
  > [2,3]
3         > [2,3,3,5]
  > [3,5]             > [2,3,3,3,5,5,5,7]
5         > [3,5,5,7]
  > [5,7]
7

อย่างที่คุณเห็นช่วงเวลาที่2,3,5,7ปรากฎ1,3,3,1ในผลลัพธ์สุดท้าย Lemme เปลี่ยนเค้าโครงเล็กน้อย

_ _ _ 7
_ _ 5
_ 3
2

จำนวนครั้งที่ความ3ประสงค์จะมีส่วนร่วมกับผลลัพธ์สุดท้ายนั้นเท่ากับจำนวนเส้นทางจาก3ไปยังมุมบนซ้ายเลื่อนขึ้นและซ้ายเท่านั้น ที่นี่มีสามเส้นทางดังกล่าวสำหรับ3:

_    _    _ _
_    _ _    _
_ 3    3    3

โปรดทราบว่าฉันสามารถย้อนกลับทิศทางได้โดยไม่สูญเสียความคิดทั่วไป ดังนั้นฉันอยากรู้ว่ามีกี่เส้นทางจากมุมซ้ายบนไปยังแต่ละตำแหน่งตามขอบขรุขระ ฉันนับได้อย่างนั้น ...

1 1 1 1 1 . . .
1 2 3 4
1 3 6
1 4   .
1       .
.         .
.
.

สำหรับทุกหมายเลขในสามเหลี่ยมนี้ถ้าเป็นหน่วย X จากด้านซ้ายและหน่วย Y จากด้านบนจำนวนที่ตำแหน่งนั้นคือ

ป้อนคำอธิบายรูปภาพที่นี่

แม้ว่าวิธีที่ฉันใช้จะX+Y = Nคงที่และXอยู่ในช่วงตั้งแต่ 0 ถึงNซึ่งไปตามแนวทแยงมุมหนึ่งเส้น ฉันคูณค่าสัมประสิทธิ์แต่ละตัวด้วยจำนวนเฉพาะที่สอดคล้องกันแล้วเพิ่มขึ้นทั้งหมด

ดูบทความ Wikipedia บนสามเหลี่ยมของ Pascalสำหรับข้อมูลเพิ่มเติมเกี่ยวกับเรื่องนี้


8
ฉันพบว่าการอธิบายค่อนข้างสวยงาม +1
Adnan

7

JavaScript ES7 107

การใช้วงเงินที่ไม่เหมาะสมที่ 27 - การที่น่าเบื่อคือการหาช่วงเวลาที่เหมาะสม

n=>eval("t=2;for(p=[for(v of'012242424626424662642646842')t-=-v];--n;)p=p.slice(0,n).map((v,i)=>v+p[i+1])")

ทดสอบข้อมูลโค้ด (โดยใช้ความเข้าใจของอาเรย์มันจะใช้ได้เฉพาะใน Firefox)

F=n=>eval("t=2;for(p=[for(v of'012242424626424662642646842')t-=-v];--n;)p=p.slice(0,n).map((v,i)=>v+p[i+1])")

// Less golfed

Q=n=>{
  t=2;
  // Note: the golfed version will return the last computed value, that is p if the loop is entered, else t=2
  p=[for(v of '012242424626424662642646842') t-=-v] // build the array of first 27 primes in p
  while(--n) p = p.slice(0,n).map((v,i)=>v+p[i+1])  
  return p
}  

//TEST
console.log=x=>O.innerHTML+=x+'\n'

for(i=1;i<28;i++)console.log(i+' : '+F(i))
<pre id=O></pre>


เป็นไปได้ที่จะย่อรหัสโดยใช้การตรวจสอบเฉพาะของ regex หรือไม่
n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳

6

Pyth, 18 ไบต์

husM.:G2tQ.f}ZPZQ0

ลองใช้งานออนไลน์: การสาธิต

คำอธิบาย:

husM.:G2tQ.f}ZPZQ0   implicit: Q = input number
          .f    Q0   find the first Q numbers Z >= 0, which satisfy
            }ZPZ        Z appears in the prime factorization of Z
                     this gives the first Q prime numbers
 u      tQ           assign this list to G and repeat the following Q-1 times:
    .:G2               create all subarrays of length 2
  sM                   sum them up and update G
h                    take the first element (=result) and print

นอกจากนี้ 18:s*V.cLtQUQ.f}ZPZQ0
Sp3000

@ Sp3000 โอ้ว้าวนี่คล้ายกับคำตอบของฉันมาก - แต่ฉันไม่ได้ดูความคิดเห็นของคุณ
orlp


5

Pyth, 16 ไบต์

s*V.cLtQQ.f}ZPZQ

ง่ายมากจริง ๆ :

s*V          ; Dot product of
  .cLtQQ     ; the binomial coefficients for n
  .f}ZPZQ    ; and the first n prime numbers.

4

Haskell, 74 ไบต์

import Data.Numbers.Primes
f n=([]:iterate(zipWith(+)=<<tail)primes)!!n!!0

ตัวอย่างการใช้งาน:

*Main> map f [1..12]
[2,5,13,33,83,205,495,1169,2707,6169,13889,30993]

มันทำงานอย่างไร: คำนวณผลรวมของเพื่อนบ้านทุกช่วง เอาหัวnซ้ำซ้ำ

[2,3,5,7,11,13,17,19,23,29,...]             -- plain primes (and 1st iteration)
[5,8,12,18,24,30,36,42,52,60,...]           -- 2nd iteration of neighbor sums
[13,20,30,42,54,66,78,94,112,128,...]       -- 3rd iteration
[33,50,72,96,120,144,172,206,240,274,...]
...

ในฐานะที่เป็นผู้ประกอบการที่ดัชนี!!จะขึ้นอยู่กับศูนย์ผม prepending !!(n-1)รายการที่ว่างเปล่าเพื่อหลีกเลี่ยงการใช้งาน


4

Matlab, 76 ไบต์

ขอบคุณ David สำหรับการบันทึกจำนวนมาก!

n=input('');x=primes(103);
for s=2:n,x=conv(x,[1 1]);end
disp(num2str(x(n)))

รุ่นเก่า 98 ไบต์

n=input('');m=1;x=[];while nnz(x)<n
m=m+1;x=primes(m);end
for s=2:n,x=conv(x,[1 1]);end
disp(x(n))

การxใช้งานx=primes(103);จะช่วยประหยัดบางไบต์เนื่องจากคุณจะต้องไปถึงN=27(และไม่สำคัญว่าจะxมีรายการมากกว่าที่คุณต้องการ) convเป็นความคิดที่ดีแม้ว่า!
David

@ David ขอบคุณ! ฉันไม่ได้เห็นความท้าทายนั้นสูงถึง 27
Luis Mendo

3

JavaScript (ES6), 121 ไบต์

n=>eval(`for(p=[],c=0,x=1;c<n;s?p[c++]=x:0)for(s=i=++x;--i>1;)x%i?0:s=0;for(;--c;p=s)for(i=c,s=[];i;)s[c-i]=p[i]+p[--i]`)

คำอธิบาย

ขนาดส่วนใหญ่มาจากการค้นหาหมายเลขเฉพาะ

n=>
  eval(`                   // eval used to enable for loops without {} or return

    // Get primes up to n
    for(                   // loop from range 2 to n
      p=[],                // p = primes
      c=0,                 // c = count of primes
      x=1;                 // x = current number to check for primality
      c<n;
      s?p[c++]=x:0         // add the number to the primes if it has no divisors
    )
      for(                 // loop from range 2 to x to check for divisors
        s=                 // s = true if x is a prime
          i=++x;
        --i>1;
      )
        x%i?0:s=0;         // check if x has a divisor

    // Sum primes
    for(;--c;p=s)          // while the new pyramid has pairs to sum
      for(i=c,s=[];i;)     // loop through each pair of the pyramid
        s[c-i]=p[i]+p[--i] // push the sum of the pair to the new pyramid s
  `)                       // implicit: return the final sum

ทดสอบ



2

จริงจัง 23 ไบต์

,r`P`M;lD`;pX@dXZ'Σ£M`n

ส่งออกผลลัพธ์เป็นรายการความยาว 1: 8 -> [1169]

ลองออนไลน์

คำอธิบาย:

,r`P`M    push the first n primes as a list
;lD       push 1 minus the length of the list (we'll call this k) ([2,3,5,7],3)
`...`n    call the following function k times:
    ;pX      duplicate the list, pop and discard the first element
    @dX      swap, pop and discard the last element
    Z        zip the two lists
    'Σ£      push the string "Σ" and convert to function
    M        map the function over the list

2

Mathematica 73 ไบต์

NestWhile[Plus@@@Partition[#,2,1]&,Prime@n~Table~{n,#},Length@#>1&][[1]]&

มันทำงานอย่างไร

Prime@n~Table~{n,#}&ให้รายการของ#ช่วงเวลาแรก

Partition[#,2,1]&จัดเรียงรายการของตัวเลขเป็น {a, b, c, d ...} {{a,b}, {b,c}, {c,d}...}}

Plus@@@{a+b, b+c, c+d...}แล้วผลตอบแทน

NestWhileเริ่มต้นด้วยรายการของ#ช่วงเวลาและนำไปใช้ซ้ำ ๆPlus@@@Partition...ตราบใดที่มีมากกว่าหนึ่งหมายเลขในรายการ


NestWhile[Plus@@@Partition[#,2,1]&,Prime@n~Table~{n,#},Length@#>1&][[1]]&[4]

33


NestWhile[Plus @@@ Partition[#, 2, 1] &, Prime@n~Table~{n, #}, Length@# > 1 &][[1]] &[5]

83


ใช้เวลาประมาณ 1/5 วินาทีในการแก้ปัญหาสำหรับ 1,000 ครั้งแรก

NestWhile[Plus @@@ Partition[#, 2, 1] &, Prime@n~Table~{n, #}, 
 Length@# > 1 &][[1]] &[10^3] // AbsoluteTiming

{0.185611, 1917231113909474354152581359443368948301825453723617274940459548079399 7849439430405641625002631859205971635284844253657654843025188471660669 0868945436580032828177831204066809442374364181056590286849530757875874 9185665854180901580438781223737728559484382552514103542932932981340942 3918431043908415228663677}


1

Python 2, 159 ไบต์

m=int(input())
q=[]
x=2
while len(q)<m:
 if not any([x%g<1 for g in q]):q+=[x]
 x+=1
for i in range(m-1):
 for p in q:q+=[q[1]+q[0]];q.pop(0)
 print(q.pop())
print q

1
บางทีฉันอาจจะขาดอะไรบางอย่าง ... แต่ทำไมprintคำสั่งภายในวง? คุณไม่ต้องการที่จะพิมพ์เพียงครั้งเดียวในตอนท้าย?
mathmandan

1

ทางช้างเผือก 1.4.8 , 26 25 ไบต์

คำตอบนี้ไม่ได้แข่งขัน การดำเนินการบางอย่างถูกสร้างขึ้นหลังจากโพสต์คำถามนี้ (แต่ไม่จำเป็นสำหรับความท้าทายนี้)

'E&{~F§{G}:y1ba?{_^_}};!

ฉันสามารถลบไบต์หลังจากอ่านความคิดเห็น เอาท์พุทเป็นรายการองค์ประกอบเดียว


คำอธิบาย

'                        #  read input from the command line
 E                       #  push a list of the first N primes
  &{~                }   #  while loop
     F                   #  push the sum of TOS elements i.e. [A, B, C] => [[A,B], [B,C]]
      §{ }               #  mapping
        G                #  sum i.e. [1, 2, 3] => 6
          :              #  duplicate the TOS
           y             #  push the length of the TOS to the stack
            1            #  push 1 to the stack
             b           #  evaluate equality of the TOS and STOS
              a          #  logical not
               ?{_ _}    #  if-else statement
                  ^      #  pop the TOS
                     ;   #  swap the TOS and STOS
                         #  dump the TOS to the stack
                      !  #  output the TOS

การใช้

python3 milkyway.py <path-to-code> -i <input-integer>

1

ศรีลังกา 169 ไบต์

alias I=>Integer;I s(I*l)=>l.size<2then(l[0]else 0)else s(*l.paired.map((I[2]i)=>i[0]+i[1]));I p(I n)=>s(*loop(2)(1.plus).filter((c)=>!(2:c-2).any((d)=>c%d<1)).take(n));

นี่เป็นการกำหนดสองฟังก์ชัน - sคำนวณผลรวมของปิรามิดของลำดับของจำนวนเต็มในขณะที่pเรียกสิ่งนี้ตามลำดับของnช่วงเวลาแรก

ดูเหมือนว่าครึ่งหนึ่งของขนาดกำลังค้นหาnช่วงเวลาแรกส่วนอีกครึ่งหนึ่งกำลังคำนวณผลรวมของปิรามิด

นี่คือรูปแบบที่จัดรูปแบบ / แสดงความคิดเห็น:

// Sum pyramid of primes
//
// Question:  http://codegolf.stackexchange.com/q/65822/2338
// My answer: http://codegolf.stackexchange.com/a/65879/2338

alias I => Integer;

// Calculate the pyramid sum of some sequence.
I s(I* l) =>
        // If less than two elements ...
        l.size < 2
        // then use the first (only element), or 0 if no such.
        then (l[0] else 0)
        // otherwise,
        else s(*
               // take the iterable of pairs of consecutive elements,
               l.paired
               // and add each of them together.
                .map((I[2] i) => i[0] + i[1])
               // then apply s (recursively) on the result.
               );

// Calculate the pyramid sum of the first n primes.
I p(I n) => s(*
              // the infinite sequence of integers, starting with 2.
              loop(2)(1.plus)
              // filter by primality (using trial division)
              .filter((c) => !(2 : c-2)
                              .any((d) => c%d < 1))
              // then take the first n elements
              .take(n)
              // then apply s on the result.
             );

@FlagAsSpam ทำ ... ขอโทษฉันลืมไปแล้ว
Paŭlo Ebermann

1

เยลลี่ขนาด 7 ไบต์

ÆN€+ƝƬṀ

ลองออนไลน์!

แต่เดิมฉันเขียนคำตอบของ Brachylog 1<|~lṗᵐ≠≜{s₂ᶠ+ᵐ}ⁱ~gแต่เมื่อมันออกมาถึง 19 ไบต์ฉันตัดสินใจว่าฉันอาจจะลองใช้ภาษาอื่น

      Ṁ    The largest value from
     Ƭ     every stage of repeatedly
   +       adding
    Ɲ      adjacent values, starting with
ÆN         nth prime
  €        mapped over the input.

เห็นได้ชัดว่าการทำแผนที่มากกว่าถือว่าจำนวนเป็นช่วงตั้งแต่ 1 ถึงตัวเองรวมและเลขเรียงลำดับตามที่สูงกว่ารายชื่อหรือสิ่งที่''เป็น


1

APL (NARS), 41 ตัวอักษร, 82 ไบต์

{1=≢⍵:↑⍵⋄∇+/¨¯1↓⍵,¨1⌽⍵}∘{⍵↑v/⍨0πv←⍳1+⍵×⍵}

ในอินพุตหากต้องการใช้ตัวเลขขนาดใหญ่ต้องป้อนชนิด number_x เป็น 47x อาจมีบางอย่างที่ไม่เป็นไร: ฉันเขียนที่นี่ว่ามีเฉพาะช่วงเวลาในเซต 1..n ^ 2 ทดสอบ:

  h←{1=≢⍵:↑⍵⋄∇+/¨¯1↓⍵,¨1⌽⍵}∘{⍵↑v/⍨0πv←⍳1+⍵×⍵}
  h 1
2
  h 2
5
  h 9
2707
  h 24
320072415
  h 47x
6282142186547177 
  h 99x
72298621492552303967009812018997 
  h 200x
433205808657246411262213593770934980590715995899633306941417373

1

Perl 6 , 52 ไบต์

{grep(&is-prime,1..*)[^$_],{[|$_]Z+.skip}...1& &say}

ลองออนไลน์!

รหัสบล็อกที่ไม่ระบุชื่อที่รับอาร์กิวเมนต์และพิมพ์รายการองค์ประกอบหนึ่งรายการที่มีผลลัพธ์


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