Java Implementation of Problem 5

View source code here on GitHub!

Includes

Problem Solution

public class p0005 implements IEuler
Object answer()
Returns:

The answer to Project Euler problem 5

 1/*
 2Project Euler Problem 5
 3
 4Problem:
 5
 62520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
 7
 8What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
 9*/
10package euler;
11
12import euler.lib.Primes;
13
14public class p0005 implements IEuler {
15    @Override
16    public Object answer() {
17        int answer = 1;
18        int[] factorTracker = new int[20], localFactorTracker = new int[20];
19        for (int i = 2; i < 21; i++) {
20            Primes.primeFactors(i)
21                  .mapToInt(Long::intValue)
22                  .forEach(p -> localFactorTracker[p]++);
23            for (int j = 2; j < 20; j++) {
24                factorTracker[j] = Math.max(factorTracker[j], localFactorTracker[j]);
25                localFactorTracker[j] = 0;
26            }
27        }
28        for (int i = 2; i < 20; i++)
29            for (int j = 0; j < factorTracker[i]; j++)
30                answer *= i;
31        return answer;
32    }
33}

Tags: java-iterator, divisibility, factorization, prime-number