17
\$\begingroup\$

I'm surprised that this challenge isn't already here, as it's so obvious. (Or I'm surprised I couldn't find it and anybody will mark it as a duplicate.)

Task

Given a non-negative integer \$n\$, calculate the sum of the first \$n\$ primes and output it.

Example #1

For \$n = 5\$, the first five primes are:

  • 2
  • 3
  • 5
  • 7
  • 11

The sum of these numbers is \$2 + 3 + 5 + 7 + 11 = 28\$, so the program has to output \$28\$.

Example #2

For \$n = 0\$, the "first zero" primes are none. And the sum of no numbers is - of course - \$0\$.

Rules

  • You may use built-ins, e.g., to check if a number is prime.
  • This is , so the lowest number of bytes in each language wins!
\$\endgroup\$
10

39 Answers 39

1
2
0
\$\begingroup\$

Python 2, 63 59 56 51 bytes

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

Try it online!


Saved:

  • -5 bytes, thanks to Jonathan Allan

Without libs:

Python 2, 83 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ f=lambda n:n and prime(n)+f(n-1) saves five (it might be golfable further too) \$\endgroup\$ Aug 7, 2018 at 7:24
0
\$\begingroup\$

Pyke, 4 bytes

~p>s

Try it here!

~p   -   prime_numbers
  >  -  first_n(^, input)
   s - sum(^)
\$\endgroup\$
0
\$\begingroup\$

CJam, 21 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

F#, 111 bytes

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

Try it online!

Seq.initInfinite creates an infinitely-long sequence with a generator function which takes, as a parameter, the item index. In this case the generator function is just the identity function id.

Seq.filter selects only the numbers created by the infinite sequence that are prime.

Seq.take takes the first n elements in that sequence.

And finally, Seq.sum sums them up.

\$\endgroup\$
0
\$\begingroup\$

cQuents, 3 bytes

;pz

Try it online!

Explanation

;    sum of first n terms for input n
 pz  each term is the next prime after the previous term
\$\endgroup\$
1
  • \$\begingroup\$ Note current version uses Z instead of z \$\endgroup\$
    – Stephen
    Feb 1, 2019 at 4:50
0
\$\begingroup\$

MY, 4 bytes

⎕ṀΣ↵

Try it online!

Still regretting no implicit input/output in this garbage language, would've been two bytes otherwise.

  • = input
  • = 1st ... nth prime inclusive
  • Σ = sum
  • = output
\$\endgroup\$
0
\$\begingroup\$

APL(NARS), 27 chars, 54 bytes

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

{¯2π⍵} here would return the n prime different from 2. So {⍵=1:2⋄¯2π⍵-1} would return the n prime 2 in count in it...

\$\endgroup\$
0
\$\begingroup\$

Pyth, 5 bytes

s.fP_

Try it online here.

s.fP_ZQ   Implicit: Q=eval(input())
          Trailing ZQ inferred
 .f   Q   Starting at Z=1, return the first Q numbers where...
   P_Z    ... Z is prime
s         Sum the resulting list, implicit print
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 S, 2 bytes

Æp

Try it online!

Built-in for "first n primes". S flag sums the list.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.