(Hacker Rank) Smallest Multiple - Solution

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static boolean isPrime(int num) {
        for (int i = 2; i < num; i++) {
            if (num%i == 0) {
                return false;
            }
        }
       
        return true;
    }
   
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
       
        int numTests = sc.nextInt();
        for (int i = 0; i < numTests; i++) {
            int n = sc.nextInt();
            ArrayList<Integer> prime = new ArrayList<Integer>();
            for (int j = 2; j <= n; j++) {
                if (isPrime(j)) {
                    prime.add(j);
                }
            }
            int mult = 1;
            for (int j = 0; j < prime.size(); j++) {
                long max = 0;
                long power = 1;
                for (int k = 1; k < 10; k++) {
                    power = (long)Math.pow(prime.get(j), k);
                    if (power <= n) {
                        max = power;
                    }
                }
                mult *= max;
            }         
            System.out.println(mult);
        }
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução