素数求和
初学Java,做一些编程小练习。
计算第n个素数到第m个素数(0<n<=m)之间所有素数的和,包括第n个素数和第m个素数:
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 int n = in.nextInt(); 8 int m = in.nextInt(); 9 int count = 0; 10 int number = 2; 11 int result = 0; 12 while(count < m) { 13 boolean isprime = true; 14 for(int i=2; i<number; i++) { 15 if(number%i == 0) { 16 isprime = false; 17 } 18 } 19 if(isprime) { 20 count += 1; 21 if(count >= n) { 22 result += number; 23 } 24 } 25 number += 1; 26 } 27 System.out.println(result); 28 } 29 }