38
\$\begingroup\$

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ドル \le a \le b \le 10^8\$

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. :)
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
asked Jan 28, 2011 at 8:31
\$\endgroup\$
9
  • \$\begingroup\$ The upper limit is too big to allow many interesting solutions (if they have to complete in reasonable time, at least). \$\endgroup\$ Commented Jan 28, 2011 at 9:13
  • 2
    \$\begingroup\$ @hallvabo You find inefficient solutions interesting? \$\endgroup\$ Commented Jan 28, 2011 at 9:25
  • \$\begingroup\$ @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 \$\endgroup\$ Commented Jan 28, 2011 at 9:38
  • \$\begingroup\$ 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! \$\endgroup\$ Commented Jan 28, 2011 at 12:08
  • \$\begingroup\$ A quick easy check: sum of primes between 1 and 100 = 1060. \$\endgroup\$ Commented Jan 28, 2011 at 12:50

61 Answers 61

1
2 3
15
\$\begingroup\$

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
answered Jan 28, 2011 at 11:19
\$\endgroup\$
11
\$\begingroup\$

Mathematica 7 (31 chars in plain text)

If PARI/GP solution allowed, then:

Plus@@Select[Range[a,b],PrimeQ]
answered Jan 28, 2011 at 17:50
\$\endgroup\$
4
  • \$\begingroup\$ What's your point? PARI/GP and Mathematica are fine programming languages. \$\endgroup\$ Commented Jan 29, 2011 at 8:28
  • \$\begingroup\$ @Eelvex, no, they break one of golf rules: using built-in specific highlevel functions. \$\endgroup\$ Commented Jan 29, 2011 at 9:19
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Jan 29, 2011 at 9:42
  • 1
    \$\begingroup\$ 28 chars Range[a,b]~Select~PrimeQ//Tr. \$\endgroup\$ Commented Sep 9, 2013 at 5:12
6
\$\begingroup\$

C, 117 bytes

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);
}
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Jan 28, 2011 at 16:49
\$\endgroup\$
6
\$\begingroup\$

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;}}
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Jan 28, 2011 at 12:24
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 19, 2011 at 9:28
5
\$\begingroup\$

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); 
 }
}
answered Jan 28, 2011 at 15:52
\$\endgroup\$
7
  • \$\begingroup\$ I like how short this is, but I wonder how inefficient it would be when calculating up to 10^8! \$\endgroup\$ Commented Jan 28, 2011 at 17:24
  • \$\begingroup\$ True, but efficiency wasn't in the rules! \$\endgroup\$ Commented Jan 28, 2011 at 18:20
  • \$\begingroup\$ You know the compiler defaults numerics to 0 right? That'ld save you a couple more chars in there \$\endgroup\$ Commented Jan 29, 2011 at 6:20
  • \$\begingroup\$ Gives error when compiled without it \$\endgroup\$ Commented Jan 29, 2011 at 21:21
  • \$\begingroup\$ ...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) \$\endgroup\$ Commented Jan 29, 2011 at 21:28
4
\$\begingroup\$

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

answered Jan 30, 2011 at 5:36
\$\endgroup\$
2
  • \$\begingroup\$ This is code-golf! Why do you use such long names for your stuff? \$\endgroup\$ Commented Feb 3, 2011 at 16:30
  • 4
    \$\begingroup\$ It's hard to find shorter names than c, u, s... The rest is language standard library. \$\endgroup\$ Commented Feb 7, 2011 at 10:04
4
\$\begingroup\$

Perl, 62 chars

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

This one uses the prime number regex.

answered Feb 6, 2011 at 11:50
\$\endgroup\$
4
\$\begingroup\$

PARI/GP, 44 characters

sum(x=nextprime(a),precprime(b),x*isprime(x))
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Jan 28, 2011 at 13:24
\$\endgroup\$
2
  • 6
    \$\begingroup\$ Shouldn't down voters give a reason for their -1? \$\endgroup\$ Commented Jan 29, 2011 at 8:27
  • \$\begingroup\$ The downvote was probably for using built-ins. \$\endgroup\$ Commented Jun 26, 2015 at 21:48
4
\$\begingroup\$

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
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Jan 29, 2011 at 6:42
\$\endgroup\$
6
  • \$\begingroup\$ tr is shorter than paste, and you can remove the single quotes (escape the $). \$\endgroup\$ Commented Feb 4, 2011 at 4:25
  • \$\begingroup\$ @Nabb, will fix it as soon as i get my hands on a *nix box, or you could do the honours. \$\endgroup\$ Commented Feb 4, 2011 at 4:28
  • \$\begingroup\$ @Nabb, can't get it to work, tr adds a trailing '+' at the end, fixing it will take more chars. \$\endgroup\$ Commented Feb 6, 2011 at 11:47
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Feb 6, 2011 at 19:29
  • \$\begingroup\$ @Nabb, you're right. Done :) \$\endgroup\$ Commented Feb 7, 2011 at 4:25
3
\$\begingroup\$

APL (25 characters)

+/((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←1↓⍳⎕
⎕:
 100
⎕:
 1
1060
answered Mar 6, 2012 at 1:47
\$\endgroup\$
3
\$\begingroup\$

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)))
answered Jul 30, 2012 at 6:47
\$\endgroup\$
3
\$\begingroup\$

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.

answered Aug 4, 2014 at 16:02
\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1: This is the way I'd actually do it, even without golfing. \$\endgroup\$ Commented Apr 28, 2015 at 14:55
3
\$\begingroup\$

Japt, 7 bytes

òV fj x

Try it here.

answered Oct 11, 2017 at 11:57
\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Japt :) \$\endgroup\$ Commented Oct 11, 2017 at 12:00
  • \$\begingroup\$ @Shaggy I originally tried to find a "prime range" builtin in Japt, but then decided to accept the challenge :p \$\endgroup\$ Commented Oct 11, 2017 at 12:01
  • 1
    \$\begingroup\$ Given how many challenges there are related to primes, a shortcut for fj<space> could be handy. \$\endgroup\$ Commented Oct 11, 2017 at 12:02
3
\$\begingroup\$

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
answered Oct 11, 2017 at 12:41
\$\endgroup\$
2
  • \$\begingroup\$ ✓ I didn't think of using p* \$\endgroup\$ Commented Jan 3, 2021 at 17:00
  • \$\begingroup\$ More boring solution with filter: Ÿʒp}O \$\endgroup\$ Commented Apr 17, 2021 at 18:09
3
\$\begingroup\$

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...

caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Feb 5, 2011 at 11:36
\$\endgroup\$
3
\$\begingroup\$

R, 57 characters

a=scan();b=a[1]:a[2];sum(b[rowSums(!outer(b,b,`%%`))==2])
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Oct 29, 2013 at 10:26
\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Oct 30, 2013 at 19:16
  • 1
    \$\begingroup\$ No 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 :) ) \$\endgroup\$ Commented Oct 30, 2013 at 19:26
  • \$\begingroup\$ Well, +1 from me just the same, as it's definitely not the longest. \$\endgroup\$ Commented Oct 30, 2013 at 19:27
3
\$\begingroup\$

C (clang), (削除) 164 (削除ここまで) (削除) 138 (削除ここまで) 122 bytes

i,x,y,z;main(f){for(scanf("%d %d",&x,&y);x<y;z+=x++*!f)if(f=0,x<2)++x;else for(i=2;x/2/i&&!(f|=x%i++<1););printf("%i",z);}

Try it online!

Original code from Programiz. Just modified the code to reduce bytes. Yes, I'm terrible at this.

Code changed thanks to Pedro Maimere, byte count didn't change. Thanks to ceilingcat for golfing 26 bytes, and another 16 bytes.

\$\endgroup\$
1
  • \$\begingroup\$ As is, your code is not adding y, if prime. You have to use while(x<=y) instead, adding one byte. On the other side, you can omit one space here: "%d %d".Try it online! \$\endgroup\$ Commented Jun 2, 2021 at 12:52
3
\$\begingroup\$

Regex 🐇 (ECMAScriptRME / Perl / PCRE), 32 bytes

^(x*),x*(?=1円)(?!(xx+)2円+$)x\Bx*

Takes its input in unary, as two strings of x characters whose lengths represent the numbers, joined by a , delimiter, specifying an inclusive range. Returns its output as the number of ways the regex can match. (The rabbit emoji indicates this output method.)

Try it online! / Attempt This Online! - Perl
Try it online! - PCRE1
Try it online! / Attempt This Online! - PCRE2
Try it on replit.com! - RegexMathEngine, in ECMAScript mode

^ # Anchor to start
(x*) # 1円 = lower end of range
, # Skip over delimiter
x* # tail += {any number, including zero}
(?=1円) # Assert that tail ≥ 1円
(?!(xx+)2円+$) # Assert that tail is not composite (i.e., tail is prime or is < 2)
x\Bx* # if tail ≥ 2, add tail to the number of possible matches; this is
 # equivalent to "(?=xx)x+"

Regex (.NET), 32 bytes

x(x*),((?=(xx+)3円+$|(x)+)x\B)*1円

Returns its output as the capture count of group 4円.

Try it online!

 # No need to anchor, as this regex will always match
x(x*) # 1円 = {lower end of range} - 1
 # This takes advantage of the input specification allowing
 # us to assume it to be ≥ 1.
, # Skip over delimiter; tail = upper end of range
(
 (?= # Lookahead - is atomic (the first match found is final)
 (xx+)3円+$ # Match if tail is composite (i.e., isn't prime and is ≥ 2)
 | # or...
 (x)+ # Push {tail} captures onto the Group 4 stack, i.e. add
 # {tail} to the return value.
 )
 x # tail -= 1
 \B # Assert tail > 0
)* # Iterate the above as many times as possible (can be zero)
1円 # Assert tail ≥ 1円
answered Mar 15, 2023 at 18:30
\$\endgroup\$
3
\$\begingroup\$

Pip, (削除) 19 (削除ここまで) 17 bytes

$+Y1=0N_%,_FIa,円b

Attempt This Online!

-2 thanks to @DLosc

Uses DLosc's answer to the prime checker challenge

Explanation

$+Y1=0N_%,_FIa,円b ; Input on command line
 ,円 ; Inclusive range between
 a b ; The two inputs
 FI ; Filtered by
 1=0N_%,_ ; Is prime (see linked answer)
$+ ; Sum the resulting list

Old:

$+Y{1=0Na%,a}FIa,Ub ; Input on command line
 , ; Range between
 a ; The first input
 Ub ; To the second input incremented
 FI ; Filtered by
 {1=0Na%,a} ; Is prime (see linked answer)
$+Y  ; Sum the resulting list
answered Mar 15, 2023 at 11:25
\$\endgroup\$
4
  • \$\begingroup\$ 17 bytes \$\endgroup\$ Commented May 1, 2023 at 19:21
  • \$\begingroup\$ @DLosc thanks, updated \$\endgroup\$ Commented May 1, 2023 at 19:59
  • \$\begingroup\$ "see linked answer" yet there's no linked answer :( \$\endgroup\$ Commented May 2, 2023 at 1:26
  • \$\begingroup\$ @AidenChow sorry, I removed the link \$\endgroup\$ Commented May 2, 2023 at 6:21
2
\$\begingroup\$

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
answered Mar 7, 2012 at 11:04
\$\endgroup\$
2
\$\begingroup\$

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
answered Jul 23, 2012 at 9:47
\$\endgroup\$
2
\$\begingroup\$

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))
answered Jan 29, 2011 at 3:27
\$\endgroup\$
2
  • \$\begingroup\$ 1. 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) \$\endgroup\$ Commented Aug 7, 2014 at 21:13
  • \$\begingroup\$ You can use input() to be shorter. Also, can you use if i%j<1and instead? \$\endgroup\$ Commented Jun 26, 2015 at 21:51
2
\$\begingroup\$

Jelly, 3 bytes

æRS

Try it online!

answered Oct 11, 2017 at 11:52
\$\endgroup\$
2
\$\begingroup\$

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 \$\ge 1\$

caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Jan 28, 2011 at 16:59
\$\endgroup\$
2
\$\begingroup\$

C#, 302 bytes

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;}}}
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Jul 28, 2012 at 3:37
\$\endgroup\$
2
\$\begingroup\$

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)
caird coinheringaahing
50.9k11 gold badges133 silver badges364 bronze badges
answered Aug 7, 2014 at 17:56
\$\endgroup\$
2
+50
\$\begingroup\$

Factor + math.primes math.unicode, (削除) 32 (削除ここまで) 23 bytes

[ primes-between Σ . ]

-9 bytes thanks to chunes!

Try it online!

answered Mar 12, 2021 at 9:35
\$\endgroup\$
3
  • 1
    \$\begingroup\$ prime? and Σ are not available by default, so you need to change the header to "Factor + math.primes math.unicode" like this. \$\endgroup\$ Commented Mar 14, 2021 at 23:27
  • \$\begingroup\$ Thank you, @Bubbler I changed the header of my answers :) \$\endgroup\$ Commented Mar 14, 2021 at 23:59
  • 1
    \$\begingroup\$ primes-between is shorter than [a,b] [ prime? ] filter. \$\endgroup\$ Commented Apr 17, 2021 at 16:09
2
\$\begingroup\$

Java 8, (削除) 239 (削除ここまで) (削除) 237 (削除ここまで) (削除) 74 (削除ここまで) 73 bytes

a->b->{long r=0,t;for(;a<=b;r-=b--==t?~b:0)for(t=1;b%++t%b>0;);return r;}

-1 byte thanks to @ceilingcat

Try it online.

Explanation:

a->b->{ // Method with two Long parameters & Long return-type
 long r=0, // Result-sum, starting at 0
 t; // Temp-long
 for(;a<=b // Loop as long as `a` is still smaller than or equal to `b`:
 ; // After every iteration:
 r-= // Decrease the result-sum by:
 b--==t? // If `b` is equal to `t` (which means `b` is a prime):
 // (and decrease `b` by 1 afterwards with `b--`)
 ~b // Decrease the result-sum by `-b-1`
 // (aka Increase the result-sum by `b+1`,
 // where `+1` is to account for the earlier `b--`)
 : // Else:
 0) // Leave `r` the same by decreasing with 0
 for(t=1; // (Re)set `t` to 1
 b%++t%b>0;); // Increase `t` by 1 first with `++t` every iteration,
 // and loop as long as `b` is NOT divisible by `t`,
 // and `b` is NOT 1 (with the second `%b`)
 return r;} // After the nested loop, return the result-sum
answered Sep 12, 2016 at 12:11
\$\endgroup\$
0
2
\$\begingroup\$

Desmos, (削除) 124 (削除ここまで) 50 bytes

f(a,b)=∑_{n=a}^bnsgn((n-1)∏_{k=3}^nmod(n,k-1))

Try it on Desmos!

(削除) I had to waste 23 bytes just for the edge case of 0 and 1(-\left\{n<2:1,0\right\}). I feel like this could definitely be golfed further, but I'm not that skilled at Desmos. I especially think that the edge cases 0 and 1 could be handled a lot better than what I'm doing currently. (削除ここまで)

To think I've come so far... just a mere 2.5 years ago I was still a complete noob at Desmos golfing. Now, coming back to this answer 2.5 years later, there are so many golfs that I can spot. Shifting the bounds to remove the curly brackets, better ways to deal with the n=1 edge case, and some other tiny golfs, resulted in an over 50% reduction in my original code. Truly amazing stuff.

Explanation

I will explain each sub-expression a little out of order from the actual code, but I feel that this is the best way to understand what the code is doing.

∑_{n=a}^b: Summation from n=a to b. This will iterate through all integers between a and b inclusive.

∏_{k=3}^nmod(n,k-1): This takes the product of k=3 to n of mod(n,k-1)which essentially tests all the possible divisors between k=2 and n-1, and checks if n is divisible by k with mod(n,k) (it is mod(n,k-1) in the actual product, which will be explained further below). If n is composite then at least one value of k will make the mod expression equal to 0, making the entire product equal to 0. Otherwise, if n is a prime, then the product would be a positive integer. The reason why the product goes from k=3 to n instead of the more intuitive k=2 to n-1 is because if the upper bound is represented by more than one character, then curly brackets are required to group the characters together. In other words, a product from k=2 to n-1 would be ∏_{k=2}^{n-1}, which is four more bytes than ∏_{k=3}^n. But because we have shifted the bounds of the product up by one, we will need to subtract one from all instances of k in order to compensate for the shift. Hence, we have k-1 instead of k in the mod expression. This adds two bytes to our total code length, resulting in a net -2 bytes from doing this bound shifting. Now, because the bounds start from k=3, what happens if n=1 or 2? When the upper bound is lower than the lower bound, then the product will default to a value of 1. This is an issue for the n=1 case, which will result in the product being a positive integer (specifically 1), but that is undesirable because a positive integer implies that n=1 is a prime when in reality it isn't a prime. But there is a way to easily fix this...

(n-1): This expression is solely to deal with the n=1 case. When n=1, n-1 is 0 which causes the entire product to equal 0. For any other positive value, n-1 would simply be a positive integer and not affect the overall sign of the product. Note that this fix only works for testing primality for positive integers, but this is okay for our code because the problem specifies that a and b can be assumed to be at least 1. To deal with the n=0 case as well, something like 0^{0^{n-1}} would probably work.

nsgn( ... ): This takes n and multiplies it by the sign (sgn in the code) of the entire product expression. Remember that if the inside expression is a positive integer, then it is a prime; otherwise, it is composite. By taking the sign of the resulting integer, we will get 1 if n is prime and 0 if n is composite. Multiplying that by n will give n and 0 for the respective possibilities.

Now considering that we are taking a summation from n=a to b, this will essentially add n to the total sum if n is a prime, but it won't contribute to the sum if n is a composite. In other words, it takes the sum of all integers between n=a and b which are prime. With that, we are done.

answered Oct 15, 2020 at 7:31
\$\endgroup\$
2
\$\begingroup\$

Thunno 2 S, 3 bytes

IœP

Explanation

IœP # Implicit input
I # Inclusive range, [a..b]
 œ # Filtered by:
 P # Primality
 # Implicit output of sum (S flag)

Screenshot

Screenshot

answered May 2, 2023 at 8:41
\$\endgroup\$
1
2 3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.