Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package homework;
- /*
- * Name:George Colon
- * Date:9/15
- * Course Number: csc-220
- * Course Name: Data Structures and Alg
- * Problem Number: IsuglyNumbers
- * Email: Gcolon6085@student.stcc.edu
- * Short Description of the Problem
- */
- import java.util.Scanner;
- import GeorgeLibrary.Library;
- public class UglyNumberFinder {
- // **********************************************
- private static void process(Scanner sc, String args[]) {
- System.out.print("Enter value: ");
- int lowLimit = sc.nextInt();
- int highLimit = sc.nextInt();
- sc.nextLine(); // IMPORTANT!! Reset Scanner
- isUgly(lowLimit, highLimit);
- }
- // **********************************************
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- // **********************************************
- public static void isUgly(int lowLimit, int highLimit) {
- int count = 1;
- System.out.println(" These are the ugly numbers from " + lowLimit + " to " + highLimit + " \n");
- for (int i = lowLimit; i < highLimit; i++) {
- int temp = i;
- double sqrt = Math.sqrt(temp);
- int x = (int) sqrt;
- if (Math.pow(sqrt, 2) == Math.pow(x, 2)) {
- // This finds numbers that are prime in order to exclude them
- } else {
- temp = i;
- while (temp % 2 == 0)
- temp = temp / 2;
- while (temp % 3 == 0)
- temp = temp / 3;
- while (temp % 5 == 0)
- temp = temp / 5;
- if (temp == 1) {
- System.out.printf("%10d) %-15d |", count, i);
- if ((count) % 3 == 0) {
- System.out.println();
- System.out.println(repeatString("-", 90));
- }
- count++;
- }
- }
- }
- }
- // **********************************************
- public static String repeatString(String str, int count) {
- String specFmt = String.format("%%%ds", count);
- String countSpaces = String.format(specFmt, "-");
- return countSpaces.replace(" ", str);
- }
- // **********************************************
- public static void main(String args[]) {
- final String TITLE = "Ugly Number Finder";
- final String CONTINUE_PROMPT = "\nDo this again? [y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- // long startTime = Library.StartTime();
- process(sc, args);
- // Library.endTime(startTime);
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement