Skip to content

Instantly share code, notes, and snippets.

@ClearNB
Created January 4, 2019 03:20
Show Gist options
  • Save ClearNB/2ab4e9b734aaac25fa1d402367d273be to your computer and use it in GitHub Desktop.
Save ClearNB/2ab4e9b734aaac25fa1d402367d273be to your computer and use it in GitHub Desktop.
Summation of primes (Java 8 ver) Sample by ClearNB
import java.util.*;
public class Solution {
public static void main(String[] args) {
//このプログラムは、事前計算した後に、入力値で表示させる機能を採用しています。
boolean arr[] = new boolean[1000000 + 1];
for(int i = 2; i <= 1000000; i++){
arr[i] = true;
}
for(int j = 2; j * j <= 1000000; j++) {
if(arr[j] == true) {
for(int k = j * 2; k <= 1000000; k += j){
arr[k] = false;
}
}
}
long arr2[] = new long[1000000 + 1];
long sum2 = 0;
for(int m = 2; m <= 1000000; m++){
if(arr[m] == true){
sum2 += m;
}
arr2[m] = sum2;
}
try (Scanner in = new Scanner(System.in)) {
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
int n = in.nextInt();
System.out.println(arr2[n]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment