2018-1-21

Get the largest prime factor with Reason

The simplest solution to this problem is to list all prime factors, sort, reverse and print the first value in the list.

The third Project Euler problem - Largest Prime Factor - is stated as follows. What is the largest prime factor of the number 600851475143?

let getPrimeFactors = (num) => {
  let rec next = (num, i, res) =>
    switch (num > 1, num mod i === 0) {
    | (true, true) => next(num / i, 2, [i, ...res])
    | (true, false) => next(num, i + 1, res)
    | _ => res
    };
  next(num, 2, [])
};

getPrimeFactors(600851475143)
    |> List.sort(compare)
    |> List.rev
    |> List.hd
    |> print_int;
Jan Järfalk — User experienced esthete, technician, geek and web worker. Aspiring artist and recreational mathematician. I indulge and travel plenty. Constraints are good.