Question Number
5
Project Euler 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?

Solution Found
True

File Status: Solution File found. Code from the function code_solution_5 will be used

Solution: 232792560

Solution Code:

      <?php

  function code_solution_5() {
    $prime_factors = [];

    $start = 2;
    $end = 20;

    for($i = $start; $i<= $end; $i++) {
      $prime_factors[$i] = get_prime_factors($i);
    }

    $prime_combined = [];
    foreach($prime_factors as $prime_factor_array) {
      foreach($prime_factor_array as $key => $value) {
        $old_val = $prime_combined[$key] ?? 0;
        $prime_combined[$key] = max([$old_val, $value]);
      }
    }

    $product = 1;
    foreach($prime_combined as $prime => $exponent) {
      $product *= pow($prime, $exponent);
    }

    return $product;
  }