Japanese Advent Calendar Java Edition of Synthetic Sequences

This is the 4th day of Advent Calendar 2018.

rule

--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

Program (Java)

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;
    }
}

compile

$ javac Composite.java

Output example

$ java Composite 2
10
$ java Composite 4
27
$ java Composite 10
112
$ java Composite 100
7059

Note

This problem is an extra edition of Zunda Problem

Recommended Posts

Japanese Advent Calendar Java Edition of Synthetic Sequences
Six Prime Number Sequence Advent Calendar 2018 Java Edition
Summary of Java Math.random and import (Calendar)
Display Japanese calendar and days of the week using java8 standard class
Memorandum of new graduate SES [Java object-oriented edition]
[Java] Overview of Java
Links for each version (Japanese version) of Java SE API