Project Euler #5: Smallest multiple

Question
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Answer : 232792560

Hacker Rank Question

Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.*;

public class Solution {

    public static long[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            int n = in.nextInt();
            long output = 1;
            for(int i = 0; i < primes.length; i++) {
                long testDivisor = 1;
                while (testDivisor * primes[i] <= n) {
                    testDivisor *= primes[i];
                    output *= primes[i];
                }
            }
            System.out.println(output);
        }
    }
}

Comments

Popular Posts