View Code of Problem 27

#include <iostream>
#include <math.h>
using namespace std;

bool isprime(int n){
  if(n<=1)
    return false;
  int bound = (int)sqrt(n)+1;
  for(int j=2;j<bound;j++){
    if(n%j==0)
      return false;
  }
  return true;
}

int main(){
  int a,b,sum;
  while(scanf("%d%d",&a,&b)!=EOF){
    sum = 0;
    if(a>b){
      int tmp = a;
      a = b;
      b = tmp;
    }
    for(int j=a+1;j<b;j++){
      if(isprime(j))
        sum+=j;
    }
    printf("%d\n",sum);
  }
  return 0;
}

Double click to view unformatted code.


Back to problem 27