A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.
A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.
You are given two non-empty arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.
Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.
For example, consider an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20The number of semiprimes within each of these ranges is as follows:
- (1, 26) is 10,
- (4, 10) is 4,
- (16, 20) is 0.
Write a function:
class Solution { public int[] solution(int N, int[] P, int[] Q); }
that, given an integer N and two non-empty arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.
For example, given an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20the function should return the values [10, 4, 0], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- M is an integer within the range [1..30,000];
- each element of arrays P and Q is an integer within the range [1..N];
- P[i] ≤ Q[i].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//2이상 = 노소수
System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
//갯수구하기
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a < N; a++) {
if(primeArray[a] == -1){
count++;
}
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k] - 1];
System.out.println("index = " + k + " isPrime = " + result[k]);
}
return result;
}
}
0 index = 0 isPrime = 0 index = 1 isPrime = 4 index = 2 isPrime = 0
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//2이상 = 노소수
System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
//갯수구하기
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a < N; a++) {
if(primeArray[a] == -1){
count++;
}
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k] - 1];
// System.out.println("index = " + k + " isPrime = " + result[k]);
}
return result;
}
}
0
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//2이상 = 노소수
// System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
//갯수구하기
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a < N; a++) {
if(primeArray[a] == -1){
count++;
}
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k] - 1];
// System.out.println("index = " + k + " isPrime = " + result[k]);
}
return result;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//2이상 = 노소수
// System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
//갯수구하기
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a < N; a++) {
if(primeArray[a] == -1){
count++;
}
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k] - 1];
// System.out.println("index = " + k + " isPrime = " + result[k]);
}
return result;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//2이상 = 노소수
// System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
//갯수구하기
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a < N; a++) {
if(primeArray[a] == -1){
count++;
}
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k] - 1];
// System.out.println("index = " + k + " isPrime = " + result[k]);
}
return result;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
//false로 초기화
//소수 구하는 법
boolean[] isNotPrime = new boolean[N + 1];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//1 = 노소수
// System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a <= N; a++) {
if(primeArray[a] == -1){
count++;
}
// System.out.println("index = " + a + " count = " + count);
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k]-1 ];
System.out.println("index = " + k + " isPrime = " + result[k]);
// System.out.println("Q[k] = " + Q[k] + " P[k] = " + P[k]);
// System.out.println("addedList[Q[k]] = " + addedList[Q[k]] + " addedList[P[k] = " + addedList[P[k]]);
}
return result;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
//false로 초기화
//소수 구하는 법
boolean[] isNotPrime = new boolean[N + 1];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//1 = 노소수
// System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a <= N; a++) {
if(primeArray[a] == -1){
count++;
}
// System.out.println("index = " + a + " count = " + count);
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k]-1 ];
System.out.println("index = " + k + " isPrime = " + result[k]);
// System.out.println("Q[k] = " + Q[k] + " P[k] = " + P[k]);
// System.out.println("addedList[Q[k]] = " + addedList[Q[k]] + " addedList[P[k] = " + addedList[P[k]]);
}
return result;
}
}
index = 0 isPrime = 10 index = 1 isPrime = 4 index = 2 isPrime = 0
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
//false로 초기화
//소수 구하는 법
boolean[] isNotPrime = new boolean[N + 1];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//1 = 노소수
// System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a <= N; a++) {
if(primeArray[a] == -1){
count++;
}
// System.out.println("index = " + a + " count = " + count);
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k]-1 ];
// System.out.println("index = " + k + " isPrime = " + result[k]);
// System.out.println("Q[k] = " + Q[k] + " P[k] = " + P[k]);
// System.out.println("addedList[Q[k]] = " + addedList[Q[k]] + " addedList[P[k] = " + addedList[P[k]]);
}
return result;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
//false로 초기화
//소수 구하는 법
boolean[] isNotPrime = new boolean[N + 1];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//1 = 노소수
// System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a <= N; a++) {
if(primeArray[a] == -1){
count++;
}
// System.out.println("index = " + a + " count = " + count);
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k]-1 ];
// System.out.println("index = " + k + " isPrime = " + result[k]);
// System.out.println("Q[k] = " + Q[k] + " P[k] = " + P[k]);
// System.out.println("addedList[Q[k]] = " + addedList[Q[k]] + " addedList[P[k] = " + addedList[P[k]]);
}
return result;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
int[] result = new int[P.length];
//false로 초기화
//소수 구하는 법
boolean[] isNotPrime = new boolean[N + 1];
// 0으로 초기화
int[] primeArray = new int[N + 1];
int[] addedList = new int[N + 1];
//0 = 소수
//1 = 노소수
// System.out.println(primeArray[3]);
primeArray[0] = 1;
primeArray[1] = 1;
int i = 2;
//소수 구하기
while (i * i <= N) {
// System.out.println("i : " + i);
if (primeArray[i] == 0) {
int k = i * i;
while (k <= N) {
if (primeArray[k] == 0) {
primeArray[k] = i;
}
k += i;
}
// System.out.println(i);
}
i++;
}
//1 ~ 100
// 4 / 2 == prime ==0
//
// 나누는 수가 소수이고 나눈수도 소수일경우
i = 2;
while (i * i <= N) {
if (primeArray[i] == 0) {
int k = i * i;
//primeArray[i] 는 무조건 소수일꺼고
// 소수들은 subPrime이 될 수 없음
while (k <= N) {
if (primeArray[k / i] == 0) {
primeArray[k] = -1;
}
k += i;
}
}
i++;
}
// System.out.println("index = " + k + " isPrime = " + primeArray[k]);
// for (int a = 0; a < P.length; a++) {
// int count = 0;
// for (int k = P[a]; k <= Q[a]; k++) {
// if(primeArray[k] == -1){
// count ++;
// }
// }
// result[a] = count;
// }
int count = 0;
for (int a = 2; a <= N; a++) {
if(primeArray[a] == -1){
count++;
}
// System.out.println("index = " + a + " count = " + count);
addedList[a] = count;
}
for(int k = 0; k < result.length; k++) {
result[k] = addedList[Q[k]] - addedList[P[k]-1 ];
// System.out.println("index = " + k + " isPrime = " + result[k]);
// System.out.println("Q[k] = " + Q[k] + " P[k] = " + P[k]);
// System.out.println("addedList[Q[k]] = " + addedList[Q[k]] + " addedList[P[k] = " + addedList[P[k]]);
}
return result;
}
}
The solution obtained perfect score.