DEV Community


Discussion on: Project Euler Problem 6 Solved with Javascript

mburszley profile image
Maximilian Burszley • Edited

I mean, JavaScript has similar constructs:

function euler6(n) {
  const sumOfSquares = range(n + 1).map((x) => x ** 2).reduce((a, b) => a + b);
  const squareOfSum = range(n + 1).reduce((a, b) => a + b).map((x) => x ** 2);

  return squareOfSum - sumOfSquares;

function range(n) {
  return [...Array(n).keys()];

This could be further simplified with generator functions to get the iterator behavior seen in the Rust example.

Thread Thread
codenutt profile image
Jared Author

Yep, that is definitely one way to do it. I personally prefer more legible code (because I'm a simple human), but this gets the job done.

Thanks for sharing!

Forem Open with the Forem app