## Project Euler 005 – JavaScript

4 03 2011

Problem 5

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<!–

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?

–>

<head>

<title>Project Euler 005</title>

</head>

<body >

<script type="text/javascript">

factors = [16, 9, 5, 7, 11, 13, 17, 19];

answer = 1;

for (i = 0; i < factors.length; i++) {

answer *= factors[i];

}

document.write("<b>" + answer + "</b>");

</script>

</body>

</html>

Discussion

I looked into a reduce function for JavaScript but it seemed more difficult than a simple loop in this case.  So that’s what you got – a simple loop.

If you have questions, leave a comment or please find me on Twitter (@azzlsoft) or email (rich@azzlsoft.com).