Advertisement
nate23nate23

hw1 csc220 9/16

Sep 16th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package csc220_16;
  2. /*
  3.  * Name: Nate Wheeler
  4.  * Date: sept 16, 2016
  5.  * Course Number:220
  6.  * Course Name: data structures
  7.  * Problem Number: hw1
  8.  * Email: nate23nate23@gmail.com
  9.  * Short Description of the Problem:
  10.  * Write a program to generate a table of "ugly numbers".
  11.  *  Ugly numbers are positive numbers whose prime factors
  12.  *  only include 2, 3, 5. For example, 6, 8 are ugly while
  13.  *  14 is not ugly since it includes another prime factor 7.
  14.  *  Note that 1 is typically treated as an ugly number.
  15.  */
  16.  
  17. import java.util.Scanner;
  18.  
  19. public class hw1 {
  20.  
  21.     public static void main(String[] args) {
  22.         System.out.println("Define the Range: ");
  23.         Scanner sc = new Scanner(System.in);
  24.         System.out.println("the minimum:");
  25.         int min = sc.nextInt();
  26.         System.out.println("the maximum");
  27.         int max = sc.nextInt();
  28.         int[] testa = range(min, max);
  29.         printa(testa);
  30.         System.out.println("The ugly numbers within this range are:");
  31.         int[] check = isUgly(testa);
  32.         printa(check);
  33.  
  34.     }
  35.  
  36.     public static int[] isUgly(int[] a) {
  37.         // log for ugly nums
  38.         int[] log = new int[(a.length / 2)];
  39.         int count = 0;
  40.         // run through range
  41.         for (int i = 0; i <= a.length; i++) {
  42.             // Identify factors of each num in range
  43.             int[] temp = factors(a[i]);
  44.             // check if factors are ugly
  45.             if (uglyFacts(temp) == true) {
  46.                 log[count] = a[i];
  47.                 count++;
  48.             }
  49.         }
  50.         return log;
  51.     }
  52.  
  53.     public static boolean uglyFacts(int[] a) {
  54.         for (int i = 0; i <= a.length; i++) {
  55.             if (a[i] == 2 || a[i] == 3 || a[i] == 5 || a[i] == 0)
  56.                 continue;
  57.             else
  58.                 return false;
  59.         }
  60.         return true;
  61.     }
  62.  
  63.     public static void printa(int[] a) {
  64.         for (int i = 0; i <= a.length + 1; i++)
  65.             System.out.printf("%-3d\n", a[i]);
  66.     }
  67.  
  68.     public static int[] range(int a, int b) {
  69.         int[] field = new int[b - a + 1];
  70.         for (int i = 0; a <= b; i++, a++)
  71.             field[i] = a;
  72.         return field;
  73.     }
  74.  
  75.     public static int[] factors(int n) {
  76.         // rated for 90,000,000 max
  77.         int[] see = new int[6];
  78.  
  79.         for (int i = 2, j = 0; i <= n; i++) {
  80.             while (n % i == 0) {
  81.                 see[j] = i;
  82.                 // System.out.println(i + "| " + n);
  83.                 // System.out.println(" -----");
  84.                 n = n / i;
  85.                 j++;
  86.             }
  87.         }
  88.         // System.out.println(Arrays.toString(see));
  89.         return see;
  90.  
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement