-6

Problem:

"Babylonians use regular numbers to keep time.

A "regular number" in math is defined to be a number that is a factor of some power of 60 (60, 3600, etc). Equivalently we can say that a regular number is one whose ONLY prime divisors are 2, 3 and 5. The first 10 regular numbers are:

1, 2, 3, 4, 5, 6, 8, 9, 10, 12.

Your task is to find the n-th regular number."

My professor asked us to specifically use priority queue to solve this problem, and the largest number we'll test is no more than n = 300. I'm clueless.

edit: I'm obviously not asking for the complete code, I just need pointers to get me started.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
jahnpahl
  • 19
  • 1

1 Answers1

0

Not sure why the use of PriorityQueue, but a pretty ugly brute force way to solve the question would be something like this.

import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.stream.Collectors;

public class Driver {

    public static void main(String[] args) {
        Driver driver = new Driver();
        System.out.println(driver.getRegularNumber(30)
                .toString());
    }

    public PriorityQueue<Integer> getRegularNumber(int n) {
        PriorityQueue<Integer> regularNumbers = new PriorityQueue<>();
        int count = 0;
        int current = 1;
        List<Integer> pf;
        List<Integer> apf = Arrays.asList(2, 3, 5);
        while (count < n) {
            // Get the prime factors of the current number.
            // If it is a subset of the allowed prime factors then add it to the Queue.
            pf = primeFactors(current);
            if (apf.containsAll(pf)) {
                regularNumbers.add(current);
                count++;
            }
            current++;
        }
        return regularNumbers;
    }

    public static List<Integer> primeFactors(int n) {
        List<Integer> factors = new ArrayList<>();
        for (int i = 2; i < n; i++) {
            while (n % i == 0) {
                factors.add(i);
                n = n / i;
            }
        }
        if (n > 2) {
            factors.add(n);
        }

        factors = factors.stream()
                .distinct()
                .collect(Collectors.toList());
        return factors;
    }
}   

This produces [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80] as output.

LockieR
  • 370
  • 6
  • 19