import java.util.Scanner; /** NumberTheory.java - Practice 5 problems about a number n. * 1. See if it is prime (function returning boolean). * 2. Determine its prime factorization. * 3. Print out all the factors. Also count them. * Find the sum of the proper factors (those that don't equal n). * Then we can see if n is perfect, deficient, or abundant. * 4. Determine the smallest value of a positive integer k for which * nk is a perfect square. * 5. Goldbach conjecture (as a function that can be called from a loop * for various input cases e.g. 2-5000). Any even integer n>=2 * can be expressed as the sum of two primes. Not applicable for odds. * No error checking. Let's assume input is positive integer. * Future work, if desired: * 1. In the case of Goldbach's conjecture, could provide a range * of number to test. * 2. Highly composite number: This is a number having more factors * than any smaller number. */ public class NumberTheory { // Call findFactors() to count and sum factors, to use later. // I also decided to have a global variable for missingFactor, to avoid // calling printPrimeFactors() more than once. The first time // I would have called the function, I don't yet need this answer. public static int numFactors = 0; public static int sumFactors = 0; public static int missingFactor = 0; /** main - test the methods */ public static void main(String [] args) { System.out.printf("Please enter a positive integer n: "); Scanner in = new Scanner (System.in); int n = Integer.parseInt(in.nextLine().trim()); if (isPrime(n)) System.out.printf("\n%d is prime.\n", n); else System.out.printf("\n%d is NOT prime.\n", n); System.out.printf("\nHere is the prime factorization of %d:\n", n); printPrimeFactors(n); // findFactors can not only print the factors, it also counts // them and finds their sum. System.out.printf("\nHere is a list of all the factors:\n"); findFactors(n, true); System.out.printf("\nThe sum of the proper factors of %d is %d.\n", n, sumFactors); if (isPerfect(n)) System.out.printf("%d is perfect.\n", n); else if (isDeficient(n)) System.out.printf("%d is deficient.\n", n); else if (isAbundant(n)) System.out.printf("%d is abundant.\n", n); else System.out.printf("Whoa! How can %d be neither perfect, deficient, " + "or abundant? Sanity check failed.\n", n); System.out.printf("\nThe missing factor to make %d a perfect square " + "is %d.\n", n, missingFactor); // Let's verify Goldbach's conjecture - could test many cases in a loop. System.out.printf("\nResult of Goldbach's conjecture:\n"); goldbach(n); } /** isPrime - This may seem redundant, but I've decided to make a * stand-alone isPrime() method. This is because when we test the * Goldbach conjecture later, I need to be able to call isPrime() * on many different values, not just on n. * My shortcut for primality is to try all divisors from 2 thru sqrt(n). * If we find one that goes into n, then it is not prime. */ public static boolean isPrime(int n) { return false; } /** printPrimeFactors - This is the verbose version, where we actually * want to print them out. */ public static void printPrimeFactors(int n) { findPrimeFactors(n, true); } /** findPrimeFactors - The prime factorization of n. * Starting at 2, see if the divisor goes evenly into n. Keep dividing * it until it can no more. As you find a factor that goes it, divide * n by it. Stop once n reaches 1. * * This method also solves the missing factor problem, and stores the * result in the global variable missingFactor. In case you just want * to solve this problem and not print out the prime factorization, * then you would specify the 2nd parameter verbose as false. * * Here is the idea behind finding the missing factor: We are to find * the smallest positive integer k for which nk is a perfect square. * Note that if n is already a perfect square, we should return 1. * At the other extreme, if n is prime or is just the product of primes, * then we would return k = n. Thus, 1 <= k <= n. * My technique is to work out the prime factorization. If we * encounter an odd power, that means we need to multiply k by this * new divisor. */ public static void findPrimeFactors(int n, boolean verbose) { } /** findFactors - find the sum of all proper factors (i.e. don't * include n itself as a factor of n). Also count all the * facturs including n itself. These quantities can be stored * in global variables, because we can't return two numbers. * If you want this method to also print all the factors, * then the verbose parameter should be true. For simplicity, * let's assume that all the factors will fit on one line. Otherwise, * we'd have to keep track of the total number of digits as we continue. */ public static void findFactors(int n, boolean verbose) { } // Precondition: When we call isPerfect(), isDeficient() // and isAbundant(), we must already have called findFactors(). public static boolean isPerfect(int n) { return false; } public static boolean isDeficient(int n) { return false; } public static boolean isAbundant(int n) { return false; } /** goldbach - Let's assume Goldbach's conjecture is true. We are given * value n. Find two primes that sum to n. Actually, we could * attempt to find all such pairs. We avoid duplicating a pair * by limiting i to n/2. Reject odd input! */ public static void goldbach(int n) { } }