+1 (315) 557-6473 

Create A Program to Work with Fundamental Theorem of Arithmetic in Java Assignment Solution.


Instructions

Objective
Write a java homework program to work with fundamental theorem of arithmetic.

Requirements and Specifications

Program to work with fundamental theorem of arithmetic in java

Source Code

import java.util.Scanner;

public class Hwk6 {

    /**

     * Method for building factorization of given n

     * @param n integer to build factorization for

     */

    private static void factorization(int n) {

        // argument must be >= 2

        if (n < 2) {

            System.out.println("Input number must be >= 2.");

            return;

        }

        // showing header message

        System.out.println("This program will demonstrate that " + n + " is either prime");

        System.out.println("or is the product of two or more prime numbers.");

        // starting calculation by calling step

        factorizationStep(n, 0);

        // showing footer message

        System.out.println();

        System.out.println("As this output shows, the Fundamental Theorem of Arithmetics holds for " + n + ".");

    }

    /**

     * Helper recursive method fo building and outputting factorization result

     * @param n integer to calculate factorization for

     * @param depth current depth in factorization tree

     */

    private static void factorizationStep(int n, int depth) {

        // iterating over all possible divisors from 2 to sqrt(n).

        // If n is not prime, it will have a divisor among this numbers

        for (int i = 2; i*i <= n; i++) {

            // checking if i divides n

            if (n % i == 0) {

                // getting second divisor

                int div = n / i;

                // making indentation

                for (int j = 0; j < depth; j++) {

                    System.out.print("\t");

                }

                // making processing for different i and div

                if (i != div) {

                    System.out.println(n + " = " + div + " * " + i + "; are these factors either prime or product of primes?");

                    // calling recursive method with increased depth for both divisors

                    factorizationStep(div, depth + 1);

                    factorizationStep(i, depth + 1);

                    // making indentation

                    for (int j = 0; j < depth; j++) {

                        System.out.print("\t");

                    }

                    System.out.println(n + " is the product of primes (" + div + " and " + i + " are prime or prime products).");

                }

                // making processing for square case

                else {

                    System.out.println(n + " = " + div + " squared; are these factors either prime or product of primes?");

                    // calling recursive method with increased depth for sqrt

                    factorizationStep(div, depth + 1);

                    // making indentation

                    for (int j = 0; j < depth; j++) {

                        System.out.print("\t");

                    }

                    System.out.println(n + " is the square of " + div + ", which is prime or the product of primes.");

                }

                return;

            }

        }

        for (int j = 0; j < depth; j++) {

            System.out.print("\t");

        }

        System.out.println(n + " is prime.");

    }

    public static void main(String[] args) {

        int n = Integer.parseInt(args[0]);

        // calling factorization program for given n

        factorization(n);

    }

}