Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc220_16;
- /*
- * Name: Nate Wheeler
- * Date: sept 16, 2016
- * Course Number:220
- * Course Name: data structures
- * Problem Number: hw1
- * Email: nate23nate23@gmail.com
- * Short Description of the Problem:
- * Write a program to generate a table of "ugly numbers".
- * Ugly numbers are positive numbers whose prime factors
- * only include 2, 3, 5. For example, 6, 8 are ugly while
- * 14 is not ugly since it includes another prime factor 7.
- * Note that 1 is typically treated as an ugly number.
- */
- import java.util.Scanner;
- public class hw1 {
- public static void main(String[] args) {
- System.out.println("Define the Range: ");
- Scanner sc = new Scanner(System.in);
- System.out.println("the minimum:");
- int min = sc.nextInt();
- System.out.println("the maximum");
- int max = sc.nextInt();
- int[] testa = range(min, max);
- printa(testa);
- System.out.println("The ugly numbers within this range are:");
- int[] check = isUgly(testa);
- printa(check);
- }
- public static int[] isUgly(int[] a) {
- // log for ugly nums
- int[] log = new int[(a.length / 2)];
- int count = 0;
- // run through range
- for (int i = 0; i <= a.length; i++) {
- // Identify factors of each num in range
- int[] temp = factors(a[i]);
- // check if factors are ugly
- if (uglyFacts(temp) == true) {
- log[count] = a[i];
- count++;
- }
- }
- return log;
- }
- public static boolean uglyFacts(int[] a) {
- for (int i = 0; i <= a.length; i++) {
- if (a[i] == 2 || a[i] == 3 || a[i] == 5 || a[i] == 0)
- continue;
- else
- return false;
- }
- return true;
- }
- public static void printa(int[] a) {
- for (int i = 0; i <= a.length + 1; i++)
- System.out.printf("%-3d\n", a[i]);
- }
- public static int[] range(int a, int b) {
- int[] field = new int[b - a + 1];
- for (int i = 0; a <= b; i++, a++)
- field[i] = a;
- return field;
- }
- public static int[] factors(int n) {
- // rated for 90,000,000 max
- int[] see = new int[6];
- for (int i = 2, j = 0; i <= n; i++) {
- while (n % i == 0) {
- see[j] = i;
- // System.out.println(i + "| " + n);
- // System.out.println(" -----");
- n = n / i;
- j++;
- }
- }
- // System.out.println(Arrays.toString(see));
- return see;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement