This is the 4th day of Advent Calendar 2018.
--Give a positive integer N as input and start with 4 composite number Print the totals from the 1st to the Nth of the sequence --N is up to 100
Composite.java
class Composite{
public static void main(String[] args){
int in = Integer.parseInt(args[0]);
int num = 4;
int sum = 0;
int composites = 0;
while(composites < in){
if(!Composite.isPrime(num)){
composites++;
sum += num;
}
num++;
}
System.out.println(sum);
}
public static boolean isPrime(int n){
if(n < 2)return false;
if(n == 2)return true;
if(n % 2 == 0)return false;
for(int i=3; i*i<=n; i+=2){
if(n % i == 0)return false;
}
return true;
}
}
$ javac Composite.java
$ java Composite 2
10
$ java Composite 4
27
$ java Composite 10
112
$ java Composite 100
7059
This problem is an extra edition of Zunda Problem
Recommended Posts