คำนวณผลรวมของจำนวนเฉพาะจำนวน n ตัวแรก


15

ฉันประหลาดใจที่ความท้าทายนี้ไม่ได้อยู่ที่นี่เพราะมันชัดเจนมาก (หรือฉันประหลาดใจที่ฉันไม่พบมันและทุกคนจะทำเครื่องหมายว่าซ้ำกัน)

งาน

ได้รับไม่ใช่เชิงลบจำนวนเต็มnคำนวณผลรวมของแรกnเฉพาะและผลผลิตมัน

ตัวอย่างที่ 1

สำหรับn=5ห้าช่วงแรกคือ:

  • 2
  • 3
  • 5
  • 7
  • 11

ผลรวมของตัวเลขเหล่านี้คือ2+3+5+7+11=28ดังนั้นโปรแกรมที่มีการส่งออก2828

ตัวอย่างที่ 2

สำหรับช่วงเวลา "ศูนย์แรก" จะไม่มีเลย และผลรวมของตัวเลขไม่เป็น - แน่นอน - 0n=00

กฎระเบียบ

  • คุณอาจใช้บิวด์อินเช่นเพื่อตรวจสอบว่าตัวเลขนั้นดีหรือไม่
  • นี่คือดังนั้นจำนวนไบต์ต่ำสุดในแต่ละภาษาจะชนะ!



2
OEIS - A7504 (ด้าน: LOL ที่นี่ในส่วนสูตร "a (n) = A033286 (n) - A152535 (n).")
Jonathan Allan

@JonathanAllan: เกี่ยวข้อง แต่ไม่เทียบเท่า ฉันคิดว่ามันเป็นความแตกต่างที่สำคัญถ้าคุณตรวจสอบช่วงเวลาในช่วงหรือช่วงเวลาจำนวนหนึ่ง สิ่งที่งานทั้งสองมีเหมือนกันคือ a) ตรวจสอบว่าตัวเลขนั้นดีหรือไม่และข) สรุปตัวเลข - ซึ่งเป็นเรื่องปกติสำหรับงานเขียนโค้ดกอล์ฟจำนวนมากที่นี่
xanoetux

3
นอกจากนี้ยังเกี่ยวข้อง: สิ่งที่ควรหลีกเลี่ยงเมื่อเขียนความท้าทาย: ตัวเลขที่สำคัญ
Laikoni

คำตอบ:


15

รหัสประจำเครื่อง 6502 , 75 ไบต์

A0 01 84 FD 88 84 FE C4 02 F0 32 E6 FD A0 00 A5 FD C9 04 90 1F 85 64 B1 FB 85
65 A9 00 A2 08 06 64 2A C5 65 90 02 E5 65 CA D0 F4 C9 00 F0 DC C8 C4 FE D0 DB
A5 FD A4 FE 91 FB C8 D0 C8 A9 00 18 A8 C4 FE F0 05 71 FB C8 D0 F7 60

คาดว่าจะมีตัวชี้ไปยังที่เก็บชั่วคราวบางอย่างใน$fb/ และจำนวนของจำนวนเฉพาะที่จะสรุปใน$fc $2ส่งคืนผลรวมในA(การลงทะเบียน accu)

ไม่เคยทำการตรวจสอบที่สำคัญในรหัสเครื่อง 6502 ดังนั้นในที่สุดมันก็มาถึง;)

หมายเหตุ: การเริ่มต้นนี้ให้ผลลัพธ์ที่ไม่ถูกต้องสำหรับปัจจัยการผลิต> = 14 นี้เป็นเพราะล้นผลงานรหัสกับ "ธรรมชาติ" ช่วงจำนวนแพลตฟอร์ม 8bit ซึ่งเป็น0 - 255สำหรับการได้รับการรับรอง

ความคิดเห็นถอดแยกชิ้นส่วน

; function to sum the first n primes
;
; input:
;   $fb/$fc: pointer to a buffer for temporary storage of primes
;   $2:      number of primes to sum (n)
; output:
;   A:       sum of the first n primes
; clobbers:
;   $fd:     current number under primality test
;   $fe:     number of primes currently found
;   $64:     temporary numerator for modulo check
;   $65:     temporary divisor for modulo check
;   X, Y
 .primesum:
A0 01       LDY #$01            ; init variable for ...
84 FD       STY $FD             ; next prime number to test
88          DEY                 ; init number of found primes
 .mainloop:
84 FE       STY $FE             ; store current number of found primes
C4 02       CPY $02             ; compare with requested number
F0 32       BEQ .sum            ; enough primes -> calculate their sum
 .mainnext:
E6 FD       INC $FD             ; check next prime number
A0 00       LDY #$00            ; start check against first prime number
 .primecheckloop:
A5 FD       LDA $FD             ; load current number to check
C9 04       CMP #$04            ; smaller than 4?
90 1F       BCC .isprime        ; is a prime (shortcut to get list started)
85 64       STA $64             ; store to temp as numerator
B1 FB       LDA ($FB),Y         ; load from prime number table
85 65       STA $65             ; store to temp as divisor
A9 00       LDA #$00            ; init modulo to 0
A2 08       LDX #$08            ; iterate over 8 bits
 .bitloop:
06 64       ASL $64             ; shift left numerator
2A          ROL A               ; shift carry into modulo
C5 65       CMP $65             ; compare with divisor
90 02       BCC .bitnext        ; smaller -> to next bit
E5 65       SBC $65             ; otherwise subtract divisor
 .bitnext:
CA          DEX                 ; next bit
D0 F4       BNE .bitloop
C9 00       CMP #$00            ; compare modulo with 0
F0 DC       BEQ .mainnext       ; equal? -> no prime number
C8          INY                 ; next index in prime number table
C4 FE       CPY $FE             ; checked against all prime numbers?
D0 DB       BNE .primecheckloop ; no -> check next
 .isprime:
A5 FD       LDA $FD             ; prime found
A4 FE       LDY $FE             ; then store in table
91 FB       STA ($FB),Y
C8          INY                 ; increment number of primes found
D0 C8       BNE .mainloop       ; and repeat whole process
 .sum:
A9 00       LDA #$00            ; initialize sum to 0
18          CLC
A8          TAY                 ; start adding table from position 0
 .sumloop:
C4 FE       CPY $FE             ; whole table added?
F0 05       BEQ .done           ; yes -> we're done
71 FB       ADC ($FB),Y         ; add current entry
C8          INY                 ; increment index
D0 F7       BNE .sumloop        ; and repeat
 .done:
60          RTS

ตัวอย่างโปรแกรมประกอบ C64 โดยใช้รูทีน:

การสาธิตออนไลน์

รหัสในรูปแบบca65 :

.import primesum   ; link with routine above

.segment "BHDR" ; BASIC header
                .word   $0801           ; load address
                .word   $080b           ; pointer next BASIC line
                .word   2018            ; line number
                .byte   $9e             ; BASIC token "SYS"
                .byte   "2061",$0,$0,$0 ; 2061 ($080d) and terminating 0 bytes

.bss
linebuf:        .res    4               ; maximum length of a valid unsigned
                                        ; 8-bit number input
convbuf:        .res    3               ; 3 BCD digits for unsigned 8-bit
                                        ; number conversion
primebuf:       .res    $100            ; buffer for primesum function

.data
prompt:         .byte   "> ", $0
errmsg:         .byte   "Error parsing number, try again.", $d, $0

.code
                lda     #$17            ; set upper/lower mode
                sta     $d018

input:
                lda     #<prompt        ; display prompt
                ldy     #>prompt
                jsr     $ab1e

                lda     #<linebuf       ; read string into buffer
                ldy     #>linebuf
                ldx     #4
                jsr     readline

                lda     linebuf         ; empty line?
                beq     input           ; try again

                lda     #<linebuf       ; convert input to int8
                ldy     #>linebuf
                jsr     touint8
                bcc     numok           ; successful -> start processing
                lda     #<errmsg        ; else show error message and repeat
                ldy     #>errmsg
                jsr     $ab1e
                bcs     input

numok:          
                sta     $2
                lda     #<primebuf
                sta     $fb
                lda     #>primebuf
                sta     $fc
                jsr     primesum        ; call function to sum primes
                tax                     ; and ...
                lda     #$0             ; 
                jmp     $bdcd           ; .. print result

; read a line of input from keyboard, terminate it with 0
; expects pointer to input buffer in A/Y, buffer length in X
.proc readline
                dex
                stx     $fb
                sta     $fc
                sty     $fd
                ldy     #$0
                sty     $cc             ; enable cursor blinking
                sty     $fe             ; temporary for loop variable
getkey:         jsr     $f142           ; get character from keyboard
                beq     getkey
                sta     $2              ; save to temporary
                and     #$7f
                cmp     #$20            ; check for control character
                bcs     checkout        ; no -> check buffer size
                cmp     #$d             ; was it enter/return?
                beq     prepout         ; -> normal flow
                cmp     #$14            ; was it backspace/delete?
                bne     getkey          ; if not, get next char
                lda     $fe             ; check current index
                beq     getkey          ; zero -> backspace not possible
                bne     prepout         ; skip checking buffer size for bs
checkout:       lda     $fe             ; buffer index
                cmp     $fb             ; check against buffer size
                beq     getkey          ; if it would overflow, loop again
prepout:        sei                     ; no interrupts
                ldy     $d3             ; get current screen column
                lda     ($d1),y         ; and clear 
                and     #$7f            ;   cursor in
                sta     ($d1),y         ;   current row
output:         lda     $2              ; load character
                jsr     $e716           ;   and output
                ldx     $cf             ; check cursor phase
                beq     store           ; invisible -> to store
                ldy     $d3             ; get current screen column
                lda     ($d1),y         ; and show
                ora     #$80            ;   cursor in
                sta     ($d1),y         ;   current row
                lda     $2              ; load character
store:          cli                     ; enable interrupts
                cmp     #$14            ; was it backspace/delete?
                beq     backspace       ; to backspace handling code
                cmp     #$d             ; was it enter/return?
                beq     done            ; then we're done.
                ldy     $fe             ; load buffer index
                sta     ($fc),y         ; store character in buffer
                iny                     ; advance buffer index
                sty     $fe
                bne     getkey          ; not zero -> ok
done:           lda     #$0             ; terminate string in buffer with zero
                ldy     $fe             ; get buffer index
                sta     ($fc),y         ; store terminator in buffer
                sei                     ; no interrupts
                ldy     $d3             ; get current screen column
                lda     ($d1),y         ; and clear 
                and     #$7f            ;   cursor in
                sta     ($d1),y         ;   current row
                inc     $cc             ; disable cursor blinking
                cli                     ; enable interrupts
                rts                     ; return
backspace:      dec     $fe             ; decrement buffer index
                bcs     getkey          ; and get next key
.endproc

; parse / convert uint8 number using a BCD representation and double-dabble
.proc touint8
                sta     $fb
                sty     $fc
                ldy     #$0
                sty     convbuf
                sty     convbuf+1
                sty     convbuf+2
scanloop:       lda     ($fb),y
                beq     copy
                iny
                cmp     #$20
                beq     scanloop
                cmp     #$30
                bcc     error
                cmp     #$3a
                bcs     error
                bcc     scanloop
error:          sec
                rts
copy:           dey
                bmi     error
                ldx     #$2
copyloop:       lda     ($fb),y
                cmp     #$30
                bcc     copynext
                cmp     #$3a
                bcs     copynext
                sec
                sbc     #$30
                sta     convbuf,x
                dex
copynext:       dey
                bpl     copyloop
                lda     #$0
                sta     $fb
                ldx     #$8
loop:           lsr     convbuf
                lda     convbuf+1
                bcc     skipbit1
                ora     #$10
skipbit1:       lsr     a
                sta     convbuf+1
                lda     convbuf+2
                bcc     skipbit2
                ora     #$10
skipbit2:       lsr     a
                sta     convbuf+2
                ror     $fb
                dex
                beq     done
                lda     convbuf
                cmp     #$8
                bmi     nosub1
                sbc     #$3
                sta     convbuf
nosub1:         lda     convbuf+1
                cmp     #$8
                bmi     nosub2
                sbc     #$3
                sta     convbuf+1
nosub2:         lda     convbuf+2
                cmp     #$8
                bmi     loop
                sbc     #$3
                sta     convbuf+2
                bcs     loop
done:           lda     $fb
                clc
                rts
.endproc

4
ฉันสนุกกับสิ่งนี้มากกว่าภาษาการตีกอล์ฟที่ไม่หยุดหย่อน (ฉันอาจสวมเสื้อยืด MOS 6502 ในปัจจุบัน)
แมตต์ลาเซย์

1
@MattLacey ขอบคุณ :) ฉันแค่ขี้เกียจเกินไปที่จะเรียนรู้ภาษาทั้งหมดเหล่านี้ ... และทำปริศนาบางอย่างในโค้ด 6502 ให้ความรู้สึก "เป็นธรรมชาติ" เพราะการประหยัดพื้นที่จริงๆแล้วเป็นวิธีการเขียนโปรแกรมมาตรฐานบนชิปตัวนั้น :)
Felix Palmen

ฉันต้องการซื้อเสื้อยืด MOS 6502
ติตัส

8

Python 2 , 49 ไบต์

f=lambda n,t=1,p=1:n and p%t*t+f(n-p%t,t+1,p*t*t)

ใช้ทฤษฎีบทของ Wilson (ตามที่ได้รับการแนะนำให้รู้จักกับเว็บไซต์โดย xnor ฉันเชื่อที่นี่ )

ลองออนไลน์!

ฟังก์ชั่นfจะเรียกซ้ำโดยมีอินพุตเริ่มต้นnและหางเมื่อnถึงศูนย์โดยให้ผลลัพธ์เป็นศูนย์ (เนื่องจากตรรกะand) nจะลดลงทุกtครั้งที่หมายเลขทดสอบที่เพิ่มขึ้นทุกครั้งที่โทรไปfเป็นสำคัญ การทดสอบที่สำคัญคือว่าที่เราติดตามตารางในปัจจัยที่(n1)!  1(modn)p


ฉันกำลังปรับหนึ่งในฟังก์ชันผู้ช่วยทั่วไปของ Lynnและบรรลุสิ่งเดียวกัน
Mr. Xcoder

... อาดังนั้นทฤษฎีบทถูกนำไปยังไซต์โดย xnor โพสต์อ้างอิงที่ดีขอบคุณ!
Jonathan Allan



6

Java 8, 89 ไบต์

n->{int r=0,i=2,t,x;for(;n>0;r+=t>1?t+0*n--:0)for(t=i++,x=2;x<t;t=t%x++<1?0:t);return r;}

ลองออนไลน์

คำอธิบาย:

n->{               // Method with integer as both parameter and return-type
  int r=0,         //  Result-sum, starting at 0
      i=2,         //  Prime-integer, starting at 2
      t,x;         //  Temp integers
  for(;n>0         //  Loop as long as `n` is still larger than 0
      ;            //    After every iteration:
       r+=t>1?     //     If `t` is larger than 1 (which means `t` is a prime):
           t       //      Increase `r` by `t`
           +0*n--  //      And decrease `n` by 1
          :        //     Else:
           0)      //      Both `r` and `n` remain the same
    for(t=i++,     //   Set `t` to the current `i`, and increase `i` by 1 afterwards
        x=2;       //   Set `x` to 2
        x<t;       //   Loop as long as `x` is still smaller than `t`
      t=t%x++<1?   //    If `t` is divisible by `x`:
         0         //     Set `t` to 0
        :          //    Else:
         t);       //     `t` remains the same
                   //   If `t` is still the same after this loop, it means it's a prime
  return r;}       //  Return the result-sum



5

Brachylog , 8 7 ไบต์

~lṗᵐ≠≜+

ลองออนไลน์!

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

คำอธิบาย

~l        Create a list of length input
  ṗᵐ      Each element of the list must be prime
    ≠     All elements must be distinct
     ≜    Find values that match those constraints
      +   Sum

~lṗᵐ≠≜+ดูเหมือนว่าจะทำงานสำหรับ 7 ไบต์ (เช่นฉันอยากรู้ว่าทำไมมันให้เอาท์พุท 2 * อินพุต + 1 ถ้าทำงานโดยไม่มีการติดฉลาก)
sundar - Reinstate Monica

2
@sundar ฉันตรวจสอบโดยใช้ดีบักเกอร์และพบสาเหตุ: มันไม่ได้เลือกค่าสำหรับช่วงเวลา แต่ก็ยังรู้ว่าทุกคนต้องอยู่ใน[2,+inf)ชัด ๆ ดังนั้นจึงรู้ว่าผลรวมของ 5 จำนวนเฉพาะ (ถ้าใส่เป็น 5) ต้องมีอย่างน้อย10และบางส่วนรู้ว่าเพราะองค์ประกอบที่แตกต่างกันจะต้องเป็นพวกเขาไม่สามารถทั้งหมด 2 11จึงเป็นอย่างน้อย TL; การนำ DR ไปใช้ของการระบุตำแหน่งแบบไม่ชัดเจนนั้นไม่แข็งแรงพอ
ทำให้เสียชีวิต

นั่นเป็นเรื่องที่น่าสนใจมาก ฉันชอบเหตุผลที่ไม่ใช่เรื่องแปลกของไวยากรณ์หรืออุบัติเหตุแบบสุ่มของการนำไปใช้ แต่เป็นสิ่งที่สมเหตุสมผลตามข้อ จำกัด ขอบคุณสำหรับการตรวจสอบมันออกมา!
sundar - Reinstate Monica



2

เรติน่า 41 ไบต์

K`_
"$+"{`$
$%"_
)/¶(__+)\1+$/+`$
_
^_

_

ลองออนไลน์! ฉันต้องการเพิ่มอีก 1 จนกว่าฉันจะได้พบกับnช่วงเวลาพิเศษ แต่ฉันไม่สามารถหาวิธีการทำใน Retina ได้ดังนั้นฉันจึงหันไปใช้ลูปซ้อนกัน คำอธิบาย:

K`_

เริ่มต้นด้วย 1

"$+"{`

วนnครั้ง

$
$%"_

ทำสำเนาของค่าก่อนหน้าและเพิ่มขึ้น

)/¶(__+)\1+$/+`$
_

ให้เพิ่มขึ้นในขณะที่มันประกอบ (การ)ปิดลูปภายนอก)

^_

1ลบเดิม

_

ผลรวมและแปลงเป็นทศนิยม



2

PHP, 66 ไบต์

ใช้ฟังก์ชั่นเฉพาะของตัวเองอีกครั้ง ...

for(;$k<$argn;$i-1||$s+=$n+!++$k)for($i=++$n;--$i&&$n%$i;);echo$s;

ทำงานเป็นท่อที่มี-nrหรือลองออนไลน์

ชำรุด

for(;$k<$argn;      # while counter < argument
    $i-1||              # 3. if divisor is 1 (e.g. $n is prime)
        $s+=$n              # add $n to sum
        +!++$k              # and increment counter
    )
    for($i=++$n;        # 1. increment $n
        --$i&&$n%$i;);  # 2. find largest divisor of $n smaller than $n:
echo$s;             # print sum

ความยาวเท่ากันหนึ่งตัวแปรลดลง:for(;$argn;$i-1||$s+=$n+!$argn--)for($i=++$n;--$i&&$n%$i;);echo$s;
ติตัส

2

Haskell , 48 ไบต์

sum.(`take`[p|p<-[2..],all((>0).mod p)[2..p-1]])

ลองออนไลน์!

หมายเหตุ: \p-> all((>0).mod p)[2..p-1]ไม่ใช่การตรวจสอบเฉพาะที่ถูกต้องเนื่องจากใช้Trueสำหรับ0,1เช่นกัน แต่เราสามารถแก้ไขได้โดยเริ่มต้นด้วย2ดังนั้นในกรณีนี้ก็เพียงพอแล้ว



2

C, C ++, D: 147 142 ไบต์

int p(int a){if(a<4)return 1;for(int i=2;i<a;++i)if(!(a%i))return 0;return 1;}int f(int n){int c=0,v=1;while(n)if(p(++v)){c+=v;--n;}return c;}

การเพิ่มประสิทธิภาพ 5 ไบต์สำหรับ C และ C ++:

-2 ไบต์ขอบคุณZacharý

#define R return
int p(int a){if(a<4)R 1;for(int i=2;i<a;++i)if(!(a%i))R 0;R 1;}int f(int n){int c=0,v=1;while(n)if(p(++v))c+=v,--n;R c;}

pทดสอบว่าตัวเลขเป็นจำนวนเฉพาะหรือไม่fรวมnตัวเลขแรก

รหัสที่ใช้ทดสอบ:

C / C ++:

for (int i = 0; i < 10; ++i)
    printf("%d => %d\n", i, f(i));

D เพิ่มประสิทธิภาพคำตอบโดยZacharý , 133 131 bytes

D มีระบบเทมเพลตแบบกอล์ฟ

T p(T)(T a){if(a<4)return 1;for(T i=2;i<a;)if(!(a%i++))return 0;return 1;}T f(T)(T n){T c,v=1;while(n)if(p(++v))c+=v,--n;return c;}

1
T p(T)(T a){if(a<4)return 1;for(T i=2;i<a;)if(!(a%i++))return 0;return 1;}T f(T)(T n){T c,v=1;while(n)if(p(++v)){c+=v;--n;}return c;}. นอกจากนี้ C / C ++ / D สามารถเป็นint p(int a){if(a<4)return 1;for(int i=2;i<a;++i)if(!(a%i))return 0;return 1;}int f(int n){int c=0,v=1;while(n)if(p(++v)){c+=v;--n;}return c;}(เช่นเดียวกับการเพิ่มประสิทธิภาพ C / C ++ เพียงแค่ปรับอัลกอริทึม abit)
Zacharý

บางทีคำตอบทั้งหมดที่คุณสามารถใช้เครื่องหมายจุลภาคเพื่อให้{c+=v;--n;}เป็นc+=v,--n;?
Zacharý

นี่เป็นอีกคนหนึ่งสำหรับ D (และอาจจะสำหรับ C / C ++ เป็นอย่างดีถ้าเปลี่ยนกลับไปเป็นints):T p(T)(T a){T r=1,i=2;for(;i<a;)r=a%i++?r:0;return r;}T f(T)(T n){T c,v=1;while(n)if(p(++v))c+=v,--n;return c;}
Zachary

แนะนำa>3&i<aแทนi<aและลบif(a<4)...
ceilingcat

2

Japt -x , 11 ไบต์

;@_j}a°X}hA

ลองออนไลน์!

บันทึกหลายไบต์ด้วยคุณสมบัติภาษาใหม่

คำอธิบาย:

;@      }hA    :Get the first n numbers in the sequence:
     a         : Get the smallest number
      °X       : Which is greater than the previous result
  _j}          : And is prime
               :Implicitly output the sum



1

APL (Dyalog Unicode) , 7 + 9 = 16 ไบต์

+/pco∘⍳

ลองออนไลน์!

9 ไบต์เพิ่มเติมเพื่อนำเข้าpco(และทุก ๆ ) Dfn:⎕CY'dfns'

วิธี:

+/pco∘⍳
        Generate the range from 1 to the argument
        Compose
  pco    P-colon (p:); without a left argument, it generates the first <right_arg> primes.
+/       Sum

คุณไม่ต้องเพิ่มอีกหนึ่งไบต์? import X(ขึ้นบรรทัดใหม่) X.something()ในไพ ธ อนนับด้วย newline
Zacharý



1

JAEL 5 ไบต์

#&kȦ

คำอธิบาย (สร้างขึ้นโดยอัตโนมัติ):

./jael --explain '#&kȦ'
ORIGINAL CODE:  #&kȦ

EXPANDING EXPLANATION:
Ȧ => .a!

EXPANDED CODE:  #&k.a!,

#     ,                 repeat (p1) times:
 &                              push number of iterations of this loop
  k                             push nth prime
   .                            push the value under the tape head
    a                           push p1 + p2
     !                          write p1 to the tape head
       ␄                print machine state

0

Python 2 , 63 59 56 51 ไบต์

f=lambda n:n and prime(n)+f(n-1)
from sympy import*

ลองออนไลน์!


ที่บันทึกไว้:

  • -5 ไบต์ขอบคุณ Jonathan Allan

ไม่มี libs:

Python 2 , 83 ไบต์

n,s=input(),0
x=2
while n:
 if all(x%i for i in range(2,x)):n-=1;s+=x
 x+=1
print s

ลองออนไลน์!


f=lambda n:n and prime(n)+f(n-1)ประหยัดห้า (มันอาจจะเล่นกอล์ฟได้อีกด้วย)
Jonathan Allan


0

CJam , 21 ไบต์

0{{T1+:Tmp!}gT}li*]:+


Explanation:
0{{T1+:Tmp!}gT}li*]:+ Original code

 {            }li*    Repeat n times
  {        }          Block
   T                  Get variable T | stack: T
    1+                Add one | Stack: T+1 
      :T              Store in variable T | Stack: T+1
        mp!           Is T not prime?     | Stack: !(isprime(T))
            g         Do while condition at top of stack is true, pop condition
             T        Push T onto the stack | Stack: Primes so far
0                 ]   Make everything on stack into an array, starting with 0 (to not throw error if n = 0)| Stack: array with 0 and all primes up to n
                   :+ Add everything in array

ลองออนไลน์!


0

F #, 111 ไบต์

let f n=Seq.initInfinite id|>Seq.filter(fun p->p>1&&Seq.exists(fun x->p%x=0){2..p-1}|>not)|>Seq.take n|>Seq.sum

ลองออนไลน์!

Seq.initInfiniteสร้างลำดับยาว ๆ ไม่สิ้นสุดด้วยฟังก์ชันตัวสร้างซึ่งใช้เป็นดัชนีดัชนีของรายการ idในกรณีนี้ฟังก์ชั่นเครื่องกำเนิดไฟฟ้าเป็นเพียงฟังก์ชั่นตัวตน

Seq.filter เลือกเฉพาะตัวเลขที่สร้างโดยลำดับที่ไม่มีที่สิ้นสุด

Seq.take ใช้เวลาก่อน nองค์ประกอบในลำดับนั้น

และในที่สุดก็Seq.sumสรุปพวกเขา



0

MY , 4 ไบต์

⎕ṀΣ↵

ลองออนไลน์!

ยังคงเสียใจที่ไม่มีอินพุต / เอาต์พุตโดยนัยในภาษาขยะนี้น่าจะเป็นสองไบต์

  • = อินพุต
  • = ที่ 1 ... รวมที่สำคัญ
  • Σ = ผลรวม
  • = เอาต์พุต

0

APL (NARS), 27 ตัวอักษร, 54 ไบต์

{⍵=0:0⋄+/{⍵=1:2⋄¯2π⍵-1}¨⍳⍵}

{¯2π⍵} ที่นี่จะคืนค่า n ไพรม์แตกต่างจาก 2 ดังนั้น {⍵ = 1: 2⋄¯2π⍵-1} จะคืนค่า n ไพรม์ 2 ที่นับเข้าด้วยกัน ...

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