Sum of primes between given range

27

3

Write the shortest code for finding the sum of primes between a and b (inclusive).

Input

  1. a and b can be taken from command line or stdin (space seperated)
  2. Assume 1 <= a <= b <= 108

Output Just print the sum with a newline character.

Bonus Points

  1. If the program accepts multiple ranges (print one sum on each line), you get extra points. :)

st0le

Posted 2011-01-28T08:31:33.547

Reputation: 2 002

@st0le So seperated by spaces would be something like 2 3 5 and that would be ranges 2 3 and 3 5? – jamylak – 2012-07-30T08:51:08.877

@jamylak, you can assume the numbers will always appear in pairs. – st0le – 2012-07-30T09:47:17.327

@st0le Ahh ok I will change my solution – jamylak – 2012-07-30T09:48:13.020

The upper limit is too big to allow many interesting solutions (if they have to complete in reasonable time, at least). – hallvabo – 2011-01-28T09:13:34.800

@hallvabo You find inefficient solutions interesting? – Matthew Read – 2011-01-28T09:25:00.323

@hallvabo, That's ok. I don't think anyone minds an ineffcient solution. If other's object, i'll be more than happy to lower the limit – st0le – 2011-01-28T09:38:57.260

Just made and ran a not very optimised or concise version of the program in C#, using 1 to 10^8. Assuming my algorithm's correct, it ran in under 1m30s, and didn't overflow from a long. Seems like a fine upper limit to me! – Nellius – 2011-01-28T12:08:15.037

A quick easy check: sum of primes between 1 and 100 = 1060. – Nellius – 2011-01-28T12:50:26.253

@Matthew Read, You find inefficient solutions interesting? - you didn't specify effectiveness, so either specify, or decrease upper limit, or we will decrease it in our solutions. – Nakilon – 2011-01-28T17:39:34.140

Answers

15

J,41 32 19 characters:

Update

(simple sieve)

g=:+/@(*1&p:)@-.&i.

e.g.

100 g 1
1060
250000x g 48
2623030823

Previous

h=:3 :'+/p:i.(_1 p:>:y)'
f=:-&h<:

eg:

100 f 1
1060

Eelvex

Posted 2011-01-28T08:31:33.547

Reputation: 5 204

11

Mathematica 7 (31 chars in plain text)

If PARI/GP solution allowed, then:

Plus@@Select[Range[a,b],PrimeQ]

Nakilon

Posted 2011-01-28T08:31:33.547

Reputation: 605

128 chars Range[a,b]~Select~PrimeQ//Tr. – chyanog – 2013-09-09T05:12:35.010

What's your point? PARI/GP and Mathematica are fine programming languages. – Eelvex – 2011-01-29T08:28:37.707

@Eelvex, no, they break one of golf rules: using built-in specific highlevel functions. – Nakilon – 2011-01-29T09:19:06.130

I don't think there is such a rule. It's still an open matter when to use highlevel functions. See for ex. this meta question

– Eelvex – 2011-01-29T09:42:10.327

6

C (117 including NL)

main(a,b,s,j){
s=0,scanf("%d%d",&a,&b);
for(a+=a==1;a<=b;a++)
for(s+=a,j=2;j<a;)
s-=a%j++?0:(j=a);
printf("%d",s);
}

Alexandru

Posted 2011-01-28T08:31:33.547

Reputation: 5 485

5

C# (294 characters):

using System;class P{static void Main(){int a=int.Parse(Console.ReadLine()),b=int.Parse(Console.ReadLine());long t=0;for(int i=a;i<=b;i++)if(p(i))t+=i;Console.WriteLine(t);}static bool p(int n){if((n%2<1&&n!=2)||n<2)return 0>1;for(int i=3;i<=Math.Sqrt(n);i+=2)if(n%i==0)return 0>1;return 1>0;}}

Nellius

Posted 2011-01-28T08:31:33.547

Reputation: 568

You can make all your ints long and save a few characters: long a=...,b=...,t=0,i=a;for(;i<=b;i++). This gets it to 288 chars. You can also let p return a long and just return either 0 or n and shorten the loop to t+=p(i). 277 chars, then. – Joey – 2011-06-19T09:28:26.683

5

PARI/GP (44 characters)

sum(x=nextprime(a),precprime(b),x*isprime(x))

Eelvex

Posted 2011-01-28T08:31:33.547

Reputation: 5 204

The downvote was probably for using built-ins. – mbomb007 – 2015-06-26T21:48:34.817

6Shouldn't down voters give a reason for their -1? – Eelvex – 2011-01-29T08:27:39.843

4

C#, 183 characters

using System;class P{static void Main(string[] a){long s=0,i=Math.Max(int.Parse(a[0]),2),j;for(;i<=int.Parse(a[1]);s+=i++)for(j=2;j<i;)if(i%j++==0){s-=i;break;}Console.WriteLine(s);}}

This would be much shorter if it didn't have to check for 1, or if there was a better way to... In a more readable format:

using System;
class P 
{ 
    static void Main(string[] a) 
    { 
        long s = 0,
             i = Math.Max(int.Parse(a[0]),2),
             j;

        for (; i <= int.Parse(a[1]);s+=i++)
            for (j = 2; j < i; )
                if (i % j++ == 0)
                {
                    s -= i;
                    break;
                }

        Console.WriteLine(s); 
    }
}

Nick Larsen

Posted 2011-01-28T08:31:33.547

Reputation: 633

I like how short this is, but I wonder how inefficient it would be when calculating up to 10^8! – Nellius – 2011-01-28T17:24:34.460

True, but efficiency wasn't in the rules! – Nick Larsen – 2011-01-28T18:20:23.613

You know the compiler defaults numerics to 0 right? That'ld save you a couple more chars in there – jcolebrand – 2011-01-29T06:20:04.980

Gives error when compiled without it – Nick Larsen – 2011-01-29T21:21:56.270

...because it is never assigned before it is used (via s -= i; because thats just syntactic sugar for s = s - i; which tries to access s before setting it) – Nick Larsen – 2011-01-29T21:28:22.220

@jcolebrand, that's only true of fields, local variables have to be initialized. – Joey – 2011-06-19T09:46:50.867

You can save a character by using string[]ain the Main signature. Antother two by omitting the call to Math.Max and instead using int i=int.Parse(a[0]);i=2<i?i:2;. The latter can even be pulled into the for header to save another char for the ;. – Joey – 2011-06-19T09:54:15.047

4

BASH Shell

47 Characters

seq 1 100|factor|awk 'NF==2{s+=$2}END{print s}'

Edit: Just realized the sum overflows and is coerced as a double.

52 50 Characters

Here's a bit longer solution, but handles overflows aswell

seq 1 100|factor|awk NF==2{print\$2}|paste -sd+|bc

st0le

Posted 2011-01-28T08:31:33.547

Reputation: 2 002

@Nabb, can't get it to work, tr adds a trailing '+' at the end, fixing it will take more chars. – st0le – 2011-02-06T11:47:44.527

Ah, missed that. Although I think you can still change to awk NF==2{print\$2} to save a byte on the longer solution (we won't accidentally run into brace expansion because there are no commas or ..s). – Nabb – 2011-02-06T19:29:25.750

@Nabb, you're right. Done :) – st0le – 2011-02-07T04:25:54.723

tr is shorter than paste, and you can remove the single quotes (escape the $). – Nabb – 2011-02-04T04:25:11.573

@Nabb, will fix it as soon as i get my hands on a *nix box, or you could do the honours. – st0le – 2011-02-04T04:28:01.747

3

Haskell (80)

c=u[2..];u(p:xs)=p:u[x|x<-xs,x`mod`p>0];s a b=(sum.filter(>=a).takeWhile(<=b))c

s 1 100 == 1060

Ming-Tang

Posted 2011-01-28T08:31:33.547

Reputation: 5 383

4It's hard to find shorter names than c, u, s... The rest is language standard library. – J B – 2011-02-07T10:04:39.193

This is code-golf! Why do you use such long names for your stuff? – FUZxxl – 2011-02-03T16:30:37.117

3

Pari/GP (24 characters)

s=0;forprime(i=a,b,s+=i)

Like some other solutions, this doesn't strictly meet the requirements, as a and b aren't read from stdin or the command line. I thought it was a nice alternative to the other Pari/GP and Mathematica solutions however.

DanaJ

Posted 2011-01-28T08:31:33.547

Reputation: 466

1+1: This is the way I'd actually do it, even without golfing. – Charles – 2015-04-28T14:55:18.123

3

Ruby 1.9, 63 chars

require'prime';p=->a,b{Prime.each(b).select{|x|x>a}.inject(:+)}

Use like this

p[1,100] #=> 1060

Using the Prime class feels like cheating, but since the Mathematica solutions used built-in prime functions...

Michael Kohl

Posted 2011-01-28T08:31:33.547

Reputation: 483

3

Perl, 62 chars

<>=~/\d+/;map$s+=$_*(1x$_)!~/^1$|(^11+)\1+$/,$&..$';print$s,$/

This one uses the prime number regex.

ninjalj

Posted 2011-01-28T08:31:33.547

Reputation: 3 018

3

Normal Task (Python 3): 95 chars

a,b=map(int,input().split())
r=range
print(sum(1%i*all(i%j for j in r(2,i))*i for i in r(a,b+1)))

Bonus Task (Python 3): 119 chars

L=iter(map(int,input().split()))
r=range
for a,b in zip(L,L):print(sum(1%i*all(i%j for j in r(2,i))*i for i in r(a,b+1)))

jamylak

Posted 2011-01-28T08:31:33.547

Reputation: 420

2

R, 57 characters

a=scan();b=a[1]:a[2];sum(b[rowSums(!outer(b,b,`%%`))==2])

plannapus

Posted 2011-01-28T08:31:33.547

Reputation: 8 610

Is specifying n=2 necessary in scan()? If the input is standard, is there a problem with omitting the argument and assuming an extra <enter> is required? – Gaffi – 2013-10-30T19:16:54.287

1No actually you're right I could have done without. It was purely for aesthetic reasons (since i knew my code wasn't the shortest anyway :) ) – plannapus – 2013-10-30T19:26:16.807

Well, +1 from me just the same, as it's definitely not the longest. – Gaffi – 2013-10-30T19:27:30.013

2

Japt, 7 bytes

òV fj x

Try it here.

Erik the Outgolfer

Posted 2011-01-28T08:31:33.547

Reputation: 38 134

Welcome to Japt :) – Shaggy – 2017-10-11T12:00:26.657

@Shaggy I originally tried to find a "prime range" builtin in Japt, but then decided to accept the challenge :p – Erik the Outgolfer – 2017-10-11T12:01:17.040

Given how many challenges there are related to primes, a shortcut for fj<space> could be handy. – Shaggy – 2017-10-11T12:02:35.897

2

Common Lisp: (107 chars)

(flet((p(i)(loop for j from 2 below i never (= (mod i j) 0))))(loop for x from(read)to(read)when(p x)sum x))

only works for starting points >= 1

tobyodavies

Posted 2011-01-28T08:31:33.547

Reputation: 991

2

APL (25 characters)

+/((R≥⎕)^~R∊R∘.×R)/R←1↓⍳⎕

This is a modification of a well-known idiom (see this page for an explanation) for generating a list of primes in APL.

Example:

      +/((R≥⎕)^~R∊R∘.×R)/R←1↓⍳⎕
⎕:
      100
⎕:
      1
1060

Dillon Cower

Posted 2011-01-28T08:31:33.547

Reputation: 2 192

2

Factor -> 98

:: s ( a b -- n )
:: i ( n -- ? )
n 1 - 2 [a,b] [ n swap mod 0 > ] all? ;
a b [a,b] [ i ] filter sum ;

Output:

( scratchpad ) 100 1000 s

--- Data stack:
75067

defhlt

Posted 2011-01-28T08:31:33.547

Reputation: 1 717

1

05AB1E, 5 bytes

ŸDp*O

Try it online!

Ÿ      Push the list [a, ..., b]
 D     Push a duplicate of that list
  p    Replace primes with 1 and everything else with 0
   *   Element-wise multiply the two lists [1*0, 2*1, 3*1, 4*0, ...]
    O  Sum of the final list of primes

Galoubet

Posted 2011-01-28T08:31:33.547

Reputation: 11

1

Python 3.1(153 chars):

from sys import*
p=[]
for i in range(int(argv[1]),int(argv[2])):
 r=1
 for j in range(2,int(argv[2])):
  if i%j==0and i!=j:r=0
 if r:p+=[i]
print(sum(p))

John

Posted 2011-01-28T08:31:33.547

Reputation: 251

You can use input() to be shorter. Also, can you use if i%j<1and instead? – mbomb007 – 2015-06-26T21:51:37.323

>

  • from sys import* 2. r=True -> r=1 (and respectively 0 for False) 3. if i%j==0and i!=j:r=0 4. if r:p+=[i] 5. print(sum(p)) (replaces last 4 lines)
  • < – seequ – 2014-08-07T21:13:46.437

    1

    R (85 characters)

    x=scan(nmax=2);sum(sapply(x[1]:x[2],function(n)if(n==2||all(n %% 2:(n-1)))n else 0))

    Extremely inefficient! I'm pretty sure it takes O(n^2) time. It might give warnings about coercing a double to a logical.

    Deobfuscated:

    x <- scan(nmax=2)
    start <- x[1]
    end <- x[2]
    
    #this function returns n if n is prime, otherwise it returns 0.
    return.prime <- function(n) {
      # if n is 2, n is prime. Otherwise, if, for each number y between 2 and n, n mod y is 0, then n must be prime
      is.prime <- n==2 || all(n%% 2:(n-1))
      if (is.prime)
        n
      else
        0
    } 
    primes <- sapply(start:end, return.prime)
    sum(primes)
    

    raptortech97

    Posted 2011-01-28T08:31:33.547

    Reputation: 111

    1

    In Q (95):

    d:{sum s:{if[2=x;:x];if[1=x;:0];$[0=x mod 2;0;0=min x mod 2+til floor sqrt x;0;x]}each x+til y}
    

    Sample Usage:

    q)d[1;100]
    1060
    

    sinedcm

    Posted 2011-01-28T08:31:33.547

    Reputation: 410

    1

    GolfScript, 27 24 bytes

    ~,>{:x,{)x\%!},,2=},{+}*
    

    This is based off of @w0lf's prime number algorithm.

    phase

    Posted 2011-01-28T08:31:33.547

    Reputation: 2 540

    1

    Perl, 103 chars

    while(<>){($a,$b)=split/ /;for($a..$b){next if$_==1;for$n(2..$_-1){$_=0if$_%$n==0}$t+=$_;}print"$t\n";}
    

    It'll accept multiple space separated lines and give the answer for each :D

    anonymous coward

    Posted 2011-01-28T08:31:33.547

    Reputation: 516

    1

    C# 302

    using System;namespace X{class B{static void Main(){long x=long.Parse(Console.ReadLine()),y=long.Parse(Console.ReadLine()),r=0;for(long i=x;i<=y;i++){if(I(i)){r+=i;}}Console.WriteLine(r);}static bool I(long n){bool b=true;if(n==1){b=false;}for(long i=2;i<n;++i){if(n%i==0){b=false;break;}}return b;}}}
    

    Saumil

    Posted 2011-01-28T08:31:33.547

    Reputation: 11

    1

    Mathematica, 27

    Predefined a and b:

    a~Range~b~Select~PrimeQ//Tr
    

    As a function (also 27):

    Tr[Range@##~Select~PrimeQ]&
    

    Mr.Wizard

    Posted 2011-01-28T08:31:33.547

    Reputation: 2 481

    0

    133 chars, Lua (no is_prime in-built function)

    for i=m,n,1 do
    if i%2~=0 and i%3~=0 and i%5~=0 and i%7~=0 and i%11~=0 then
    s=s+1
    end
    end
    print(s)
    

    Here's an example where I added the line "print(i)" to display all primes found and the sum at the end of them: http://codepad.org/afUvYHnm.

    user8524

    Posted 2011-01-28T08:31:33.547

    Reputation: 1

    “a and b can be taken from command line or stdin” In which of those two ways can the numbers be passed to your code? – manatwork – 2013-07-07T10:41:13.487

    1According to this 13 (anything over it) is not a prime number. – st0le – 2013-07-07T16:53:39.387

    @st0le According to the logic 13 is a "prime" (but e.g. 2 is not) - on the other hand 13*13 = 169 is "prime" again... – Howard – 2013-07-08T11:30:28.627

    0

    PowerShell - 94

    $a,$b=$args[0,1]
    (.{$p=2..$b
    while($p){$p[0];$p=@($p|?{$_%$p[0]})}}|
    ?{$_-gt$a}|
    measure -s).sum
    

    Rynant

    Posted 2011-01-28T08:31:33.547

    Reputation: 2 353

    0

    Casio-Basic, 42 bytes

    sum(seq(piecewise(isPrime(x),x,0),x,a,b
    

    Uses a hybrid/piecewise function that returns the number if it's prime, otherwise return 0. seq runs this over the range a to b, then sum adds it all up.

    39 bytes for the function, 3 bytes to enter a,b as parameters.

    numbermaniac

    Posted 2011-01-28T08:31:33.547

    Reputation: 639

    0

    Jelly, 3 bytes

    æRS
    

    Try it online!

    Erik the Outgolfer

    Posted 2011-01-28T08:31:33.547

    Reputation: 38 134

    0

    Add++, 10 bytes

    L,d@ßrÞP¦+
    

    Try it online!

    How it works

    D,f,@@,		; Define a dyadic function, f
    		; Example arguments:	[2 23]
    	d	; Duplicate;	STACK = [2 23 23]
    	@	; Reverse;	STACK = [23 23 2]
    	ßr	; Range;	STACK = [23 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22]
    	Þ	; Filter on:
    	  P	;  Primality	STACK = [23 2 3 5 7 11 13 17 19]
    	¦+	; Sum;		STACK = [100]
    		; Implicitly return top of stack
    

    caird coinheringaahing

    Posted 2011-01-28T08:31:33.547

    Reputation: 13 702

    0

    F# (141)

    One third of the code is for parsing the input.

    let[|a;b|]=System.Console.ReadLine().Split(' ')
    {int a..int b}|>Seq.filter(fun n->n>1&&Seq.forall((%)n>>(<>)0){2..n-1})|>Seq.sum|>printfn"%A"
    

    Ming-Tang

    Posted 2011-01-28T08:31:33.547

    Reputation: 5 383

    0

    Scala: 260

    object P extends App{
    def c(M:Int)={val p=(false::false::true::List.range(3,M+1).map(_%2!=0)).toArray
    for(i<-(3 to M)
    if p(i))
    {var j=i*i
    while(j<M){p(j)=false
    j+=i}}
    p}
    val l=args.map(_.toInt)
    val p=c(l(1))
    println((l(0)to l(1)).filter(p).map(_.toLong).sum)}
    

    A self-written primes-sieve.

    time scala P 3900000 4000000
    25811704341
    
    real    0m8.288s
    user    0m6.968s
    sys 0m0.456s
    

    user unknown

    Posted 2011-01-28T08:31:33.547

    Reputation: 4 210

    0

    Python, 133

    A little bit of sorcery:

    x,y=map(int,raw_input().split())
    y+=1
    a=range(y)
    print sum(i for i in[[i for a[::i]in[([0]*y)[::i]]][0]for i in a[2:]if a[i]]if i>=x)
    

    JBernardo

    Posted 2011-01-28T08:31:33.547

    Reputation: 1 659

    You can remove y+=1 and instead use range(y+1) and ([0]*-~y)[::i] to save a byte (removing the newline). And using Python 3 will allow you to use input(), as long as you put parentheses after print, therefore removing 4 bytes, but adding 1. Worth it. – mbomb007 – 2015-06-26T21:54:02.313

    -1 (Well I don't have enough rep to downvote yet) This is invalid in Python 2 or 3, you can't expect input to conveniently contain quotation marks for you. Change to raw_input or use python 3 plz – jamylak – 2012-07-30T06:21:59.733

    0

    Perl, 62 with bonus

    use ntheory":all";while(<>){say vecsum(@{primes(split/\s+/)})}
    

    Takes lines with two whitespace separated numbers and prints the sum of primes within the range. Exits when it sees EOF.

    47 for the simple case we assume the input magically arrives in $a and $b like a few other solutions:

    use ntheory":all";forprimes{$s+=$_}$a,$b;say$s
    

    or

    use ntheory":all";say vecsum(@{primes($a,$b)})
    

    With a newer module version that can be 39 characters:

    use ntheory":all";say sum_primes($a,$b)
    

    DanaJ

    Posted 2011-01-28T08:31:33.547

    Reputation: 466

    0

    Python 3: 86 chars

    a,b=map(int,input().split())
    P=k=1
    s=0
    while k<=b:s+=P%k*k*(k>=a);P*=k*k;k+=1
    print(s)
    

    Uses the factorial trick with Wilson's Theorem to check whether k is prime. P%k is 1 if k is prime and 0 otherwise. If it is prime, k is added to the running sum s.

    xnor

    Posted 2011-01-28T08:31:33.547

    Reputation: 115 687

    0

    Python - 194

    File fsoe-sum.py:

    S,E=input()
    L={}
    n=2
    s=0
    while n<=E:
            try:
                    P=L[n];del L[n]
            except:
                    P=[n]
                    if S<=n: s+=n
            for p in P:
                    m=n+p
                    try:
                            if p not in L[m]:L[m].append(p)
                    except:
                            L[m] = [p]
            n+=1
    print s
    

    Filesize is 194 bytes when using tabs to indent and no final newline.

    Not the shortest pythonish solution but do you see the enbedded sieve? ;-)

    Run:

    $ python fsoe-sum.py
    1,1000000
    37550402023
    $ python fsoe-sum.py
    1,2000000
    142913828922
    $ python fsoe-sum.py
    1000001,2000000            
    105363426899
    $ python -c 'print 142913828922-37550402023'
    105363426899
    

    user19214

    Posted 2011-01-28T08:31:33.547

    Reputation:

    0

    Perl, 94

    my$s;map{my($a,$b)=($_,0);for(2..$a-1){$a%$_==0&&$b++}$b or$s+=$a}($ARGV[0]..$ARGV[1]);print$s
    

    This takes input from the command line. It doesn't use regex.

    KSFT

    Posted 2011-01-28T08:31:33.547

    Reputation: 1 527

    0

    Python: 110 chars

    l,h=map(int,raw_input().split())
    print sum(filter(lambda p:p!=1 and all(p%i for i in range(2,p)),range(l,h)))
    

    zxul767

    Posted 2011-01-28T08:31:33.547

    Reputation: 321

    This is not inclusive. – jamylak – 2012-07-30T06:23:18.907

    0

    Julia, 69 bytes

    a,b=int(split(readline()));println(sum(setdiff(primes(b),primes(a))))
    

    This reads a space-delimited pair of integers from STDIN and prints the result to STDOUT. The only thing I've really golfed here is whitespace; otherwise this is probably how I would go about it in a non-golfing context.

    Ungolfed + explanation:

    # Read a line from STDIN, split it into an array on the space, convert
    # the elements to integers, and assign the first element to a and the
    # second to b
    a, b = int(split(readline()))
    
    # Get the primes between a and b inclusive. primes(x) returns the primes
    # <= x, so the set difference of primes(b) and primes(a) will get us only
    # those between a and b
    d = setdiff(primes(b), primes(a))
    
    # Print the sum to STDOUT
    println(sum(d))
    

    Just realized how old this challenge is.

    Alex A.

    Posted 2011-01-28T08:31:33.547

    Reputation: 23 761

    0

    Java 7, 239 237 bytes

    class M{public static void main(String[]a){c(new Long(a[0]),new Long(a[1]));}static void c(long a,long b){long r=0,i=a-1;for(;++i<=b;r+=p(i)?i:0);System.out.print(r);}static boolean p(long n){int i=2;while(i<n)n=n%i++<1?0:n;return n>1;}}
    

    Ungolfed & test case:

    class M{
      static void c(long a,long b){
        long r = 0,
             i = a-1;
        for(; ++i <= b; r += p(i) ? i : 0);
        System.out.print(r);
      }
    
      public static void main(String[] a){
        c(new Long(a[0]), new Long(a[1]));
      }
    
      static boolean p(long n){
        int i = 2;
        while(i < n){
          n = n % i++ < 1 ? 0 : n;
        }
        return n > 1;
      }
    }
    

    Usage: java -jar M.jar 5 17
    Output: 53

    Kevin Cruijssen

    Posted 2011-01-28T08:31:33.547

    Reputation: 67 575

    0

    Haskell, 47 44 bytes

    a!b=sum[x|x<-[a..b],all((<)0.mod x)[2..x-1]]
    

    SEJPM helped me save 3 bytes!

    BlackCap

    Posted 2011-01-28T08:31:33.547

    Reputation: 3 576

    44 bytes: a!b=sum[x|x<-[a..b],all((<)0.mod x)[2..x-1]] (using all instead of and) – SEJPM – 2017-10-11T12:00:45.040

    Fails for 1!3 (gives 6 instead of 5).. – ბიმო – 2018-08-09T00:16:23.487

    0

    Ruby, 60 bytes

    require'prime';p=->a,b{eval Prime.each(b).reject{|x|x<a}*?+}
    

    Usage

    puts p[*gets.split.map(&:to_i)]
    

    Inputs & Outputs

    2 10     #=> 10
    3 10     #=> 8
    100 1000 #=> 75067
    

    cia_rana

    Posted 2011-01-28T08:31:33.547

    Reputation: 441