public class MertensFunction {

    public static void main(String[] args) {
        System.out.printf("First 199 terms of the merten function are as follows:%n    ");
        for ( int n = 1 ; n < 200 ; n++ ) {
            System.out.printf("%2d  ", mertenFunction(n));
            if ( (n+1) % 20 == 0 ) {
                System.out.printf("%n");
            }
        }

        for ( int exponent = 3 ; exponent<= 8 ; exponent++ ) {
            int zeroCount = 0;
            int zeroCrossingCount = 0;
            int positiveCount = 0;
            int negativeCount = 0;
            int mSum = 0;
            int mMin = Integer.MAX_VALUE;
            int mMinIndex = 0;
            int mMax = Integer.MIN_VALUE;
            int mMaxIndex = 0;
            int nMax = (int) Math.pow(10, exponent);
            for ( int n = 1 ; n <= nMax ; n++ ) {
                int m = mertenFunction(n);
                mSum += m;
                if ( m < mMin ) {
                    mMin = m;
                    mMinIndex = n;
                }
                if ( m > mMax ) {
                    mMax = m;
                    mMaxIndex = n;
                }
                if ( m > 0 ) {
                    positiveCount++;
                }
                if ( m < 0 ) {
                    negativeCount++;
                }
                if ( m == 0 ) {
                    zeroCount++;
                }
                if ( m == 0 && mertenFunction(n - 1) != 0 ) {
                    zeroCrossingCount++;
                }
            }
            System.out.printf("%nFor M(x) with x from 1 to%,d%n", nMax);        
            System.out.printf("The maximum of M(x) is M(%,d) =%,d.%n", mMaxIndex, mMax);
            System.out.printf("The minimum of M(x) is M(%,d) =%,d.%n", mMinIndex, mMin);
            System.out.printf("The sum of M(x) is%,d.%n", mSum);
            System.out.printf("The count of positive M(x) is%,d, count of negative M(x) is%,d.%n", positiveCount, negativeCount);
            System.out.printf("M(x) has%,d zeroes in the interval.%n", zeroCount);
            System.out.printf("M(x) has%,d crossings in the interval.%n", zeroCrossingCount);
        }
    }

    private static int MU_MAX = 100_000_000;
    private static int[] MU = null;
    private static int[] MERTEN = null;