SHOW:
|
|
- or go back to the newest paste.
1 | /* | |
2 | * Name: | |
3 | * Date: | |
4 | * Course Number: | |
5 | * Course Name: | |
6 | * Problem Number: | |
7 | * Email: | |
8 | * Short Description of the Problem | |
9 | */ | |
10 | ||
11 | ||
12 | import java.util.Scanner; | |
13 | import java.lang.Math; | |
14 | ||
15 | ||
16 | public class UglyNumberTester { | |
17 | ||
18 | ||
19 | //********************************************** | |
20 | ||
21 | private static void input(Scanner sc, String args[]) { | |
22 | ||
23 | int x; | |
24 | int y; | |
25 | ||
26 | System.out.print("Enter starting value: "); | |
27 | x = sc.nextInt(); | |
28 | sc.nextLine(); | |
29 | System.out.print("Enter end value: "); | |
30 | y = sc.nextInt(); | |
31 | sc.nextLine(); | |
32 | System.out.println( process(x,y) ); | |
33 | ||
34 | sc.nextLine(); | |
35 | ||
36 | } | |
37 | ||
38 | private static String process(int x, int y) { | |
39 | String output = ""; | |
40 | ||
41 | while ( x <= y ) { | |
42 | ||
43 | if ( isUgly(x)) | |
44 | { | |
45 | output += " " + x; | |
46 | } | |
47 | x++; | |
48 | } | |
49 | ||
50 | return output; | |
51 | } | |
52 | ||
53 | //checks if the number has prime factors of only 5,3 or 2 | |
54 | private static boolean isUgly(int z) { | |
55 | ||
56 | while(z % 5 == 0) | |
57 | { | |
58 | z /=5; | |
59 | } | |
60 | ||
61 | while(z % 3 == 0) | |
62 | { | |
63 | z /=3; | |
64 | } | |
65 | ||
66 | while (z % 2 == 0) | |
67 | { | |
68 | z /=2; | |
69 | } | |
70 | ||
71 | ||
72 | if(z >= 7) | |
73 | { | |
74 | return false; | |
75 | } | |
76 | ||
77 | return true; | |
78 | } | |
79 | ||
80 | //********************************************** | |
81 | ||
82 | private static boolean doThisAgain(Scanner sc, String prompt) { | |
83 | System.out.print(prompt); | |
84 | String doOver = sc.nextLine(); | |
85 | return doOver.equalsIgnoreCase("Y"); | |
86 | } | |
87 | ||
88 | //********************************************** | |
89 | ||
90 | public static void main(String args[]) { | |
91 | final String TITLE = "Ugly Number Tester"; | |
92 | final String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
93 | ||
94 | System.out.println("Welcome to " + TITLE); | |
95 | Scanner sc = new Scanner(System.in); | |
96 | do { | |
97 | input(sc, args); | |
98 | } while (doThisAgain(sc, CONTINUE_PROMPT)); | |
99 | sc.close(); | |
100 | System.out.println("Thank you for using " + TITLE); | |
101 | } | |
102 | ||
103 | } |